#!/bin/bash -l
DIALOG=${DIALOG=dialog}
CONFIG="/etc/bios-installer.conf"
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
tempfile2=`tempfile 2>/dev/null` || tempfile2=/tmp/test2$$
tempfiler1=`tempfile 2>/dev/null` || tempfiler1=/tmp/test2$$
tempfiler2=`tempfile 2>/dev/null` || tempfiler2=/tmp/test2$$
ARCHIVETYPE=""
trap "rm -f ${tempfile}" 0 1 2 5 15
trap "rm -f ${tempfile2}" 0 1 2 5 15
trap "rm -f ${tempfiler1}" 0 1 2 5 15
trap "rm -f ${tempfiler2}" 0 1 2 5 15

if [ "`id -u`" != "0" ] ; then
	echo "Image Installer must run as user 'root'"
	exit 1
fi

function debug {
	if [ ${DEBUG} == "1" ] ; then
	echo -e -n "$@"
	fi
}

function gen_templlist {
	CNT=1
	cd ${BASEMOUNT}
	for IMG in `find . -maxdepth 1 -type d | grep -v "^\.$" | sed -e s@"\./"@""@g | sort`
	do
	echo ${IMG} ${CNT} off
	CNT=`expr ${CNT} + 1`
	done
}

function detect_server_model {
	PRD=`dmidecode -s system-product-name`
	if [ -z "${PRD}" ] ; then
		PRD="unknown"
	fi
	echo ${PRD}
}

function prepare_setup {	
	if [ -f ${CONFIG} ]; then
		. ${CONFIG}
		if [ -n "${BASEMOUNT}" ]; then
			mkdir -p "${BASEMOUNT}"
			case ${IMAGELOCATION} in
				NFS)
					mount -t nfs ${IMAGELOCATION_NFS} ${BASEMOUNT}
					
				;;
				DIR)
					mount -o bind ${IMAGELOCATION_DIR} ${BASEMOUNT}
				;;
				*)
					echo "IMAGELOCTION: ${IMAGELOCATION} not supported"
					exit 1
				;;
			esac
		else
			echo "no base mountpoint in config"
			exit 1
		fi
	else
		echo "configuration file not found"
		exit 1
	fi
}

function umount_base {
	cd /
	umount ${BASEMOUNT}
}

function run_flash {
	cd ${BASEMOUNT}/$(cat ${tempfile2})
	if [ -x run-all ] ; then
		clear
		chvt 11
		mkdir -p /usr/libexec/dell_dup	
        	local out=`./run-all 2>&1| tee ${tempfiler1} /dev/tty11`
		chvt 12
	else
		clear
        	echo "no autorun file detected, can not flash any firmwares"
	        exit 1
	fi
	echo 0
}

function run_flash_dsu {
	clear
	chvt 11
	mkdir -p /usr/libexec/dell_dup
	local out=`dsu -n 2>&1 | tee ${tempfiler1} /dev/tty11`
	sleep 30
	reboot
	chvt 12
	echo 0
}

################### start up the stuff #############################
prepare_setup

# define autorun 
IMG=`cat /proc/cmdline | grep ix_bios_autoflash | sed -r 's/.*(ix_bios_autoflash=\S+).*/\1/'`
if [ -n "${IMG}" ] ; then
  export $(echo ${IMG})
else
  export ix_bios_autoflash=0
fi

if [ "${ix_bios_autoflash}" == "1" ] ; then
	detect_server_model | awk '{print tolower($0)}' > ${tempfile2}
	retval=`run_flash_dsu`
else
	######## Server Type SELECT ##########
	${DIALOG} --backtitle "${TITLE}" \
		--title "Server model selection" --clear \
		--radiolist "Please select server type (autodetected: `detect_server_model`): " 20 75 10 `gen_templlist` 2> ${tempfile2}
	retval=$?
	imagefile=`cat ${tempfile2}`
	case ${retval} in 
		0)
			clear
			if [ -z "`cat ${tempfile2}`" ]; then
				umount_base
				echo "no server model selected"
				exit 1
			fi
			;;
		1)
			umount_base
			echo "cancel pressed in server model selection dialog"
			exit 1
			;;
		255)
			gen_templlist
			umount_base
			echo "esc pressed in server model selection dialog"
			exit 255
			;;
	esac
	retval=`run_flash`
fi

if [ "${retval}" == "0" ] ; then
	${DIALOG} --backtitle "$TITLE" \
		--title "Flashing completed" \
		--yesno "Update completed, reboot now?" 5 32
	retval=$?
	case ${retval} in
		0)
			#yes
			umount_base
			clear && cat ${tempfiler1} && read -p "press any key to continue"
			reboot
		;;
		1)
			#no
			umount_base
			clear && cat ${tempfiler1} && read -p "press any key to continue"
		;;
	esac
else
	${DIALOG} --backtitle "$TITLE" \
		--title "Imaging may be failed" \
		--msgbox "Write produced output ($retval), exiting..." 5 28 && clear && cat ${tempfiler1} && read -p "press any key to continue"
		
fi
exit 0
