#! /bin/bash

function main() {
   listen &
   while true; do
      sleep 10
      publish
   done
}

function listen() {
   if [ ! -d /main ]; then
      go build && ./MovieNight
   elif [ ! -e /main/exec-MovieNight ]; then
      /main/MovieNight
   else
      /main/exec-MovieNight
   fi
   kill -9 1
}

function publish() {
   ffmpeg \
      -i rtsp://${RTSP_IP:-192.168.0.83}:${RTSP_PORT:-8554}/unicast \
      -preset ultrafast \
      -filter:v scale=-1:${RES:-720} \
      -vcodec libx264 \
      -acodec copy \
      -f flv \
      -b:v ${KBPS:-500}k \
      -b:a 0k \
      rtmp://localhost:1935/live/ALongStreamKey 
}

if [ "$0" == "$BASH_SOURCE" ]; then
   main "$@"
fi
