#!/bin/bash

name=groups/tools
ver=1.0
arch=all
dep_apt="clang build-essential libncursesw5-dev autotools-dev autoconf automake gcc g++ make libncurses-dev tar"
dep_apk="ncurses-dev tar automake autoconf clang gcc g++ make ncurses-dev tar"

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

if ! [ -f "/usr/bin/apt" ] && ! [ -f "/sbin/apk" ]; then
    echo "This build script cannot be executed on your system. Sorry"
    exit
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 $dep_apk"

    $cmd &> /dev/null
}

download_sources() {
    curl --output '/tmp/cpufetch.tar.gz' --url 'https://github.com/Dr-Noob/cpufetch/archive/refs/heads/master.tar.gz' 
    curl --output '/tmp/neofetch.tar.gz' --url 'https://sources.juliandev02.ga/packages/main/n//neofetch/neofetch-7.3.8.tar.gz' 
    curl --output '/tmp/htop.tar.gz' --url 'https://github.com/htop-dev/htop/archive/refs/heads/main.tar.gz' 
    mkdir cpufetch
    mkdir neofetch
    mkdir htop
}

extract() {
    tar xf /tmp/cpufetch.tar.gz --directory /tmp/target/cpufetch/
    tar xf /tmp/neofetch.tar.gz --directory /tmp/target/htop/
    tar xf /tmp/htop.tar.gz --directory /tmp/target/htop/
}

compile() {
    echo -e "${Green}>>>${Reset}${Bold} Compiling cpufetch ... "
    cd /tmp/target/cpufetch/cpufetch* || exit
    make
    echo -e "${Green}>>>${Reset}${Bold} Installing cpufetch ... "
    cp /tmp/target/cpufetch/cpufetch*/cpufetch /usr/bin/


    echo -e "${Green}>>>${Reset}${Bold} Compiling htop ... "
    cd /tmp/target/cpufetch/htop* || exit
    ./autogen.sh
    ./configure
    make
    echo -e "${Green}>>>${Reset}${Bold} Installing htop ... "
    make install

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

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

    echo -e "${Green}>>>${Reset}${Bold} Downloading sources ... "
    download_sources

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

    echo -e "${Green}>>>${Reset}${Bold} Starting Build Process ... "
    sleep 1
    compile

}

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

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

    echo -e "${Green}>>>${Reset}${Bold} Starting Build Process ... "
    sleep 1
    compile
}

remove() {
    echo -e "${Green}>>>${Reset}${Bold} Removing cpufetch, htop and neofetch ... "
    rm -f /usr/bin/cpufetch
    rm -f /usr/bin/htop
    rm -f /usr/local/bin/htop
    rm -f /usr/bin/neofetch
    read -rep $'\033[0;33m>>>\033[0m\e[1m Remove dependencies? [Y/N] ' ans

    if [[ "${ans}" != "y" ]] && [[ "${ans}" != "Y" ]] && [[ "${ans}" != "j" ]] && [[ "${ans}" != "j" ]]; then
        exit
    else 
        [[ "${pkg_mgr}" == "apt" ]] && cmd="apt remove -y $dep_apt"
        [[ "${pkg_mgr}" == "apk" ]] && cmd="apk remove $dep_apk"

        $cmd &> /dev/null
    fi
}

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