#!/bin/sh

MAIN=org.tizen.common.sign.signer.TizenSigner

SCRIPT="$0"

while [ -h "$SCRIPT" ] ; do
	ls=`ls -ld "$SCRIPT"`

	link=`expr "$ls" : '.*-> \(.*\)$'`
	if expr "$link" : '/.*' > /dev/null; then
		SCRIPT="$link"
	else
		SCRIPT=`dirname "$SCRIPT"`/"$link"
	fi
done

DIR_BIN=`dirname $SCRIPT`
DIR_HOME=$DIR_BIN/..
DIR_LIB=$DIR_HOME/lib
DIR_CONFIG=$DIR_HOME/conf

JARS=`find $DIR_LIB -name "*.jar"`

JAR=""
for JAR_FILE in $JARS; do
	JAR=$JAR:$JAR_FILE
done

CLASSPATH=$DIR_CONFIG:$JAR

OPT_LOGGING="-Dlog4j.configuration=log4j.xml"
OPT_PRG_NAME="-Dcli.name=$SCRIPT"

READ_ARG="n"

OPT_TRACE=

for WORD in $@; do
        if [ "y" = "$READ_ARG" ]
        then
                OPT_LOGGING="-Dlog4j.configuration=log4j-${WORD}.xml"
                if [ "$WORD" = "trace" ]
                then
                        OPT_TRACE="-Dlog4j.debug=true"
                fi
                READ_ARG="n"
        fi
        case $WORD in
        "--log" | "-l" )
        READ_ARG="y"
        ;;
        esac
done

OPT="$OPT_TRACE $OPT_LOGGING $OPT_PRG_NAME"

for ARG in "$@"; do
	#ARGS="$ARGS \"$(printf "%s" "$ARG"| sed 's/\\/\\\\/g' | sed 's/\"/\\"/g' )\""
	ARGS="$ARGS $(printf "'%s'" "$ARG" )"
done

EXEC="java -cp $CLASSPATH $OPT $MAIN $ARGS"

if [ -n "$OPT_TRACE" ]
then
	echo "Args :$#"
        echo "Command :$EXEC"
fi

exec 3>&1 4>&2 #set up extra file descriptors

error=$( { eval "$EXEC" 1>&3; } 2>&1 )

echo "The message is \"${error}\""

exec 3>&- 4>&- # release the extra file descriptors

if [ "${error}" = "" ]
then
    exit 0;
else
    exit 1;
fi


