#!/bin/bash
# WF 2021-10-20
# run script for scan2wiki server

#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'

#
# a colored message
#   params:
#     1: l_color - the color of the message
#     2: l_msg - the message to display
#
color_msg() {
  local l_color="$1"
  local l_msg="$2"
  echo -e "${l_color}$l_msg${endColor}"
}

# error
#
#   show an error message and exit
#
#   params:
#     1: l_msg - the message to display
error() {
  local l_msg="$1"
  # use ansi red for error
  color_msg $red "Error: $l_msg" 1>&2
  exit 1
}

#
# show the usage
#
usage() {
  echo "usage: $0 [-s] [-c] [-h] [--host host] [--port port]"
  echo "  -h: show this usage"
  echo "  -s: start as server"
  echo "  -k: kill server if running"
  echo "  -c: start client"
  echo "  --host host - the host to serve from"
  echo "  --port port - the port to server from"
}
export PYTHONPATH=""

#
# open the given url waiting for the given number of seconds
#
# param #1: the url to open
# param #2: the number of loops to wait
# param #3: the sleep time per loop
openUrl() {
  local l_url="$1"
  local l_loops="$2"
  local l_sleep="$3"
  local l_count=1
  local l_done=0
  until [ $l_done -eq 1 ]
  do
    l_count=$((l_count+1))
    if [ "$l_count" -ge "$l_loops" ]
    then
      echo "giving up to wait for $l_url"
      l_done=1
    fi
    status=$(curl -Is $l_url | head -1)
    echo "waiting $l_count/$l_loops for $l_url: $status"
    case $status in
      *200*OK*)
        open $l_url&
        l_done="1" ;;
      *)
        sleep $l_sleep
        ;;
    esac
  done
}

#
# kill the given process by name if it is running
#
# param #1: l_name: the name to search for
killifrunning() {
  local l_name="$1"
  pgrep -fl "$l_name"
  if [ $? -eq 0 ]
  then
    color_msg $blue "killing running $l_name server"
    sudo pkill -f "$l_name"
  fi
}

#
# start the scan2wiki  server
#
startServer() {
  local l_baseUrl="$1"
  local l_logdir=/var/log/scan2wiki
  local l_logfile=scan2wiki.log
  color_msg $blue "starting server only"
  if [ ! -d $l_logdir ]
  then
    sudo mkdir -p $l_logdir
    sudo chmod 770 $l_logdir
  fi
  sudo chown $USER $l_logdir
  sudo chgrp $(id -gn) $l_logdir
  export PYTHONPATH="."
  cmd="python3 $pyapp -s -l --host 0.0.0.0 -rp $HOME/Pictures/scan"
  nohup $cmd > $l_logdir/$l_logfile 2>&1 &
  color_msg $green "log for $cmd is at $l_logdir/$l_logfile"
}

pyapp=scan/scan_cmd.py
port=8334
host=$(hostname)
baseUrl="http://$host:$port"

# commandline option
while [  "$1" != ""  ]
do
  option=$1
  shift
  case $option in
    -h|--help)
      usage
      exit 0;;
    --baseUrl)
      if [ $# -lt 1 ]
      then
        usage
      fi
      baseUrl=$1
      shift
      ;;
    -s)
      startServer $baseUrl
      ;;
    -k)
      killifrunning $pyapp
      ;;
    -c)
     color_msg $blue "starting in client mode"
     openUrl "http://localhost:$port" 60 0.5&
     ;;
    *)
      ;;
  esac
done
