#!/bin/sh

PRO="PRODUCT = "
SN="sn = "
FullPath=/app/config/fac.ini
#FullPath=/tmp/fac.ini

case "$1" in
        set)action="$1";;
        get)action="$1";;
        *) exit 1;;
esac
shift 1

while [ -n "$1" ]; do
        case $1 in
                -sn) sn="$2";shift 2;;
                -product) product="$2";shift 2;;
                --) shift;break;;
                -*) help;exit 1;;
                *) break;;
        esac
done

set_product()
{
    echo "set PRODUCT to $1"
	line=`sed -n '/'"$PRO"'/=' $FullPath`
	if [ "$line" == "" ]; then
		#echo "is Null"
		sed -i "2a $PRO$1" $FullPath
	else
		#echo $line
		sed -i "${line}c $PRO$1" $FullPath
	fi
}

set_sn()
{
    echo "set SN to $1"
	line=`sed -n '/'"$SN"'/=' $FullPath`
	if [ "$line" == "" ]; then
		#echo "is Null"
		sed -i "6a $SN$1" $FullPath
	else
		#echo $line
		sed -i "${line}c $SN$1" $FullPath
	fi
}

#cmd=$1	
#echo "$action"
if [ "$action" == "get" ]; then
	cat $FullPath
elif [ "$action" == "set" ]; then
	if [ -n "$sn" ]
	then
		set_sn $sn
	fi
	
	if [ -n "$product" ]
	then
		set_product $product
	fi
else 
	echo "Wrong Action"
fi

#cat $FullPath
