#!/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 /tmp/koffe/rootfs/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
    mkdir /tmp/koffe/
    mkdir /tmp/koffe/l/
    mkdir /tmp/koffe/n/
    mkdir /tmp/koffe/aur/
    mkdir /tmp/koffe/aurbg/
	mkdir /tmp/koffe/rootfs/
	cp -ar /usr/share/archiso/configs/releng/* /tmp/koffe/rootfs/.
    mkdir /tmp/koffe/rootfs/airootfs/root/pkg/
    cp $pts/* /tmp/koffe/rootfs/airootfs/root/
    echo '' > /tmp/koffe/rootfs/airootfs/root/install.list
    mkdir ~/koffeiso
    mkdir ~/out
    rm -rf /var/cache/pacman/pkg/*
    pactree -u -s libnewt > /tmp/koffe/n/libnewt
    pacman -Spdd --noconfirm - < /tmp/koffe/n/libnewt > /tmp/koffe/l/libnewt
    echo libnewt >> /tmp/koffe/rootfs/airootfs/root/install.list
    wget -nc -i /tmp/koffe/l/libnewt -P /tmp/koffe/rootfs/airootfs/root/pkg/

	pactree -u -s efibootmgr > /tmp/koffe/n/efibootmgr
    pacman -Spdd --noconfirm - < /tmp/koffe/n/efibootmgr > /tmp/koffe/l/efibootmgr
    echo efibootmgr >> /tmp/koffe/rootfs/airootfs/root/install.list
    wget -nc -i /tmp/koffe/l/efibootmgr -P /tmp/koffe/rootfs/airootfs/root/pkg/


    pactree -u -s grub > /tmp/koffe/n/grub
    pacman -Spdd --noconfirm - < /tmp/koffe/n/grub > /tmp/koffe/l/grub
    echo grub >> /tmp/koffe/rootfs/airootfs/root/install.list
    wget -nc -i /tmp/koffe/l/grub -P /tmp/koffe/rootfs/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
		PACAUR=0
        clear
        echo $app
        variable=$(pacman -Sg $app)
        if [[ $variable == '' ]];
        then
            #base
			aurornot=$(pactree -s -u $app 2>&1 >/dev/null | cut -f 1 -d ':')
			if [[ $auronnot != "error" ]];
			then
            	pactree -u -s $app > /tmp/koffe/n/$app
            	pacman -Spdd --noconfirm - < /tmp/koffe/n/$app > /tmp/koffe/l/$app
            	echo $app >> /tmp/koffe/rootfs/airootfs/root/install.list
            	wget -nc -i /tmp/koffe/l/$app -P /tmp/koffe/rootfs/airootfs/root/pkg/
			fi
			
			if [[ $aurornot == "error" ]];
			then
				PACAUR=1
			fi
        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 >> /tmp/koffe/rootfs/airootfs/root/install.list
            done
            pacman -Spdd --noconfirm - < /tmp/koffe/grtree > /tmp/koffe/grlinks
            wget -nc -i /tmp/koffe/grlinks -P /tmp/koffe/rootfs/airootfs/root/pkg/
        fi
		if [[ $PACAUR == 1 ]];
		then
			echo "AUR PACKAGE! BEWARE"
        	mkdir /tmp/koffe/home
        	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
        	echo $app >> /tmp/koffe/rootfs/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
				aurdep_aur=$(pactree -s -u $depend 2>&1 >/dev/null | cut -f 1 -d ':')
				if [[ $aurdep_aur != "error" ]];
				then
            	pactree -u -s $depend > /tmp/koffe/n/$depend
            	pacman -Spdd --noconfirm - < /tmp/koffe/n/$depend > /tmp/koffe/l/$depend
            	wget -nc -i /tmp/koffe/l/$depend -P /tmp/koffe/rootfs/airootfs/root/pkg/
				fi
				if [[ $aurdep_aur == "error" ]];
				then
				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 /tmp/koffe/rootfs/airootfs/root/pkg
				fi
        	done
			declare -a bbm
        	bbm=$(cat /tmp/koffe/aurbg/$app/.SRCINFO | grep makedepends | cut -f 2 -d '=' | cut -f 1 -d '>' | cut -f 1 -d ':')
        	for depend in $bbm
        	do
				pacman -S $depend --noconfirm
			done
			sudo -u nobody makepkg --noconfirm --skippgpcheck
        	cp /tmp/koffe/aurbg/$app/*.pkg.tar.zst /tmp/koffe/rootfs/airootfs/root/pkg
		fi
    done
    repo-add /tmp/koffe/rootfs/airootfs/root/pkg/koffe.db.tar.gz /tmp/koffe/rootfs/airootfs/root/pkg/*
	echo "bash ~/install" > /tmp/koffe/rootfs/airootfs/root/.zshrc
    mkarchiso -v -w ~/koffeiso -o $pathto/ /tmp/koffe/rootfs/
    rm -rf ~/koffeiso
    rm -rf /tmp/koffe/rootfs//airootfs/root/*
    rm -rf /tmp/koffe/
fi
      
