#! /bin/sh
#set -x
avnavdir=`readlink -f $0`
avnavdir=`dirname $avnavdir`
pdir=$avnavdir
#demochart from the viewer/demo dir, will be copied to the charts dir
#if also copying the template
DEMOCHART="osm-online.xml"

basedir=""
chartdir=""
xmlfile=""
viewer=""
silent=0
gui=0
loglevel="INFO"
template="$avnavdir/avnav_template.xml"

usage(){
  echo "usage: $0 [-e] [-d] [-q] [-b basedir] [-c chartdir] [-x cfgfile] [-t template] [-g] [-a softwareBase] [-v viewerDir]"
  echo "       -b basedir     : data dir for avanv routes,tracks,charts,..."
  echo "       -c chartdir    : separate dir for charts (instead of basedir/charts)"
  echo "       -x cfgfile     :  start the server directly with this config (not for gui)"
  echo "       -t template    : create basedir/avnav_server.xml from this if not found"
  echo "       -l loglevel    : set the log level (ERROR|INFO|DEBUG)"
  echo "       -e             : directly fail on any errors in avnav_server.xml instead of trying avnav_server.xml.ok"
  echo "       -d             : start with loglevel debug"
  echo "       -g             : start with GUI instead of normal server mode"
  echo "       -q             : do not log to stderr"
  echo "       -n envFile     : read environment variables from env file if it exists"
  echo "       -------------- some development options --------------"
  echo "       -a softwareBase: find the server software at this directory"
  echo "       -v viewerDir   : find the viewer software at this directory"
}

err(){
  echo "ERROR: $*"
  exit 1
}


while getopts dec:b:x:qgt:a:v:l:n: opt; do
case $opt in
  d)
    loglevel="DEBUG"
    ;;
  e)
    eopt="-e"
    ;;
  c)
    chartdir=$OPTARG
    ;;
  b)
    basedir=$OPTARG
    ;;
  x)
    xmlfile=$OPTARG
    [ ! -f "$xmlfile" ] && err "config file $xmlfile not found"
    ;;
  q)
    silent=1
    ;;
  g)
    gui=1
    ;;
  t)
    template="$OPTARG"
    [ ! -f "$template" ] && err "template $template not found"
    ;;
  a)
    avnavdir="$OPTARG"
    ;;
  v)
    viewer="$OPTARG"
    ;;
  l)
    loglevel="$OPTARG"
    ;;
  n)
    envFile="$OPTARG"
    if [ -r "$envFile" ]  ; then
    . "$envFile"
    fi
    ;;
  \?)
    echo "invalid option $opt"
    usage
    exit 1
esac
done
shift $((OPTIND-1))

[ "$gui" != 0 -a "$chartdir" != "" ] && err "you cannot set the chartdir when running the gui"
[ "$gui" != 0 -a "$xmlfile" != "" ] && err "you cannot set the config file when running the gui"

startprog="$avnavdir/server/avnav_server.py"
[ ! -x $startprog ] && err "$startprog not found"

guiprog="$avnavdir/gui/avnav_gui.py"
if [ "$gui" != 0 ]; then
  if [ ! -f "$guiprog" ]; then
    guiprog="$pdir/avnav_gui.py"
  fi
  [ ! -x "$guiprog" ] && err "the gui $guiprog is not installed"
fi  

if [ "$basedir" = "" ] ; then
  if [ "$HOME" = "" ] ; then
    err "no parameter -b is set and environment HOME is unset"
  fi
  basedir="$HOME/avnav"
  if [ -d $basedir/data ] ; then
    basedir=$basedir/data
    echo "using old style basedir $basedir"
  fi
fi
if [ ! -d $basedir ] ; then
    echo "creating $basedir"
    mkdir -p $basedir || err "unable to create $basedir"
fi
basedir=`readlink -f $basedir`
for d in log charts routes tracks ; do
  if [ ! -d "$basedir/$d" ] ; then
    mkdir -p "$basedir/$d" || err "unable to create $basedir/$p"
  fi
done
if [ "$chartdir" = "" ] ; then
  chartdir="$basedir/charts"
fi
if [ ! -d "$chartdir" ] ; then
  mkdir -p "$chartdir" || err "unable to create chart dir $chartdir"
fi
if [ "$viewer" = "" ]; then
  viewer="$avnavdir/viewer"
fi
viewer=`readlink -f "$viewer"`
if [ "$xmlfile" = "" ] ; then
  xmlfile=$basedir/avnav_server.xml
  if [ ! -f "$xmlfile" ] ; then
    echo "creating initial $xmlfile"
    sed -e "s?#DATADIR#?$basedir?" -e "s?#AVNAVDIR#?$avnavdir?" "$template" > $xmlfile || err "unable to create $xmlfile"
    demo="$viewer/demo/$DEMOCHART"
    if [ -f "$demo" ];then
      echo "copying demo chart $demo"
      cp "$demo" "$chartdir"
    fi  
  fi
fi
if [ "$gui" != 0 ] ; then
  python3 -c 'import wx' > /dev/null 2>&1
  [ $? != 0 ] && err "missing package python3-wxgtkn.n, please install a valid version of this package (e.g. python3-wxgtk3.0)"
  exec $guiprog -b "$basedir" -u "viewer=$viewer,libraries=$avnavdir/viewer/libraries,sounds=$avnavdir/sounds"
else
  echo "chartdir=$chartdir, basedir=$basedir, config=$xmlfile, starting server..."
  if [ "$silent" = 0 ] ; then
    exec $startprog  -l $loglevel -c "$chartdir" -w "$basedir" -u "viewer=$viewer,libraries=$avnavdir/viewer/libraries,sounds=$avnavdir/sounds" $dopt $eopt "$xmlfile"
  else
    exec $startprog  -q  -l $loglevel -c "$chartdir" -w "$basedir" -u "viewer=$viewer,libraries=$avnavdir/viewer/libraries,sounds=$avnavdir/sounds" $dopt $eopt "$xmlfile" 
  fi
fi


