#!/usr/bin/ksh

# Camera credentials: 
# (modify here after 3 lines as needed)
login=###2_B_DEFINED###
pwd=###2_B_DEFINED###
ip=###2_B_DEFINED###

# Initialisation depending camera PTZ speed
# v_speed (visual_speed) is number of pixel move during 1 sec. move depending
# the  PTz speed. Default is 250 with camera PTz speed=1; 700 with PTz speed=0
# When using motion, please set track_speed within /etc/motion/motion.conf
# instead of here after v_speed.
v_speed=250

EchoFatal ()
{
  echo "ERROR:$*" 1>&2
  exit 1
}

EchoErr ()
{
  echo "ERROR:$*" 1>&2
}

LogResult ()
{
  echo "$1" >> $l_LOG
}

DBug()
{
  (
  if [[ -n "$l_dbg" ]]
  then
    echo "DBG: $*"
    echo "DBG: Press [ENTER] to continue\c"
    read x
  fi
  )
}

printUsage ()
{
  if [[ -z "$1" ]]
  then
    echo "\nUsage: $progName [-h|V|dbg] <-bright [0-256]|-mode [0-3]|-set [0-16]|-goto [0-16]|-mv [DIR] [step_size|-max]>\n"
    echo "DESCRIPTION"
    echo "   $nickName send Get command to the Foscam FI8918W"
    echo "   camera so that setting the brighness, the light mode,"
    echo "   set or goto pre-setted positions or move one or max step"
    echo "   along the provided DIR direction. DIR is either :"
    echo "   LEFT, RIGHT, UP, DOWN, LEFT_UP, RIGHT_UP, LEFT_DOWN or RIGHT_DOWN"
    echo "   Please note that a single param has to be be set each time but -max.\n"
    echo "   NEW in Version 2.0 :"
    echo "   When providing the step_size, the number of step is estimated.\n"
    echo "OPTIONS"
    echo "   The following options are now available:"
    echo "\t-h  help:  runs the inline help you are reading,"
    echo "\t-V  Vers:  Print release version."
    echo "\t-dbg    :  toggle on debug mode."
    echo "\t-bright :  set the brightness,"
    echo "\t-mode   :  light mode (50Hz,60Hz,outdoor),"
    echo "\t-set    :  set the provided arg position,"
    echo "\t-goto   :  goto the provided arg position,"
    echo "\t-mv move:  move 1 or max step in the provided direction\n"
  else
    EchoErr "$1, Usage: $progName [-h|V|dbg] <-bright [0-256]|-mode [0-3]|-set [0-16]|-goto [0-16]|-mv [DIR] [step_size|-max]>"
  fi
  exit 1
}

#

Set_num_with ()
{
  case $1 in
  UP)         num=0;;
  DOWN)       num=2;;
  LEFT)       num=6;;
  RIGHT)      num=4;;
  LEFT_UP)    num=91;;
  RIGHT_UP)   num=90;;
  LEFT_DOWN)  num=93;;
  RIGHT_DOWN) num=92;;
  *) EchoFatal "Unknown direction \"$1\"";;
  esac
}

readStepSize ()
{
  [[ -n "$Move_Cam" ]] || printUsage "arg \"$1\" coded yet"
  motion_track_speed=$(($(cat /etc/motion/motion.conf | grep '^track_speed' | awk '{print $2}')))
  [[ -n "$motion_track_speed" ]] && [[ 50 -lt $motion_track_speed ]] && v_speed=$motion_track_speed
  sleep_t=$(echo "scale=2; $1 / $v_speed;" | bc)
}

################# MAIN ##############################
l_pid=$$
progName=${0##*/}
progArgs="$*"
progDir=${0%/*}
outDir="/tmp"
nickName=${progName%.ksh}
VERSION="$nickName Version 2.0 - 2011.09.26"
l_dbg=""
mode="-q"
log=$nickName.$$
Move_Cam=""
# acending compatibility with v1.0 : 
# sleep_t="0.4" et sleep_max="1" with PTz steep equal 1
sleep_t="0.4"
sleep_max="1.0"


Received="$0 $*"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Recuperation du parametre en entree #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

while [[ $# -ne 0 ]]
do
  case $1 in
    -h)  printUsage ;;
    -V)  echo "$VERSION"; exit 0 ;;
    -max) sleep_t="$sleep_max";;
    -dbg) l_dbg="DBG"; mode="-v";;
    -brightness) shift; cmd="camera_control.cgi?param=1&value=$1";;
    -mode) shift; cmd="camera_control.cgi?param=3&value=$1";;
    -set) shift; num=$1; cmd="decoder_control.cgi?command=$((num*2+30))";;
    -goto) shift; num=$1; cmd="decoder_control.cgi?command=$((num*2+31))";;
    -mv) shift; Set_num_with $1; cmd="decoder_control.cgi?command=$num"; Move_Cam="True";;
    -*) printUsage "Unknown flag \"$1\"";;
    *)  readStepSize $1;;
  esac
  shift
done

[[ -n "$l_dbg" ]] || exec 1>/dev/null
[[ -w $outDir ]] || EchoFatal "$nickName: can\'t write to dir: $outDir"
cd $outDir

if [[ -n "$Move_Cam" ]]
then
  fork=$(($(ps -o pid,comm | grep $progName | grep -v $l_pid | wc -l)))
  if [[ 2 -le $fork ]]
  then
    echo "$(date) : pid=$l_pid,n=$fork allready running \"$fork\" IGNORING received $Received" | tee -a /var/log/$nickName.dbg.log
    ps -o pid,comm | grep $progName | tee -a /var/log/$nickName.dbg.log
    exit 0
  fi
  /usr/bin/wget $mode -O $log "http://$ip/$cmd&user=$login&pwd=$pwd"
  /bin/sleep "$sleep_t"
  /usr/bin/wget $mode -O $log "http://$ip/decoder_control.cgi?command=1&user=$login&pwd=$pwd"
else
  /usr/bin/wget $mode -O $log "http://$ip/$cmd&user=$login&pwd=$pwd"
fi
RC="$(cat $outDir/$log | grep -v ok)"
rm -f $outDir/$log
[[ -z "$RC" ]] || EchoFatal $RC
exit 0
