#!/usr/bin/bash
#
# Copyright (C) 2007 Joaquim Boura <x-un-i@berlios.de>
# heavily based on the knoppix installer from
#         Fabian Franz <knoppix-installer@fabian-franz.de>
#
# 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 2
# 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 package; if not, see <http://www.gnu.org/licenses>
#
# On Debian GNU/Linux systems, the text of the GPL license can be
# found in /usr/share/common-licenses/GPL.
#
#--------------------------------------------------------------------------
ERRLOG=/tmp/.installer.error
DEBUGLOG=/tmp/.installer.debug
STARTTIME=$(date +%s)
[ -e "$ERRLOG" ] && rm -f "$ERRLOG"
[ -e "$DEBUGLOG" ] && rm -f "$DEBUGLOG"

# redirect STDERR to debug log
exec 2> "${DEBUGLOG}"

# In case UID is not set, set and use it
if [ -z "${UID}" ]; then
	UID=$(id -ur)
fi

if [ -n "$INSTDBG" ]; then
	set -x
fi
#--------------------------------------------------------------
# one place to save the error code and message
#--------------------------------------------------------------
error() {
	local exitval="$1"
	shift

	echo "E: $*"
	clean_exit "$exitval"
}
#--------------------------------------------------------------
# sayit
#--------------------------------------------------------------
sayit() {
        echo "O: $1"
}
#--------------------------------------------------------------
# usage
#--------------------------------------------------------------
usage() {
	echo "$0 [-d|--debug] [-h|--help]"
	exit 1
}
#--------------------------------------------------------------
# preparation, process cli arguments with getopt
#--------------------------------------------------------------
if ! TEMP=$(getopt -o di:h --long debug,inotify:,help \
		-n  "$(basename "$0")" -- "$@"); then
	error 255 "getopt terminated abnormally"
fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"


while true ; do
	case "$1" in
		-d|--debug)
			set -x
			shift
			;;
		-h|--help)
			usage
			exit 0
			;;
		*)
			break
			;;
	esac
done

#--------------------------------------------------------------
#
# Include needed Modules
#--------------------------------------------------------------

SEARCHPATH="/usr/share/fll-installer"
sayit "loading modules"
for i in $(find "$SEARCHPATH/modules" -name '*.bm'); do
	. "$i"
done
#------------------

# trap clean_exit EXIT

TMPDIR="$(mktemp -p /tmp -d fll-installer.XXXXXX)"

#------------------

main
# we should never arrive here otherwise

# something did go wrong
error 254 "internal error"


#------------------

