#!/bin/sh

export BASEDIR=$(dirname $0)
export system64=$(uname -m)
cd $BASEDIR

show_help() {
	echo Usage : FlashToolConsole [OPTIONS]
        echo
	echo MANDATORY :
	echo '     --action=flash|imei'
        echo '     --file=/path/to/file.ftf (only when action=flash)'
	echo
	echo
	echo OPTIONAL :
	echo '    --wipedata=yes|no (only when action=flash)'
	echo '    --wipecache=yes|no (only when action=flash)'
	echo '    --baseband=yes|no (only when action=flash)'
	echo '    --system=yes|no (only when action=flash)'
	echo '    --kernel=yes|no (only when action=flash)'
	exit 0
}

if test $# -eq 0
then
   show_help;
fi

while test $# != 0
do
  case $1 in
  --*=*)
    ac_option=`expr "X$1" : 'X\([^=]*\)='`
    ac_optarg=`expr "X$1" : 'X[^=]*=\(.*\)'`
    ac_shift=:
    ;;
  *)
    ac_option=$1
    ac_optarg=$2
    ac_shift=shift
    ;;
  esac

  case $ac_option in
  -a | --action)
     export p_action=$ac_optarg;;
  -f | --file)
     export p_file=$ac_optarg;;
  --wipedata)
     export p_data=$ac_optarg;;
  --wipecache)
     export p_cache=$ac_optarg;;
  --baseband)
     export p_baseband=$ac_optarg;;
  --system)
     export p_system=$ac_optarg;;
  --kernel)
     export p_kernel=$ac_optarg;;
  -h | --help)
     show_help;;
  *) 
     show_help;;
  esac
  shift
done

case $p_action in
   flash | imei)
      ;;
   *)
     show_help;;
esac

if [ "$p_action" = "flash" ]
then
    if test -z "$p_file"
    then
        echo --file is mandatory when --action=flash
        exit 1
    fi
    if [ -e $p_file ]
    then
    if test -z $p_data
    then
       p_data=$(echo yes)
    fi
    if test -z $p_cache
    then
       p_cache=$(echo yes)
    fi
    if test -z $p_system
    then
       p_system=$(echo yes)
    fi
    if test -z $p_kernel
    then
       p_kernel=$(echo yes)
    fi
    if test -z $p_baseband
    then
       p_baseband=$(echo yes)
    fi
    else
       echo $p_file does not exist.
       exit 1
    fi
fi
 
chmod 755 ${BASEDIR}/x10flasher_lib/adb.linux
chmod 755 ${BASEDIR}/x10flasher_lib/fastboot.linux
chmod 755 ${BASEDIR}/x10flasher_lib/adb.mac
chmod 755 ${BASEDIR}/x10flasher_lib/fastboot.mac
chmod 755 ./x10flasher_lib/bin2elf
chmod 755 ./x10flasher_lib/bin2sin

if test -z "${JAVA_HOME}"
then
   if test "${system64}" = "x86_64"
   then
      export JAVA_HOME=${BASEDIR}/x10flasher_lib/linjre64
   else
      export JAVA_HOME=${BASEDIR}/x10flasher_lib/linjre32
   fi
   echo "JAVA_HOME not set. Using default value : ${JAVA_HOME}"
fi

if test -e ${JAVA_HOME}/bin/java
then
   for i in $(ls ${BASEDIR}/x10flasher_lib/*jar)
   do
      CLASSPATH=$CLASSPATH$(echo $BASEDIR/$i:)
   done
   case $p_action in
      flash)
         $JAVA_HOME/bin/java -Xms128m -Xmx512m -Duser.country=US -Duser.language=en -cp ${CLASSPATH}${BASEDIR}/x10flasher.jar gui.FlasherGUI -console --action=flash --file=$p_file --wipedata=$p_data --wipecache=$p_cache --baseband=$p_baseband --system=$p_system --kernel=$p_kernel
         ;;
      imei)
         $JAVA_HOME/bin/java -Xms128m -Xmx512m -Duser.country=US -Duser.language=en -cp ${CLASSPATH}${BASEDIR}/x10flasher.jar gui.FlasherGUI -console --action=imei
   esac
else
   echo "No Java in specified path in JAVA_HOME=${JAVA_HOME}"
   echo "Set the variable to a valid Java installation"
fi
