#!/bin/sh

#check if root is running this
if [ "`id -u`" != "0" ]; then
        echo "Permission denied, must be root!" 1>&2
        exit 1
fi

# colour for output
txtbld=$(tput bold)
txtund=${txtbld}$(tput sgr 0 1)
txtred=$(tput setaf 1)
txtyellow=$(tput setaf 3)
txtblue=$(tput setaf 4)
txtreset=$(tput sgr0)

downloadPkg()
{
echo -e -n "\tDownloading...\t\t"
opkg-cl -o "$1" -f /etc/opkg/opkg.conf --cache /var/gnx-installer/packageCache --download-only install $2 > /var/gnx-installer/tmpLog 2>&1
if [ "$?" != "0" ]; then
	echo -e "${txtbld}${txtred}[ERROR]${txtreset}"
	echo -n "opkg output reads:"
	cat /var/gnx-installer/tmpLog
else
	echo -e "${txtbld}${txtblue}[OK]${txtreset}"
fi
}

installPkg()
{
echo -e -n "\tInstalling...\t\t"
opkg-cl -o $1 -f /etc/opkg/opkg.conf --cache /var/gnx-installer/packageCache install $2 > /var/gnx-installer/tmpLog 2>&1
if [ "$?" != "0" ]; then
	echo -e "${txtbld}${txtred}[ERROR]${txtreset}"
	echo -n "opkg output reads:"
	cat /var/gnx-installer/tmpLog
else
	echo -e "${txtbld}${txtblue}[OK]${txtreset}"
fi
}

#delete package in the package cache, in case we are downloading to memory (don't want it to fill up with archives that are already installed)
cleanUp()
{
echo -e -n "\tCleaning up...\t\t"
rm -f /var/gnx-installer/packageCache/* /var/gnx-installer/tmpLog
if [ "$?" != "0" ]; then
	echo -e "${txtbld}${txtred}[ERROR]${txtreset}"
else
	echo -e "${txtbld}${txtblue}[OK]${txtreset}"
fi
}

configurePkg()
{

if [ -z "$2" ]; then
	#we are configuring in root
	echo -e -n "\tConfiguring at /..."
	opkg-cl configure $1 > /var/gnx-installer/tmpLog 2>&1
else
	#we are configuring under chroot
	echo -e -n "\tConfiguring at $2..."
	chroot $2 opkg-cl configure $1  > /var/gnx-installer/tmpLog 2>&1
fi

if [ "$?" != "0" ]; then
	echo -e "${txtbld}${txtred}[ERROR]${txtreset}"
	echo "opkg output reads: `cat /var/gnx-installer/tmpLog`"
else
	echo -e "${txtbld}${txtblue}[OK]${txtreset}"
fi
}

#check if GNX_PATH and PKG_LIST is provided
if [ -z "$GNX_PATH" ]; then
	echo "GNX_PATH must be set to the absolute path of the root of Guinnux so the we know where to install Guinnux packages" 1>&2
        exit 1
fi
if [ -z "$PKG_LIST" ]; then
        echo "PKG_LIST must be set to the absolute path of the package list so that we know what packages to install" 1>&2
        exit 1
else
	if [ -f "$PKG_LIST" ]; then
		echo "Using package list at $PKG_LIST"
	else
		echo "Error: $PKG_LIST: no such file" 1>&2
		exit 1
	fi
fi

#check if GNX_PATH has been opened by openGnx
mountpoint -q "$GNX_PATH"
if [ "$?" != "0" ]; then
	echo "Error: $GNX_PATH has not been opened for the installer, please use openGnx to achieve this." 1>&2
	exit 1
fi

#make the opkg clibdir in GNX_PATH if it doesn't exist
mkdir -p ${GNX_PATH}/var/lib/opkg

#update the package list in the GNX_PATH
echo -n -e "${txtund}${txtblue}Updating package list on $GNX_PATH (the Guinnux root)...${txtreset}\n..."
opkg-cl -o $GNX_PATH -f /etc/opkg/opkg.conf update > /var/gnx-installer/tmpLog 2>&1
if [ "$?" != "0" ]; then
        echo "Error: Failed to update the package list in $GNX_PATH" 1>&2
	echo "opkg output read: `cat /var/gnx-installer/tmpLog`" 1>&2
        exit 1
else
	echo -e "${txtbld}${txtblue}[OK]${txtreset}"
fi

#keep record of the curent package we are dealing with
let "currPkg = 1" 
#record the total number of packages
let "numPkgs = 0"
for pkg in `cat $PKG_LIST`; do
        let "numPkgs += 1"
done

#determine if we are installing to the root or to a chroot
#then act appropriately
echo -e "${txtund}${txtblue}Installing packages to $GNX_PATH (the Guinnux root)...${txtreset}"
if [ "$GNX_PATH" == "/" ]; then
	#we are performing a root install, so we can download install and configure without chroot
	for pkg in `cat $PKG_LIST`; do
		echo -e "${txtbld}${txtblue}($currPkg/$numPkgs) $pkg:${txtreset}"

		downloadPkg "/" $pkg
		installPkg "/" $pkg
		configurePkg $pkg	
		cleanUp $pkg

		let "currPkg += 1"
	done
		
else
	#we are performing a chroot install, we must download and install to $GNX_PATH and then chroot to configure

	#determine if we are attempting to install Guinnux from a host different to the target (which results in configuration on first boot)
	hostMachine=`uname`
	if [ "`uname -m`" == "x86_64" ]; then
		#we are attempting a PC to SD card install, we must setup up Guinnux to configure on first boot
		echo "Error: installing Guinnux from a PC is not currently supported...aborting" 1>&2
		exit 1
	else
		#we are doing a regular install. Download and install the packages in order, then configure in order under chroot
		for pkg in `cat $PKG_LIST`; do
			echo -e "${txtbld}${txtblue}($currPkg/$numPkgs) $pkg:${txtreset}"

			downloadPkg $GNX_PATH $pkg
			installPkg $GNX_PATH $pkg
			cleanUp

			let "currPkg += 1"
		done

		#we should be able to chroot configure after opkg-cl is installed above (allowing us to configure)
		let "currPkg = 1"
		for pkg in `cat $PKG_LIST`; do
			echo -e "${txtbld}${txtblue}($currPkg/$numPkgs) $pkg:${txtreset}"

			configurePkg $pkg $GNX_PATH
			
			let "currPkg += 1"	
		done		
							
	fi
fi
cp /etc/opkg/opkg.conf "$GNX_PATH/etc/opkg"


echo ""

# catch any dependencies that may not have been configured
echo -e "${txtund}${txtblue}Configure any dependencies that may not have been configured...${txtreset}"
chroot $GNX_PATH opkg-cl install base		#chroot should work even if it is root, if base is installed it doesn't matter
if [ "$?" == "0" ]; then
	echo "${txtbld}${txtblue}[Successful]${txtreset}"
else
	echo "${txtbld}${txtred}Error: some dependencies may have failed${txtreset}" 2>&1
fi


exit 0
