#!/bin/sh

# Date: 2010-04-14
# CR: crms00225116
# Author: Damien Couderc
# Label: access control of fab partition

# Date: 2010-06-30
# CR: crms00241837
# Author: Damien Couderc
# Label: display embedded status


# Date: 2010-12-23
# CR: crms00282384
# Author: Claire Dechrist
# Label: reset admin password

#set -x

# crms00241837 BEGIN
# save program name
PROGNAME=`basename "$0"`
# crms00282384
PROGNAME=${PROGNAME#-}
# crms00241837 END

# the following variables are borrowed from usb_[un]mount_key.sh scripts
MOUNT_POINT="/mnt/removable/usb_disk"
mountLockFile="/tmp/.mountedFlash"

# usb init service
USB_INIT='/etc/init.d/usb'
# usb scanning timeout
USB_TIMEOUT=10

# path of the file that contains verification data
VERSLIST="$MOUNT_POINT/boot2fab.lst"

# path of the files that contain the token and the version to verify
TOKENFILE='/etc/boot2fab.token'
VERSFILE='/etc/boot2fab.version'

# get version number
VERSION=`cat $VERSFILE`

# get verification token
TOKEN=`cat $TOKENFILE`


# verify if usb service is started
service=`$USB_INIT status`
if [ "$service" != "started" ]; then
	# start the service
	printf "Starting USB service ...\n"
	$USB_INIT start >/dev/null 2>&1

	# loop until USB_TIMEOUT is reached
	printf "Waiting for USB key mounting ...\n"
	timeout=$USB_TIMEOUT
	while [ $timeout -gt 0 ]; do
		# wait one second
		sleep 1

		# check for mount lock
		if [ -f "$mountLockFile" ]; then
			# lock has been found, we leave the timeout loop
			printf "USB key lock file has been found.\n"
			break
		fi

		# decrementing the timer
		timeout=`expr $timeout - 1`
		printf "$timeout seconds remaining ...\n"
	done
fi

# check for mounted usb key
if [ ! -f "$mountLockFile" ]; then
	printf "USB key missing.\n" >&2
	# crms00241837 BEGIN
	printf "%s KO\n" "$PROGNAME"
	# crms00241837 END
	exit 1
fi

# check for list file
if [ ! -f "$VERSLIST" ]; then
	printf "USB key verification file is missing.\n" >&2
	# crms00241837 BEGIN
	printf "%s KO\n" "$PROGNAME"
	# crms00241837 END
	exit 1
fi

# get the appropriate token from the list
token=`grep $VERSION $VERSLIST | cut -d '=' -f 2 | tr -d '\n'`

# check token
if [ -z "$token" ]; then
	printf "USB key verification file must be updated.\n" >&2
	# crms00241837 BEGIN
	printf "%s KO\n" "$PROGNAME"
	# crms00241837 END
	exit 1
fi

# compare tokens
if [ "$TOKEN" != "$token" ]; then
	printf "Verification does not match.\n" >&2
	# crms00241837 BEGIN
	printf "%s KO\n" "$PROGNAME"
	# crms00241837 END
	exit 1
fi

#crms00282384
/usr/sbin/chpasswd_admin.sh  __RESET_TO_DEFAULT__


# now set the u-boot boot partition to fab
# NOTE: carriage return is needed else boot fails
printf "root=FAB\n" > /config/u-boot/u-boot.cfg
if [ $? -ne 0 ]; then
	printf "Failed to activate fab partition.\n" >&2
	# crms00241837 BEGIN
	printf "%s KO\n" "$PROGNAME"
	# crms00241837 END
	exit 1
fi

# and reboot
printf "Rebooting into fab.\n"
# crms00241837 BEGIN
printf "%s OK\n" "$PROGNAME"
# crms00241837 END
reboot --owner boot2fab --reason "switching to FAB"

# vim: set noexpandtab tabstop=4 softtabstop=4 shiftwidth=4:
