#!/bin/sh

set -e

if [ "${DEBUG:-no}" != 'no' ]; then
   set -x
fi

. '/usr/lib/avreg-common/helpers.sh'

BASE_HTTP_PORT_DEF=874
BASE_RTSP_PORT_DEF=8574
GROUP_SZ_DEF=10
MAX_CAMS_DEF=100
PORT_STEP=1

BASE_HTTP_PORT=$BASE_HTTP_PORT_DEF
BASE_RTSP_PORT=$BASE_RTSP_PORT_DEF
GROUP_SZ=$GROUP_SZ_DEF
MAX_CAMS=$MAX_CAMS_DEF
OUTPUT_DIR='./'

APPNAME=$(basename $0)

usage() {
   cat <<__EOF__
Usage: $APPNAME [OPTIONS] [EMPTY-OUTPUT-OUTPUT_DIR]

Options:
  -c, --cams=NBR             max numbers of camera's streams, default $MAX_CAMS_DEF
  -g, --group=NUM            max camera's streams per profile (process avregd), default $GROUP_SZ_DEF
  -p, --http-base=HTTP_PORT  base avregd http server port, default $BASE_HTTP_PORT_DEF
  -r, --rtsp-base=RTSP_PORT  base avregd rtsp server port, default $BASE_RTSP_PORT_DEF
  -s, --port-step=PORT_STEP  step of port values increment, default 1

  -h, --help                 display this help and exit
  -v, --version              output version information and exit

Examples:

# Make and setup 5 profiles with 4 streams per each avregd process:

mkdir /tmp/empty-dir

$APPNAME -c20 -g4 /tmp/empty-dir

sudo avreg-service stop

sudo rm /etc/avreg/profiles/*

sudo cp /tmp/empty-dir/* /etc/avreg/profiles/

sudo systemctl daemon-reload

sudo avreg-service start

Report bugs to <support@avreg.net>.
__EOF__
}

version() {
   cat <<__EOF__
$APPNAME (AVReg) 6.3

Copyright (C) 2020 Setevye informatsionnye sistemy, Ltd
__EOF__
}

print_profile() {
   local min_cam=$1 max_cam=$2 http_port=$3 rtsp_port=$4
   local devlist
   if [ "$min_cam" = "$max_cam" ]; then
      devlist="$min_cam"
   else
      devlist="${min_cam}-${max_cam}"
   fi
   cat <<EOF
devlist = ${devlist}

avregd {
    http-port = $http_port
    rtsp-port = $rtsp_port
}

avreg-site {
    avregd-httpd = 'http://\$_SERVER[SERVER_NAME]:$http_port'
}
EOF
}

set +e
TEST=$(getopt -o 'hvc:g:p:r:s:' \
   --longoptions 'help,version,cams:,group:,http-base:,rtsp-base:,port-step:' \
   -n "${APPNAME}" \
   -- "$@")
if [ $? -ne 0 ]; then
   usage >&2
   exit 1
fi
set -e

# Note the quotes around `$TEST': they are essential!
eval set -- "$TEST"

pos=0
while [ -n "$*" ]; do
   case "$1" in
   '-v' | '--version')
      version >&2
      exit 0
      ;;
   '-h' | '--help')
      usage >&2
      exit 0
      ;;
   '-c' | '--cams')
      MAX_CAMS=$(($2 / 1))
      shift 2
      ;;
   '-g' | '--group')
      GROUP_SZ=$(($2 / 1))
      shift 2
      ;;
   '-p' | '--http-base')
      BASE_HTTP_PORT=$(($2 / 1))
      shift 2
      ;;
   '-r' | '--rtsp-base')
      BASE_RTSP_PORT=$(($2 / 1))
      shift 2
      ;;
   '-s' | '--port-step')
      PORT_STEP=$(($2 / 1))
      shift 2
      ;;
   '--') shift ;;
   *)
      case "$pos" in
      0)
         OUTPUT_DIR="$1"
         shift
         ;;
      *)
         usage "Ambiguous option \"$1\"" >&1
         exit 1
         ;;
      esac
      pos=$(($pos + 1))
      ;;
   esac
done

if [ ! -d "$OUTPUT_DIR" ]; then
   echo "Error: $OUTPUT_DIR is not a directory." >&2
   usage >&2
   exit 1
fi

if ! is_dir_empty "$OUTPUT_DIR"; then
   echo "Error: $OUTPUT_DIR is not a empty directory." >&2
   usage >&2
   exit 1
fi

cam_start=1
http_port=$BASE_HTTP_PORT
rtsp_port=$BASE_RTSP_PORT
a=${#MAX_CAMS}

while :; do
   cam_end=$((cam_start + GROUP_SZ - 1))

   if [ ${GROUP_SZ} -eq 1 ]; then
      profile_name=$(printf "c%.${a}u" $cam_start)
   else
      profile_name=$(printf "c%.${a}u-%.${a}u" $cam_start $cam_end)
   fi

   print_profile $cam_start $cam_end $http_port $rtsp_port >"$OUTPUT_DIR/$profile_name"

   [ $cam_end -ge $MAX_CAMS ] && break
   cam_start=$((cam_start + GROUP_SZ))
   http_port=$((http_port + PORT_STEP))
   rtsp_port=$((rtsp_port + PORT_STEP))
done
