#!/bin/sh


Usage() {
	printf "\nUsage: pcs-set-params [scale_params]\n"
	printf " Scale params:\n"
	printf "  -s|--scale <scale number>\n"
	printf "  -c|--counter <scale counter>\n"
	printf "  -b|--board_group <board group>\n"
	printf "  -m|--mup_group <more used plu's group>\n"
	printf "  -l|--lang <ISO locale language. ex: en_US>\n"
	printf "\n"
	exit 1
}

SetLocalCgfScale() {
	mysql -upcscale -pepelsa pcscale -e"REPLACE INTO LocalCfgScale (ParamKey,ParamValue) VALUES('$1','$2')"
}

SetSettings() {
	gsettings set $1 $2 "$3"
}

# Execute getopt
ARGS=$(getopt -o s:c:b:m:l:h -l "scale:,counter:,board_group:,mup_group:,lang:,help" -n "pcs-set-params" -- "$@");

#Bad arguments
if [ $? -ne 0 ];
then
	Usage
	exit 1
fi

eval set -- "$ARGS";

while true; do
	case "$1" in
    	-s|--scale)
    		scale=$2
    		shift 2;
    		;;
    	-c|--counter)
    		counter=$2
    		shift 2;
    		;;
    	-b|--board_group)
    		board_group=$2
    		shift 2;
    		;;
		-m|--mup_group)
    		mup_group=$2
    		shift 2;
    		;;
    	-l|--lang)
    		lang=$2
    		shift 2;
    		;;
    	-h|--help)
    		Usage
    		;;
	    --)
    		shift;
    		break;
    		;;
	esac
done

echo "scale = $scale"
echo "counter = $counter"
echo "board_group = $board_group"
echo "mup_group = $mup_group"
echo "lang = $lang"

if [ "$scale" != "" ];then
	SetLocalCgfScale "NETWORK/scale-number" $scale
fi
if [ "$counter" != "" ];then
	SetLocalCgfScale "NETWORK/scale-counter" $counter
fi
if [ "$board_group" != "" ];then
	SetLocalCgfScale "NETWORK/plu-board-group" $board_group
fi
if [ "$mup_group" != "" ];then
	SetLocalCgfScale "CORE/mup-group" $mup_group
fi
if [ "$lang" != "" ];then
	SetSettings com.grupoepelsa.TouchScale.config.locale language "$lang"
fi
