#!/bin/sh
# Glacier - A source based package manager written in POSIX sh
# (C) 2022 Everest Developers
# This program is free software: you can redistribute it and/or modify it 
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>. 

# Preloading

source /etc/glacier/hooks.sh

while [[ $# -gt 0 ]]; do
	case $1 in
		-h|--help)
			printf "$blue [ * ] Glacier - A source based package manager written in POSIX sh [ * ] $reset\n"
			printf "$blue=== Information ====================================================================================$reset\n"
			printf "$ glacier {-h --help}			show this message and exit\n"
			printf "$ glacier {--version}			display the current Glacier version and exit\n"
			printf "$blue=== Package Operations =============================================================================$reset\n"
			printf "# glacier {install -f}			install a package\n"
			printf "# glacier {update -u}			update a package\n"
			printf "# glacier {remove -x}			remove a package\n"
			printf "$ glacier {query -q}			query a package\n"
			printf "# glacier {cache -c}			cache a package\n"
			printf "# glacier {cache-install -ci}		install a cached package\n"
			printf "# glacier {cache-clear -cc}		clear the package cache\n"
			printf "$blue=== Debugging ======================================================================================$reset\n"
			printf "$ glacier {--showDebugInfo}		show debugging info\n"
			printf "$blue=== Licensing ======================================================================================$reset\n"
			printf "This program is freely distributable under the terms of the GNU General Public License version 3 or later.\n"
			printf "You should have recieved a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.\n"
			
			exit 0
		;;
		--version)
			printf "$blue Glacier v2.2.3 $reset\n"
			exit 0
		;;
		install|-f)
			# Require the script to be run as root
			if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    				printf "\033[1;31m [ \xE2\x9C\x95 ] \033[m Please run Glacier as root.\n"
    				exit
			fi

			# Get package name and download package archive
			printf "\033[1;34m [ ? ] \033[m Enter package name: " && read input
			printf "\033[1;34m [ i ] \033[m Installing $input.tar.gz...\n"
			printf "\033[1;34m [ i ] \033[m Checking databases... " && $GLACIER_DOWNLOAD_BACKEND $GREPO1/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO2/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO3/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO4/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO5/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO6/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO7/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO8/$input.tar.gz
				if [ "$?" != "0" ]; then
    					printf "\033[1;31m [ \xE2\x9C\x95 ] \033[m Package not found.\n" 1>&2
    					exit 1
				fi
			# Unpack Tarball
			printf "\033[1;34m [ i ] \033[m Unpacking $input.tar.gz...\n"
			mkdir $input && mv $input.tar.gz $input && cd $input
			printf "$blue [ i ]$reset Verifying integrity of package...\n"
			sha256sum $input.tar.gz
			tar -xf $input.tar.gz
			if [ "$?" != "0" ]; then
    				printf "\033[1;31m [ \xE2\x9C\x95 ] \033[m Could not unpack $input.tar.gz.\n" 1>&2
    				cd .. && rm -rf $input
				exit 1
			fi
		# Run installation hooks
		chmod +x INSTALL.sh
		chmod +x $input.ts.sh
		printf "$blue [ i ]$reset  The following instruction set will be executed:\n"
		cat INSTALL.sh
		printf "\033[1;34m [ i ] \033[m Executing installation instructions...\n"
		./INSTALL.sh
			if [ "$?" != "0" ]; then
				printf "$red [ $error ]$reset Instructions were either unable to execute, or encountered an error while executing.\n" 1>&2
				cd .. && rm -rf $input
				exit 1
			fi
		./$input.ts.sh
		# Clean up
		printf "\033[1;34m [ i ] \033[m Cleaning up...\n"
		mv $input-pkginfo.json /etc/glacier/pkginfo
		cd ..
		rm -rf $input
		printf "\033[1;32m [ \xE2\x9C\x93 ] \033[m Operation completed.\n"
		exit 0
		;;
		update|-u)
		# Require Glacier to be run as root
		if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    			printf "\033[1;31m [ $error ] \033[m Please run Glacier as root.\n"
    			exit
		fi

		printf "\033[1;34m [ ? ] \033[m Enter package name: " && read input
		printf "\033[1;34m [ i ] \033[m Installing $input.tar.gz...\n"
		printf "\033[1;34m [ i ] \033[m Checking databases... " && $GLACIER_DOWNLOAD_BACKEND $GREPO1/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO2/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO3/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO4/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO5/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO6/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO7/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO8/$input.tar.gz
			if [ "$?" != "0" ]; then
    				printf "\033[1;31m [ $error ] \033[m Package not found.\n" 1>&2
    				exit 1
			fi
		printf "\033[1;34m [ i ] \033[m Unpacking $input.tar.gz...\n"
		mkdir $input && mv $input.tar.gz $input && cd $input
		printf "$blue [ i ]$reset Verifying integrity of package...\n"
		sha256sum $input.tar.gz
		tar -xf $input.tar.gz
			if [ "$?" != "0" ]; then
    				printf "\033[1;31m [ $error ] \033[m Could not unpack $input.tar.gz.\n" 1>&2
    				exit 1
			fi
		rm /var/log/glacier/$input.timestamp
		chmod +x UPDATE.sh
		chmod +x $input.ts.sh
		printf "$blue [ i ]$reset The following instruction set will be executed:\n"
		cat UPDATE.sh
		printf "\033[1;34m [ i ] \033[m Executing update instructions...\n"
		./UPDATE.sh
			if [ "$?" != "0" ]; then
				printf "$red [ $error ]$reset Instructions were either unable to execute, or encountered an error while executing.\n" 1>&2
				exit 1
			fi
		./$input.ts.sh
		printf "\033[1;34m [ i ] \033[m Cleaning up...\n"
		mv $input-pkginfo.json /etc/glacier/pkginfo
		cd ..
		rm -rf $input
		printf "\033[1;32m [ $check ] \033[m Operation completed.\n"
		exit 0
		;;
		remove|-x)
		# Require the script to be run as root
		if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    			printf "\033[1;31m [ $error ] \033[m Please run Glacier as root.\n"
    			exit
		fi

		printf "\033[1;34m [ ? ] \033[m Enter package name: " && read input
		printf "\033[1;34m [ i ] \033[m Removing $input.tar.gz...\n"
		printf "\033[1;34m [ i ] \033[m Checking databases... " && $GLACIER_DOWNLOAD_BACKEND $GREPO1/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO2/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO3/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO4/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO5/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO6/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO7/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO8/$input.tar.gz
			if [ "$?" != "0" ]; then
    				printf "\033[1;31m [ $error ] \033[m Package not found.\n" 1>&2
    				exit 1
			fi
		printf "\033[1;34m [ i ] \033[m Unpacking $input.tar.gz...\n"
		mkdir $input && mv $input.tar.gz $input && cd $input
		printf "$blue [ i ]$reset Verifying integrity of package...\n"
		sha256sum $input.tar.gz
		tar -xf $input.tar.gz
			if [ "$?" != "0" ]; then
    				printf "\033[1;31m [ $error ] \033[m Could not unpack $input.tar.gz.\n" 1>&2
    				exit 1
			fi
		chmod +x REMOVE.sh
		printf "$blue [ i ] $reset The following instruction set will be executed:\n"
		cat REMOVE.sh
		printf "$blue [ i ] $reset Executing removal instructions...\n"
		./REMOVE.sh
			if [ "$?" != "0" ]; then
				printf "$red [ $error ]$reset Instructions were either unable to execute, or encountered an error while executing.\n" 1>&2
				exit 1
			fi
		rm /var/log/glacier/$input.timestamp
		printf "\033[1;34m [ i ] \033[m Cleaning up... \n" # Status message
		cd ..
		rm -rf $input
		rm /etc/glacier/pkginfo/$input-pkginfo.json
		printf "\033[1;32m [ $check ] \033[m Operation completed.\n"
		exit 0
		;;
		query|-q)
		printf "\033[1;34m [ ? ] \033[m Enter package name: " && read input
		cat /etc/glacier/pkginfo/$input-pkginfo.json
		cat /var/log/glacier/$input.timestamp
			if [ "$?" != "0" ]; then
    				printf "\033[1;31m [ $error ] \033[m Package not found.\n" 1>&2
    				exit 1
			fi
		exit 0
		;;
		cache|-c)
		# Require the script to be run as root
		if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    			printf "\033[1;31m [ $error ] \033[m Please run Glacier as root.\n"
    			exit
		fi

		printf "\033[1;34m [ ? ] \033[m Enter package name: " && read input
		printf "\033[1;34m [ i ] \033[m Caching $input.tar.gz...\n"
		printf "\033[1;34m [ i ] \033[m Checking databases... " && $GLACIER_DOWNLOAD_BACKEND $GREPO1/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO2/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO3/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO4/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO5/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO6/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO7/$input.tar.gz || $GLACIER_DOWNLOAD_BACKEND $GREPO8/$input.tar.gz
			if [ "$?" != "0" ]; then
    				printf "\033[1;31m [ $error ] \033[m Package not found.\n" 1>&2
    				exit 1
			fi
		printf "$blue [ i ]$reset Verifying integrity of package...\n"
		sha256sum $input.tar.gz
		mv $input.tar.gz /var/cache/glacier
		printf "\033[1;32m [ $check ] \033[m Operation completed.\n"
		exit 0
		;;
		cache-install|-ci)
		# Require the script to be run as root
		if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    			echo "$red[ $error ]$reset Please run Glacier as root.\n"
    			exit
		fi

		printf "\033[1;34m [ ? ] \033[m Enter package name: " && read input
		printf "\033[1;34m [ i ] \033[m Checking cache for $input.tar.gz...\n"
		cd /var/cache/glacier && cp $input.tar.gz /tmp && cd /tmp
			if [ "$?" != "0" ]; then
    				printf "\033[1;31m [ $error ] \033[m Could not fetch package from cache.\n" 1>&2
    				exit 1
			fi
		printf "\033[1;34m [ i ] \033[m Unpacking $input.tar.gz...\n"
		mkdir $input && mv $input.tar.gz $input && cd $input
		tar -xf $input.tar.gz
			if [ "$?" != "0" ]; then
    				printf "\033[1;31m [ $error ] \033[m Could not unpack $input.tar.gz.\n" 1>&2
    				exit 1
			fi
		chmod +x INSTALL.sh
		chmod +x $input.ts.sh
		./INSTALL.sh # Actually executes installation script
			if [ "$?" != "0" ]; then
				printf "$red [ $error ]$reset Instructions were either unable to execute, or encountered an error while executing.\n" 1>&2
				exit 1
			fi
		./$input.ts.sh
		printf "\033[1;34m [ i ] \033[m Cleaning up...\n" # Status message
		mv -v $input-pkginfo.json /etc/glacier/pkginfo
		cd ..
		rm -rvf $input
		printf "\033[1;32m [ $check ] \033[m Operation completed.\n"
		exit 0
		;;
		cache-clear|-cc)
		# Require the script to be run as root
		if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    			printf "\033[1;31m [ $error ] \033[m Please run Glacier as root.\n"
    			exit
		fi

		printf "\033[1;34m [ i ] \033[m Clearing cache...\n"
		cd /var/cache/glacier && rm -rf *
			if [ "$?" != "0" ]; then
    				printf "\033[1;31m [ $error ] \033[m Some items could not be cleared.\n" 1>&2
    				exit 1
			fi
		printf "\033[1;32m [ $check ] \033[m Cache cleared.\n"
		exit 0
		;;
		--showDebugInfo)
		printf "Glacier v2.2.1\n"
		printf ":: Checking for viable download backend...\n"
			whereis wget
			whereis curl
			whereis aria2c
		printf ":: Checking which download backend is in use...\n"
			echo $GLACIER_DOWNLOAD_BACKEND
		printf ":: Checking if all Glacier directories and files are present...\n"
			ls /etc | grep glacier
			ls /etc/glacier
			whereis make.conf
		printf ":: Any missing files above, especially make.conf, will cause Glavier to behave abnormally.\n"
		printf ":: Checking that unicode symbols display properly...\n"
			printf "$check\n"
			printf "$error\n"
			printf "$warning\n"
			printf "$question\n"
			printf "$red This is a test.$reset\n"
			printf "$green This is a test.$reset\n"
			printf "$yellow This is a test.$reset\n"
			printf "$blue This is a test.$reset\n"
		printf ":: Ensuring repositories are correct...\n"
			echo $GREPO1
			echo $GREPO2
			echo $GREPO3
			echo $GREPO4
			echo $GREPO5
			echo $GREPO6
			echo $GREPO7
			echo $GREPO8
		printf ":: Debug info has been gathered.\n"
		printf ":: If you wish to save this information to a file, run 'glacier --showDebugInfo > glacier.debug'.\n"
		exit 0
		;;
		-*|--*)
		printf "$red [ $error ]$reset Unknown option, use 'glacier -h' to see usage.\n"
		exit 1
		;;
		*)
		printf "$red [ $error ]$reset Unknown option, use 'glacier -h' to see usage.\n"
		exit 1
		;;
	esac
done

printf "$red [ $error ]$reset Unknown option, use 'glacier -h' to see usage.\n"
