#!/bin/bash

name=neofetch
ver=7.1.0
arch=all
dep_apt="bash"
dep_apk="bash"

Reset='\033[0m'
Bold="\e[1m"
Green='\033[0;32m'

if ! [ -f "/usr/bin/apt" ] && ! [ -f "/sbin/apk" ]; then
    echo "This package script is official not supported for your system."
    echo "Make sure, you have installed the following dependency: bash"
fi

[[ -f "/usr/bin/apt" ]] && pkg_mgr="apt"
[[ -f "/sbin/apk" ]] && pkg_mgr="apk"

if (( $(id -u) != 0 )); then
    echo "This package script can only be run with root (permisson denied)"
    exit
fi

get_dependencies() {
    [[ "${pkg_mgr}" == "apt" ]] && cmd="apt install -y $dep_apt"
    [[ "${pkg_mgr}" == "apk" ]] && cmd="apk add bash $dep_apk"

    $cmd &> /dev/null
}

extract() {
    cd /tmp || exit
    mv neofetch* target.tar.gz
    mkdir /tmp/target/
    tar xf /tmp/target.tar.gz --directory /tmp/target/
}

inst() {
    echo -e "${Green}>>>${Reset}${Bold} Installing dependencies ... "
    get_dependencies

    echo -e "${Green}>>>${Reset}${Bold} Extracting sources ... "
    extract

    echo -e "${Green}>>>${Reset}${Bold} Installing neofetch ... "
    install -Dt /usr/bin/ -m 775 /tmp/target/hyfetch*/neofetch
}

upgrade() {
    echo -e "${Green}>>>${Reset}${Bold} Extracting sources ... "
    extract

    echo -e "${Green}>>>${Reset}${Bold} Installing neofetch ... "
    install -Dt /usr/bin/ -m 775 /tmp/target/hyfetch*/neofetch
}

remove() {
    echo -e "${Green}>>>${Reset}${Bold} Removing neofetch ... "
    rm -f /usr/bin/neofetch
}

clean_tmp() {
    echo -e "${Green}>>>${Reset}${Bold} Cleaning temporary files ... "
    rm -rf /tmp/target*
    rm -f /tmp/PKGBUILD
}

if [ "$1" = "--install" ]; then
    inst
    clean_tmp

elif [ "$1" = "--upgrade" ]; then
    upgrade
    clean_tmp

elif [ "$1" = "--remove" ]; then
    remove
    clean_tmp
fi