#!/bin/sh
# Switch to a chosen USB composition.

COMP_DIR="/usr/bin/usb/compositions"
symlink_hsusb="/usr/bin/usb/boot_hsusb_composition"
symlink_hsic="/usr/bin/usb/boot_hsic_composition"

legal_composition() {
	for c in $( ls $COMP_DIR ); do
		if [ "$1" = "$c" ]; then
			echo "1"
			exit
		fi
	done
	echo "0"
}

read_pid() {
	local tmp_pid='0'
	read -p "Pid number : " tmp_pid
	while [ true ]; do
		if [ `legal_composition $tmp_pid` = "1" ]; then
			echo "$tmp_pid"
			exit
		fi
		read -p "Illegal Pid number, try again : " tmp_pid
	done
}

read_hsic() {
	local tmp_hsic='0'
	read -p "Choose core: y - hsic, n - hsusb  ? (y/n)" tmp_hsic
	while [ true ]; do
		if [ $tmp_hsic = "y" ] || [ $tmp_hsic = "n" ]; then
			echo "$tmp_hsic"
			exit
		fi
		read -p "Only 'y' or 'n' are allowed, try again : " tmp_hsic
	done
}

read_presistent() {
	local tmp_hsic='0'
	read -p "Would you like it to be the default composition ? (y/n)" tmp_hsic
	while [ true ]; do
		if [ $tmp_hsic = "y" ] || [ $tmp_hsic = "n" ]; then
			echo "$tmp_hsic"
			exit
		fi
		read -p "Only 'y' or 'n' are allowed, try again : " tmp_hsic
	done
}

common_functions() {
	if [ "$1" = "empty" ] || [ "$2" = "empty" ]; then
		echo "0"
		exit
	fi
	func_list_1=`grep functions $COMP_DIR/$1 | sed 's/echo //' | sed 's/>.*$//' | tr , '\n' | tr : '\n'`
	func_list_2=`grep functions $COMP_DIR/$2 | sed 's/echo //' | sed 's/>.*$//' | tr , '\n' | tr : '\n'`
	for func_1 in $func_list_1
	do
		for func_2 in $func_list_2
		do
		if [ "$func_1" = "$func_2" ]; then
			echo $func_1
			exit
		fi
		done
	done
	echo "0"
}

hsusb_comp=`readlink $symlink_hsusb | xargs basename`
hsic_comp=`readlink $symlink_hsic | xargs basename`

if [ "$#" -ge 4 ]; then
	echo "Usage: usb_composition [Pid] [HSIC] [PERSISTENT]" >&2
	exit

elif [ "$#" -eq 3 ]; then

	if [ `legal_composition $1` = "0" ]; then
		echo "Illegal pid"
		exit
	fi
	if [ $2 != "y" ] && [ $2 != "n" ]; then
		echo "Illegal hsic choice (must be 'y' or 'n')."
		exit
	fi
	if [ $3 != "y" ] && [ $3 != "n" ]; then
		echo "Illegal presistent choice (must be 'y' or 'n')."
		exit
	fi
	pid=$1
	hsic=$2
	presistent=$3

elif [ "$#" -eq 2 ]; then

	if [ `legal_composition $1` = "0" ]; then
		echo "Illegal pid"
		exit
	fi
	if [ $2 != "y" ] && [ $2 != "n" ]; then
		echo "Illegal hsic choice (must be 'y' or 'n')."
		exit
	fi
	pid=$1
	hsic=$2
	presistent="n"

elif [ "$#" -eq 1 ]; then

	if [ `legal_composition $1` = "0" ]; then
		echo "Illegal pid"
		exit
	fi
	pid=$1
	hsic="n"
	presistent="n"

elif [ "$#" -eq 0 ]; then
	echo "boot hsusb composition: $hsusb_comp"
	echo "boot hsic composition: $hsic_comp"
	echo "Choose Composition by Pid:"
	for i in $( ls $COMP_DIR ); do
		desc=`grep DESCRIPTION: $COMP_DIR/$i | sed 's/.*DESCRIPTION: *//g'`
		echo "   $i -	$desc"
	done
	pid=`read_pid`
	hsic=`read_hsic`
        presistent=`read_presistent`


fi

if [ $hsic = "y" ]; then
	other=$hsusb_comp
else
	other=$hsic_comp
fi

common=`common_functions $other $pid`
if [ $common != "0" ]; then
	echo "Can't change composition, hsusb and hsic cant use the same usb function : $common"
	exit
fi

if [ $presistent = "y" ]; then
	if [ $hsic = "y" ]; then
		rm $symlink_hsic
		ln -s $COMP_DIR/$pid $symlink_hsic
	else
		rm $symlink_hsusb
		ln -s $COMP_DIR/$pid $symlink_hsusb
	fi
fi

if [ $hsic = "n" ]; then
	$COMP_DIR/$pid n
fi

