#!/bin/bash

########################################################################
# MuGLIn - MuGLIn GNU/Linux Installation		                       #
#                                                                      #
# Copyright (C) 2010 Jakob Gurnhofer <jakob.gurnhofer@gmail.com>       #
# Copyricht (C) 2010 Srdjan Markovic <smark2ki@htl.moedling.at>        #
#                                                                      #
# This file is part of MuGLIn source code.                             #
#                                                                      #
# MuGLIn 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.                                  #
#                                                                      #
# MuGLIn 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 MuGLIn. If not, see <http://www.gnu.org/licenses/>.       #
########################################################################

# All messages should go to stderr because stdout is used to get the iif-tmpfile

. /etc/muglin/base.conf

IMSDIR="/usr/local/lib/muglin/image/"
PRESET_DIR="$IMSDIR/os_scripts/debootstrap_presets"
BLDIR="$IMSDIR/bootldr/"

if [ ! -x "/usr/sbin/debootstrap" ]; then
      echo "Debootstrap is missing. Aborting." >&2
      exit
fi

checkinput(){
topic=$1
shift
if [[ "$(echo $@ | egrep '^-' )" != "" ]]; then
			echo "$topic should not start with an \"-\", maybe forgotten argument?" >&2
			exit -1
fi
}
until [[ "$1" == "" ]]; do
	case $1 in
	--dir)
		shift
		if [[ ! -d $1 ]]; then
			mkdir -p $1
		fi
		DIR=$1		
	;;
	--addinclude)
		shift
		checkinput "Includes" "$1"
		INCLUDE="$INCLUDE,$1"
	;;
	--replinclude)
		shift
		checkinput "Includes" "$1"
		INCLUDE="$1"
	;;
	--arch)
		shift
		if [[ "$(echo "i386 i686 x86_64" | grep "$1")" != "" ]]; then
			echo "Unsupported architecture" >&2
			exit -1
		fi
		ARCH="$1"
	;;
	--version)
		shift
		checkinput "Version" "$1"
		VERSION="$1"
	;;
	--mirror)
		shift
		checkinput "Mirror" "$1"
		MIRROR="$1"
	;;
	--preset)
		shift
		if [[ ! -e "$PRESET_DIR/$1" ]]; then
			echo "Preset \"$1\" does not exist. Aborting" >&2
			exit -1
		fi
		. $PRESET_DIR/$1
	;;
	--help|-h)
		echo "Installs a Debian based system using debootstrap." >&2
		echo "Options:" >&2
		echo -e "\t--mirror" >&2
		echo -e "\t--preset" >&2
		echo -e "\t--version" >&2
		echo -e "\t--arch" >&2
		echo -e "\t--addinclude" >&2
		echo -e "\t--replinclude" >&2
		echo -e "\t--help" >&2
		echo "If you want to use a preset, please provide it first and use the other options to override some parameters" >&2
		exit 1
	;;
	esac
	shift
done
	
# debootstraps the standard system
mkdir $DIR/etc
echo do_initrd = yes > $DIR/etc/kernel-img.conf
debootstrap --arch="$ARCH" --include="$INCLUDE" $VERSION $DIR "$MIRROR" >/dev/null
if [[ "$?" != "0" ]];then
	echo "Debootstrap failed. Aborting!"  >&2
	exit -1
fi
TMP=$(mktemp)
case "$ARCH" in
  i386|i486|i586|i686) echo "Arch=x86" >> $TMP;;
  x86_64|amd64) echo "Arch=x64" >> $TMP;;
esac

# and prepares the bootloader stuff
mkdir $DIR/bootldr
cp "$BLDIR/grub" "$DIR/bootldr/install"

echo "Type=1" >> $TMP
echo "Boot=GRB" >> $TMP
echo "DEFPOST=\"$DEFPOST\"" >> $TMP
echo $TMP
