#!/bin/sh

# 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)

# record the exit code if we need to clean up
EXIT_CODE="0"

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

#Get the disk layout and setup from the command line or use the defaults
for i in $(cat /proc/cmdline); do
	case $i in
		root\=*)
			root="`echo $i | awk -F"root=" '{print $2}' | awk -F" " '{print $1}'`"
			;;
		root_type\=*)
			root_type="-t `echo $i | awk -F"root_type=" '{print $2}' | awk -F" " '{print $1}'`"
			FS_TYPE="`echo $i | awk -F"root_type=" '{print $2}' | awk -F" " '{print $1}'`"
			;;
		root_opts\=*)
			root_opts="-o `echo $i | awk -F"root_opts=" '{print $2}' | awk -F" " '{print $1}'`"
			;;
		init\=*)
			init="`echo $i | awk -F"init=" '{print $2}' | awk -F" " '{print $1}'`"
			;;
        rescue\=*)
            rescue="`echo $i | awk -F"rescue=" '{print $2}' | awk -F" " '{print $1}'`"
            ;;
        rescue_type\=*)
            rescue_type="-t `echo $i | awk -F"rescue_type=" '{print $2}' | awk -F" " '{print $1}'`"
            ;;
        rescue_opts\=*)
            rescue_opts="-o `echo $i | awk -F"rescue_opts=" '{print $2}' | awk -F" " '{print $1}'`"
            ;;
        boot\=*)
            boot="`echo $i | awk -F"boot=" '{print $2}' | awk -F" " '{print $1}'`"
            ;;
        boot_type\=*)
            boot_type="-t `echo $i | awk -F"boot_type=" '{print $2}' | awk -F" " '{print $1}'`"
            ;;
        boot_opts\=*)
            boot_opts="-o `echo $i | awk -F"boot_opts=" '{print $2}' | awk -F" " '{print $1}'`"
            ;;
        bootrescue*)
            bootrescue="yes"
            ;;    
	esac
done


if [ -z "$root" ]; then root="/dev/mmcblk0p1"; fi
if [ -z "$root_type" ]; then root_type="-t ext4"; FS_TYPE="ext4"; fi
if [ -z "$root_opts" ]; then root_opts="-o data=journal"; fi
if [ -z "$init" ]; then init="/sbin/init"; fi
if [ -z "$rescue" ]; then rescue="/dev/mtdblock1"; fi
if [ -z "$rescue_type" ]; then rescue_type="-t jffs2"; fi
if [ -z "$rescue_opts" ]; then rescue_opts="-o ro"; fi
if [ ! -z "$boot" ]; then
	if [ -z "$boot_type" ]; then boot_type="-t jffs2"; fi
	if [ -z "$boot_opts" ]; then boot_opts="-o rw"; fi
fi

mkdir /mnt/gnx -p


echo "Starting Guinnux installer..."
echo    "${txtbld}${txtblue}######################   Guinnux Installer  ########################${txtreset}"
echo    ""
echo    "Welcome to the Guinnux installer. The installer will install\
 the base Guinnux operating system on the SD card.\
 NOTE: This will erase all content on the SD card!" | fold -s

echo    "The installer will now proceed. Before the main setup begins,\
 please confirm the following installation specifications." | fold -s
echo ""

echo "Using the following parameters:"
echo " root = '$root'"
echo " root_type = '$root_type'"
echo " FSTYPE = '$FS_TYPE'"
echo " root_opts = '$root_opts'"
if [ ! -z $boot ]; then
echo " boot = '$boot'"
echo " boot_type = '$boot_type'"
echo " boot_opts = '$boot_opts'"
fi

PART=$root
if [ -z $DISK ]; then
	DISK="/dev/mmcblk0"
fi

if [ -e $root ]; then
    echo "The root partition already exists. Will just format the filesystem."
else
	echo "The root partion does not exist. Will have to create a new partition table"
	# state some more details about the partition setup
	echo -e "${txtund}${txtblue}Current partition table (will be lost on install!):${txtreset}"
	/sbin/fdisk -l $DISK | grep -v '^$' | fold -s
	
	echo ""
	
	# prompt for if we must continue
	while true; do
	        read -p "Do you want to overwrite the partition table? (${txtbld}y${txtreset}es / ${txtbld}n${txtreset}o / ${txtbld}a${txtreset}bort) : " choice
	        case $choice in
	                [y]* )
	                        echo -e "Partition table will be ${txtbld}overwritten${txtreset} when setup begins.\n" ;
	                        SAVE=0;
	                        break;;
	                [n]* )
	                        echo -e "Partition table will be ${txtbld}preserved${txtreset} when setup begins...\n" ;
	                        SAVE=1;
	                        break;;
	                [a]* )
	                        echo -e "Aborting...\n" ;
	                        exit 0;;
	        esac
	done
	
	if [ "$SAVE" == 0 ]; then
	    echo "${txtbld}${txtblue}Creating new empty DOS partition table on $DISK...${txtreset}"
	    fdisk $DISK > /dev/null 2>&1 << EOF
o
v
w
EOF
	
	    echo "${txtbld}${txtblue}Creating new partition on $DISK...${txtreset}"
	    fdisk -u $DISK > /dev/null 2>&1 << EOF
n
p
1
2048

v
w
EOF
	
	else
	    echo "${txtbld}${txtblue}Partition table on $DISK will not be changed${txtreset}..."
	fi
fi


echo "${txtbld}${txtblue}Creating $FS_TYPE filesystem on $PART...${txtreset}"
mkfs.$FS_TYPE $PART
if [ "$?" != "0" ]; then
        echo "Error: Failed to format the Guinnux root with \"$FS_TYPE\"" 1>&2
        exit "$?"
fi

echo ""

# establish GNX_PATH
if [ -z "$GNX_PATH" ]; then
	GNX_PATH="/mnt/gnx"
fi

if [ -z "$DISK" ]; then
        DISK="/dev/mmcblk0"
fi
if [ -z "$PART" ]; then
	PART="/dev/mmcblk0p1"
fi

echo "Using the following mount options:"
echo " GNX_PATH = '$GNX_PATH'"
echo " DISK = '$DISK'"
echo " PART = '$PART'"

#open gnx root up for editing
if [ "$GNX_PATH" == "/" ]; then
	# we are running from the guinnux root so don't do any special mounting/unmounting
	echo ""
else
    #mount $PART on $GNX_PATH and bind device files
    mkdir -p "$GNX_PATH" 1>/dev/null
    mountpoint -q "$GNX_PATH" 1>/dev/null
    if [ "$?" == "0" ]; then
	   echo "Error: $GNX_PATH is already a mountpoint, cannot continue" 1>&2
	   exit 1
    fi
    ls -l $PART
    mount $root_type $root_opts $PART $GNX_PATH 1>/dev/null
    if [ "$?" != "0" ]; then
	   echo "Error: Failed to mount $PART on $GNX_PATH" 1>&2
	 	exit 1	 
    fi
    if [ ! -z $boot ]; then
    	mkdir -p $GNX_PATH/boot
    	mount $boot_type $boot_opts $boot $GNX_PATH/boot
    	if [ "$?" != "0" ]; then
	 	echo "Error: Boot partition Mount error $?" 1>&2 
	    exit 1
        fi
    fi
    mkdir -p $GNX_PATH/dev
    mkdir -p $GNX_PATH/proc
    mkdir -p $GNX_PATH/sys
    
    mount --bind /dev $GNX_PATH/dev
    mount --bind /proc $GNX_PATH/proc
    mount --bind /sys $GNX_PATH/sys
    if [ "$?" != "0" ]; then
        echo "Error: Failed to bind device files to the Guinnux root" 1>&2
    fi
fi

echo "${txtbld}${txtblue}Updating the Guinnux Installer${txtreset}"
pacman -Sy -dd --force --noconfirm gnx-installer

export GNX_PATH
exec /usr/bin/gnx-install 

