#!/bin/sh

# update.sh

. "$(dirname "$0")"/prmsg.sh
get_opts "$1"

# TODO begin remove me

echo "nach get_opts arg 1 is $1"
echo "nach get_opts prt=${prt}"

# TODO end remove me

d=$(dirname "$0")
cd $d

# alle weiteren skripte setzen voraus, dass das aktuelle Verzeichnis das ist, in dem update.sh liegt

if ! intern/check_requirements.sh
then
	exit 1
fi

########################################
# calculate next mtd nums

boot_switch=$(fw_printenv -n my_boot_switch)
echo "boot_switch=${boot_switch}"
next_boot_switch=$((1 - ${boot_switch}))
echo "next_boot_switch=${next_boot_switch}"
next_kernel_num=$(fw_printenv -n my_kernel${next_boot_switch}_num)
next_rootfs_num=$(fw_printenv -n my_rootfs${next_boot_switch}_num)

if [ "$boot_switch" != 0 -a "$boot_switch" != 1 ]
then
	echo "ERROR: Unerwarteter Wert in U-Boot-Umgebungsvariable my_boot_switch: $boot_switch"
	displayMsg "Update error!" "my_boot_switch: $boot_switch"
	exit 1
fi

if [ "$next_boot_switch" != 0 -a "$next_boot_switch" != 1 ]
then
	echo "ERROR: Unerwarteter berechneter neuer Wert fuer my_boot_switch: $next_boot_switch"
	displayMsg "Update error!" "my_boot_switch: $next_boot_switch"
	exit 1
fi

if [ "$next_kernel_num" != "$(fw_printenv -n my_kernel0_num)" -a "$next_kernel_num" != "$(fw_printenv -n my_kernel1_num)" ]
then
	echo "ERROR: Unerwarteter berechneter Wert fuer naechste kernel-Partition: $next_kernel_num"
	displayMsg "Update error!" "kernel-part.: $next_kernel_num"
	exit 1
fi


if [ "$next_rootfs_num" != "$(fw_printenv -n my_rootfs0_num)" -a "$next_rootfs_num" != "$(fw_printenv -n my_rootfs1_num)" ]
then
	echo "ERROR: Unerwarteter berechneter Wert fuer naechste rootfs-Partition: $next_rootfs_num"
	displayMsg "Update error!" "rootfs-part.: $next_rootfs_num"
	exit 1
fi

##################################################
mkdir -p /data/update


##################################################
# Update rootfs, kernel, and bootloader parallelly

./rootfs_fork.sh $next_rootfs_num $boot_switch $next_boot_switch &
pid_rootfs=$!
./kernel_fork.sh $next_kernel_num $boot_switch $next_boot_switch &
pid_kernel=$!
./bootloader_fork.sh $boot_switch $next_boot_switch
status_bootloader=$?


wait $pid_kernel
status_kernel=$?

wait $pid_rootfs
status_rootfs=$?

if [ $status_rootfs != 0 -o $status_kernel != 0 -o $status_bootloader != 0 ]
then
	displayMsg "Update error!" "See log"
	echo "Breche mit Fehlercode ab: status_rootfs: $status_rootfs"
	echo "status_kernel: $status_kernel; status_bootloader: $status_bootloader"
	exit 1
fi

if ! ./mount_inactive_rootfs.sh
then
	exit 1
fi

intern/set_version.sh /mnt/inactive_rootfs rootfs $(cat version_rootfs.txt)
intern/set_version.sh /mnt/inactive_rootfs kernel $(cat version_kernel.txt)

#bootloader version nicht im rootfs, sondern in /data/update/version_bootloader.txt, da diese bei fallback NICHT zurueckgeaendert werden soll, weil es fuer den bootloader kein fallback gibt!
cp version_bootloader.txt /data/update/version_bootloader.txt

./update_env.sh&
pid_env=$!
/scripts/eeprom_bootcount_set.sh 0
status_bootcount=$?

wait $pid_env
status_env=$?

if [ $status_bootcount != 0 -o $status_env != 0 ]
then
	echo "Breche mit Fehlercode ab"
	displayMsg "Update error!" "See log"
	exit 1
fi

if [ -f setbootmsg2display.sh ]
then
    sh setbootmsg2display.sh
fi

rm -rf /data/update/tmp

printf "\n\n** UPDATE ERFOLGREICH! Bitte anschließenden reboot nicht vergessen!\n\n"
displayMsg "Update successful!" "Please restart"
