#!/bin/bash

echo_usage()
{
echo "Usage: glis installstep /path/to/config/file"
echo "Install Steps:"
echo "    1 -- Network Setup"
echo "    2 -- Partitioning"
echo "    3 -- Unpack Tarball"
echo "    4 -- Prepare System"
echo "    5 -- Get Portage Tree"
echo "    6 -- Bootstrap"
echo "    7 -- Emerge System"
echo "    8 -- Build Kernel"
echo "    9 -- Emerge Utilites"
echo "   10 -- Implement Configuration"
echo "   11 -- Emerge Misc."
echo "  ALL -- Perform all steps"
echo "Config File:"
echo "  The config file MUST exist"
}

# Make sure the number of arguements is correct
if [ $# -ne 2 ]; then
   echo_usage
   exit -255
fi

# If the config file exists, then source it
# Also, source the fuctions we will use
if [ -f "$2" ]; then
   export GLIS_CONFIG="$2"
   source ${GLIS_CONFIG}
   source functions/000-write_config.sh
   source functions/010-network_pre.sh
   source functions/020-partition_system.sh
   source functions/030-unpack_tarball.sh
   source functions/040-prepare_system.sh
   source functions/050-portage_tree.sh
   source functions/080-build_kernel.sh
   source functions/090-emerge_utilities.sh
   source functions/100-implement_config.sh

   # If install stage is not set, or is out of range, set to stage 1
   if [ "${INSTALL_STAGE}" == "" ] || [ ${INSTALL_STAGE} -lt 1 ] || [ ${INSTALL_STAGE} -gt 3 ]; then
      INSTALL_STAGE=1
      write_config "INSTALL_STAGE" "INSTALL_STAGE=1" "${GLIS_CONFIG}" "="
   fi

# Else, give usage
else
   echo_usage
   exit -255
fi

# Export relevant needed variables
[ "${PORTAGE_TMPDIR}" != "" ] && export PORTAGE_TMPDIR
[ "${HTTP_PROXY_PRE}" != "" ] && export http_proxy=${HTTP_PROXY_PRE}
[ "${FTP_PROXY_PRE}" != "" ] && export ftp_proxy=${FTP_PROXY_PRE}
[ "${RSYNC_PROXY_PRE}" != "" ] && export RSYNC_PROXY=${RSYNC_PROXY_PRE}

# Process the arguements
# If invalid arguement, give usage
case "$1" in
     1) network_pre || exit ${1};;
	 
     2) partition_system || exit ${1};;
	 
     3) unpack_tarball || exit ${1};;
	 
     4) prepare_system || exit ${1};;
	 
     5) portage_tree || exit ${1};;
	 
     6) if [ ${INSTALL_STAGE} -eq 1 ]; then
          chroot /mnt/gentoo /usr/portage/scripts/bootstrap.sh || exit ${1}
	    fi;;
		
     7) if [ ${INSTALL_STAGE} -eq 1 ]; then
          chroot /mnt/gentoo emerge ${EMERGE_OPTIONS} system || exit ${1}
	    fi;;
		
     8) build_kernel || exit ${1};;
	 
     9) emerge_utilities || exit ${1};;
	 
    10) implement_config || exit ${1};;
	
    11) if [ "${MISC_PROGRAMS}" != "" ]; then
           chroot /mnt/gentoo emerge ${EMERGE_OPTIONS} ${MISC_PROGRAMS} || exit ${1}
        fi;;
		
   ALL) network_pre || exit 1
        partition_system || exit 2
        unpack_tarball || exit 3
        prepare_system || exit 4
        portage_tree || exit 5
        if [ ${INSTALL_STAGE} -eq 1 ]; then
          chroot /mnt/gentoo /usr/portage/scripts/bootstrap.sh || exit 6
	    fi
        if [ ${INSTALL_STAGE} -eq 1 ]; then
          chroot /mnt/gentoo emerge ${EMERGE_OPTIONS} system || exit 7
	    fi
        build_kernel || exit 8
        emerge_utilities || exit 9
        implement_config || exit 10
        if [ "${MISC_PROGRAMS}" != "" ]; then
           chroot /mnt/gentoo emerge ${EMERGE_OPTIONS} ${MISC_PROGRAMS} || exit 11
        fi;;
        
     *) echo_usage
        exit -255;;
esac
