#!/bin/bash
x=1
pts=/usr/share/koffe/
pathto=$(realpath ./)
if [ $1 ]; 
then
    for argument in $@
    do
        if [[ $argument == "--help" ]];
        then
            echo "koffe is a tool for making simple and intuitive offline archlinux installers" 
            echo "with support for most packages, package groups or aur packages."
            echo
            echo "The programs you select are the only ones getting installed" 
            echo "(and the dependencies, of course), so make sure to include packages that are needed for a" 
            echo "healthy arch install. So smthng like "
            echo "'koffe --pacs=base,linux,linux-firmware,sudo,grub,xorg,gnome,minecraft-launcher,code,yay'" 
            echo "and any other packages you want to install," 
            echo "you can also use whatever WM or DE you want, just keep in mind if the greeter is anything" 
            echo "other than gdm, sddm or lightdm you need to enable it yourself. Also, if you're" 
            echo "making an archiso to be used on many computers and include multiple sets of drivers," 
            echo "in the live disk you can edit 'install.list' to manage what programs to install"
            echo
            echo "Once you're done with the iso, to install arch just boot from it and do 'bash install'"
            echo ---------------------------------------------------
            echo 
            echo Flags:
            echo "koffe --pacs=Prog1,Prog2 -----> starts koffe and creates" 
            echo "                                an iso with the programs Prog1 and Prog2"
            echo "                                IMPORTANT: Separate the progs with commas"
            echo "                                and do not use spaces"
            echo
            echo "koffe --clean ----------------> cleans building dirs after unexpected" 
            echo "                                exit or unsuccesful iso generation. Will"
            echo "                                nullify any other args parsed with it and will"
            echo "                                not create an iso"
            echo
            echo "koffe --scripts=/path/path/ --> copies the scripts in the specified path"
            echo "                                and uses them instead of the default koffe"
            echo "                                scripts in /usr/share/koffe/. Avoid paths with spaces"
            exit
        fi
        if [[ $(echo $argument | cut -f 1 -d '=') == "--pacs" ]];
        then
            declare -a prog
            x=0
            prog=$(echo $argument | cut -f 2 -d '=')
            prog=${prog//,/ }
            echo $prog
        fi
        if [[ $(echo $argument | cut -f 1 -d '=') == "--scripts" ]];
        then
            pts=$(echo $argument | cut -f 2 -d '=')
        fi
        if [[ $argument == "--clean" ]];
        then
            if [[ $(whoami) == root ]];
            then
                rm -rf ~/koffeiso
                rm -rf /usr/share/archiso/configs/releng/airootfs/root/*
                rm -rf /tmp/koffe/
                exit
            fi
        fi
    done
fi
if [ $(whoami) != root ];
then
    echo
    echo WARNING: the program needs to be ran as root
    echo $prog
fi
if [ $(whoami) == root ];
then
    cp $pts/* /usr/share/archiso/configs/releng/airootfs/root/
    echo '' > /usr/share/archiso/configs/releng/airootfs/root/install.list
    pacman -Sy
    mkdir /tmp/koffe/
    mkdir /tmp/koffe/l/
    mkdir /tmp/koffe/n/
    mkdir /tmp/koffe/aur/
    mkdir /tmp/koffe/aurbg/
    mkdir /usr/share/archiso/configs/releng/airootfs/root/pkg/
    mkdir ~/koffeiso
    mkdir ~/out
    rm -rf /var/cache/pacman/pkg/*
    pactree -u -s libnewt > /tmp/koffe/n/libnewt
    pacman -Sp --noconfirm - < /tmp/koffe/n/libnewt > /tmp/koffe/l/libnewt
    echo libnewt >> /usr/share/archiso/configs/releng/airootfs/root/install.list
    wget -nc -i /tmp/koffe/l/libnewt -P /usr/share/archiso/configs/releng/airootfs/root/pkg/
    pactree -u -s grub > /tmp/koffe/n/grub
    pacman -Sp --noconfirm - < /tmp/koffe/n/grub > /tmp/koffe/l/grub
    echo grub >> /usr/share/archiso/configs/releng/airootfs/root/install.list
    wget -nc -i /tmp/koffe/l/grub -P /usr/share/archiso/configs/releng/airootfs/root/pkg/
    if [[ x == 1 ]];
    then
        declare -a prog
        prog=$(whiptail --inputbox "Please enter all the programs to be included in the installer separated by a space, you can also include package groups like gnome or xorg or AUR packages" 0 0 4 3>&1 1>&2 2>&3 3>&-)
    fi
    for app in $prog
    do
        clear
        echo $app
        variable=$(pacman -Sg $app)
        if [[ $variable == '' ]];
        then
            #base
            pactree -u -s $app > /tmp/koffe/n/$app
            pacman -Sp --noconfirm - < /tmp/koffe/n/$app > /tmp/koffe/l/$app
            echo $app >> /usr/share/archiso/configs/releng/airootfs/root/install.list
            wget -nc -i /tmp/koffe/l/$app -P /usr/share/archiso/configs/releng/airootfs/root/pkg/
        fi
        if [[ $variable != '' ]];
        then
            #gnome
            pacman -Sg $app > /tmp/koffe/generaltemp
            cut -f 2- -d ' ' /tmp/koffe/generaltemp > /tmp/koffe/grlist
            grlist=$(cat /tmp/koffe/grlist)
            echo '' > /tmp/koffe/grtree
            for component in $grlist
            do
                pactree -s -u $component >> /tmp/koffe/grtree
                echo $component >> /usr/share/archiso/configs/releng/airootfs/root/install.list
            done
            pacman -Sp --noconfirm - < /tmp/koffe/grtree > /tmp/koffe/grlinks
            wget -nc -i /tmp/koffe/grlinks -P /usr/share/archiso/configs/releng/airootfs/root/pkg/
        fi
        mkdir /tmp/koffe/home
        sudo usermod -d /tmp/koffe/home/ nobody
        git clone https://aur.archlinux.org/$app /tmp/koffe/aurbg/$app
        chown -hR nobody /tmp/koffe/
        cd /tmp/koffe/aurbg/$app
        sudo -u nobody makepkg --noconfirm --skippgpcheck
        cp /tmp/koffe/aurbg/$app/*.pkg.tar.zst /usr/share/archiso/configs/releng/airootfs/root/pkg
        echo $app >> /usr/share/archiso/configs/releng/airootfs/root/install.list
        declare -a bb
        bb=$(cat /tmp/koffe/aurbg/$app/.SRCINFO | grep depends | cut -f 2 -d '=' | cut -f 1 -d '>' | cut -f 1 -d '"')
        for depend in $bb
        do
            pactree -u -s $depend > /tmp/koffe/n/$depend
            pacman -Sp --noconfirm - < /tmp/koffe/n/$depend > /tmp/koffe/l/$depend
            wget -nc -i /tmp/koffe/l/$depend -P /usr/share/archiso/configs/releng/airootfs/root/pkg/
            git clone https://aur.archlinux.org/$depend /tmp/koffe/aurbg/$depend
            chown -hR nobody /tmp/koffe/$depend
            cd /tmp/koffe/aurbg/$depend
            sudo -u nobody makepkg --noconfirm --skippgpcheck
            cp /tmp/koffe/aurbg/$depend/*.pkg.tar.zst /usr/share/archiso/configs/releng/airootfs/root/pkg
        done
    done
    repo-add /usr/share/archiso/configs/releng/airootfs/root/pkg/koffe.db.tar.gz /usr/share/archiso/configs/releng/airootfs/root/pkg/*
    mkarchiso -v -w ~/koffeiso -o $pathto/ /usr/share/archiso/configs/releng
    rm -rf ~/koffeiso
    rm -rf /usr/share/archiso/configs/releng/airootfs/root/*
    rm -rf /tmp/koffe/
fi
      
