#!/bin/sh
# 
# $Id: faum.in,v 1.11 2011-02-04 15:26:42 potyra Exp $
#
# Copyright (C) 2009 FAUmachine Team <info@faumachine.org>.
# This program is free software. You can redistribute it and/or modify it
# under the terms of the GNU General Public License, either version 2 of
# the License, or (at your option) any later version. See COPYING.

set -e

PREFIX=/usr

check() {
	if [ -z "$HOME" ]; then
		echo "HOME is unset. Exiting."
		exit 1;
	fi

	if [ -z $(which zenity) ]; then
		echo "zenity not found. Exiting."
		exit 1;
	fi

	if [ -z $(which $PREFIX/bin/fauhdlc) ]; then
		echo "$PREFIX/bin/fauhdlc not found. Exiting."
		exit 1;
	fi
}

create_faumrc() {
	echo "IMAGE_PATH=\"$IMAGE_PATH\"" > "$HOME/.faumrc"
	echo "MEDIA_SIZE=$MEDIA_SIZE" >> "$HOME/.faumrc"
	echo "RAM_SIZE=$RAM_SIZE" >> "$HOME/.faumrc"
	echo "SIMULATION_PC=\"$SIMULATION_PC\"" >> "$HOME/.faumrc"
}


ask_directory() {
	# $1 -> title
	RET=$(zenity --title="$1" --file-selection --directory)
}

ask_text() {
	# $1 -> title
	# $2 -> question text
	RET=$(zenity  --title "$1" --entry --text "$2")
}

ask_list() {
	# $1 -> title
	# $2 -> info text
	# $3 -> column headers
	# $4... -> options
	TITLE=$1
	shift
	TEXT=$1
	shift
	COLHEADER=$1
	shift
	RET=$(zenity --title "$TITLE" --text="$TEXT" --list \
		--column="$COLHEADER" $*)
}

msg_error() {
	# $1 -> title
	# $2 -> error message
	zenity --title="$1" --error --text="$2"
}

check

if [ ! -e "$HOME/.faumrc" ]; then
	CREATE_FAUMRC=1
else
	. $HOME/.faumrc
	CREATE_FAUMRC=0
fi

if [ -z "$IMAGE_PATH" ]; then
	ask_directory "In which path should virtual machines get stored?"
	IMAGE_PATH=$RET
	CREATE_FAUMRC=1
	set +e
	touch "$IMAGE_PATH/testfile"
	if [ $? -eq 1 ]; then
		msg_error "Configuring faum" \
			"\"$IMAGE_PATH\" is not writable. Aborting."
		exit 1
	fi
	set -e
	rm "$IMAGE_PATH/testfile"
fi

if [ -z "$SIMULATION_PC" ]; then
	ask_list "Configuring faum: Which PC to simulate" \
		"Select which PC to simulate" \
		"Architecture" \
		"i386" "amd64"

	case "$RET" in
		"i386")
			SIMULATION_PC="pc-4.vhdl"
			;;
		"amd64")
			SIMULATION_PC="pc-amd64-pci.vhdl"
			;;
		*)
			msg_error "Error" "Unsupported choice, exiting"
			exit 1;
		;;
	esac
	CREATE_FAUMRC=1
fi
			

if [ -z "$MEDIA_SIZE" ]; then
	ask_text "Configuring faum: Disk size" \
		"Size of the virtual disk in Megabytes"
	MEDIA_SIZE=$RET
	CREATE_FAUMRC=1
fi

if [ -z "$RAM_SIZE" ]; then
	ask_list "Configuring faum: Memory size" \
		"Memory size of the virtual RAM (Mb)" \
		"Memory size" \
		"64" "128" "256" 
	RAM_SIZE=$RET
	CREATE_FAUMRC=1
fi

if [ $CREATE_FAUMRC -eq "1" ]; then
	create_faumrc
fi

if [ ! -d "$IMAGE_PATH" ]; then
	msg_error "Error" "Node path $IMAGE_PATH does not exist"
	exit 1
fi

# sed expression to change disk size
CDS_SED="/disksize : integer := [0-9]\+/disksize : integer :="
# same for memsize
CMS_SED="/memsize : integer := [0-9]\+/memsize : integer :="

FAUM_DATA=$PREFIX/share/faumachine/vhdl
PC_VHDL="$FAUM_DATA/$SIMULATION_PC"
HW_TB_VHDL=$FAUM_DATA/test_bench_network.vhdl
HW_UB_VHDL=$FAUM_DATA/user-begin.vhdl
HW_UE_VHDL=$FAUM_DATA/user-end.vhdl

# check if slirp binary is present
HAVE_SLIRP=1
which slirp > /dev/null 2>&1 || HAVE_SLIRP=0

if [ $HAVE_SLIRP -eq 0 ]; then
	msg_error "Warning" "Slirp not installed, continuing without network."
	HW_TB_VHDL=$FAUM_DATA/test_bench.vhdl
fi

cat "$PC_VHDL" | \
	sed -e s"$CDS_SED $MEDIA_SIZE/" \
	    -e s"$CMS_SED $RAM_SIZE/" > \
	    "$IMAGE_PATH/system.vhdl"

cat "$HW_UB_VHDL" >> "$IMAGE_PATH/system.vhdl"

cat "$HW_UE_VHDL" |\
	sed -e "s/terminate;/wait;/" \
		>> "$IMAGE_PATH/system.vhdl"

cat "$HW_TB_VHDL" >> "$IMAGE_PATH/system.vhdl"

$PREFIX/bin/fauhdlc \
	--lib expect "$FAUM_DATA/sigs_and_comps.vhdl" \
	--lib expect "$FAUM_DATA/expect.vhdl" \
	--lib expect "$IMAGE_PATH/system.vhdl" \
	-o "$IMAGE_PATH/system.ic"

cp "$FAUM_DATA/simulation.setup" "$IMAGE_PATH"
# enable interactive mode
echo 'interactive     1' >> "$IMAGE_PATH/simulation.setup"

cd "$IMAGE_PATH"

$PREFIX/bin/faum-node-pc
