#!/bin/bash
while true; do (

NAME=$1
SERVER_IP=192.168.1.66
#SERVER_IP=192.168.1.20
PORT=8080
USERNAME=admin
PASSWORD="123456"
CAMERAS="0 1 2 3 4 5 6 7 8 9"
# the real values _will_ differ depending on the machine running the script !!!
SECONDS_PER_FILE=3600
OVERLAP_PER_FILE=60
FFMPEG_TIMEOUT=10000000 # 10 seconds

PIDFILE_BASE=/tmp/wget_script.$RANDOM.pids
if [ -z "$NAME" ]; then
	NAME=0
fi
SCRIPT_DIR="$( cd $( dirname "${BASH_SOURCE[0]}" ); pwd )/"
CAPTURE_DIR="${SCRIPT_DIR}/capture/"
mkdir -p $CAPTURE_DIR
cd $CAPTURE_DIR
#OUT_FILE_TYPE="mjpeg"
OUT_FILE_TYPE="avi"
SCRIPT_PID=$$

SHUTDOWN_IN_PROGRESS="false"
quit() {
	if [ "$SHUTDOWN_IN_PROGRESS" == "false" ]; then
		SHUTDOWN_IN_PROGRESS="true"
		echo "quitting..."
		PIDS="$(cat ${PIDFILE_BASE}_A 2>/dev/null) $(cat ${PIDFILE_BASE}_B 2>/dev/null)"
		rm -f ${PIDFILE_BASE}_A ${PIDFILE_BASE}_B ${PIDFILE_BASE}_KILL_A ${PIDFILE_BASE}_KILL_B ${PIDFILE_BASE}_SLEEP_PID_A ${PIDFILE_BASE}_SLEEP_PID_B ${PIDFILE_BASE}_SLEEP_SYNC_A ${PIDFILE_BASE}_SLEEP_SYNC_B
		kill $PIDS 2>/dev/null || (echo "waiting for remaining proccesses..."; sleep 10; echo "killing remaining proccesses..."; kill -9 $PIDS 2>/dev/null)
		exit $1
	fi
}

trap 'quit 0' SIGINT SIGTERM EXIT

do_record() {
	GROUP_ID=$1
	for i in $CAMERAS; do
		echo "starting recording for camera $i" >&2
		OUTFILE_START=tmp_dump_cam_${i}_$(date +%s.%N)_RANDOM$RANDOM.$OUT_FILE_TYPE
		( 
			EXACT_START_TIME=$(date +%s.%N)
			outfile_close_trap() {
				echo "finishing capture file for camera $i"
				EXACT_END_TIME=$(date +%s.%N)
				if [ -f $OUTFILE_START ]; then
					mv $OUTFILE_START dump_cam_${i}_${EXACT_START_TIME}_${EXACT_END_TIME}.$OUT_FILE_TYPE
				fi
                                if [ -f ${PIDFILE_BASE}_KILL_${GROUP_ID} ]; then
                                        THIS_KILL_PID="$(cat ${PIDFILE_BASE}_KILL_${GROUP_ID} | grep -oe "\<${THIS_CAPTURE_PID}\>" )"
                                fi
                                if [ -z "$THIS_KILL_PID" ]; then
                                        # echo "capture for camera $i died, killing group $GROUP_ID"
					# while [ -f ${PIDFILE_BASE}_SLEEP_SYNC_$GROUP_ID ]; do sleep 1; done
					# if [ -f ${PIDFILE_BASE}_SLEEP_PID_${GROUP_ID} ]; then
					#	THIS_SLEEP_PID="$(cat ${PIDFILE_BASE}_SLEEP_PID_${GROUP_ID})"
					#	kill -9 $THIS_SLEEP_PID
					# fi
					echo "capture for camera $i died, commiting suicide"
					kill $SCRIPT_PID >> /dev/null 2>&1
					sleep 10
					kill -9 %SCRIPT_PID >> /dev/null 2>&1
                                fi
			}
			trap outfile_close_trap SIGINT SIGTERM EXIT
			#wget --ignore-length --http-user=$USERNAME --http-passwd=$PASSWORD  http://${SERVER_IP}:$PORT/cam/$i/stream.mjpeg -O $OUTFILE_START -q >> /dev/null 2>&1 &
			#THIS_CAPTURE_PID=$!
			${SCRIPT_DIR}/ffmpeg -use_wallclock_as_timestamps 1 -timeout $FFMPEG_TIMEOUT -f mjpeg -i http://$USERNAME:$PASSWORD@${SERVER_IP}:$PORT:8080/cam/$i/stream.mjpeg -c copy -ts abs -y $OUTFILE_START >> /dev/null 2>&1 &
			THIS_CAPTURE_PID=$!
			echo $THIS_CAPTURE_PID >> ${PIDFILE_BASE}_$GROUP_ID
			wait $THIS_CAPTURE_PID
		) &
	done
}	

while true; do
	if [ ! -z "$PIDS_A" ]; then kill -9 $PIDS_A 2>/dev/null; fi
	rm -f ${PIDFILE_BASE}_KILL_A
	echo "starting group A"
	touch ${PIDFILE_BASE}_SLEEP_SYNC_A
	do_record A
	PIDS_B="$(cat ${PIDFILE_BASE}_B 2>/dev/null)"
	if [ ! -z "$PIDS_B" ]; then
		sleep $OVERLAP_PER_FILE
		echo "killing group B"
		echo $PIDS_B > ${PIDFILE_BASE}_KILL_B
		kill $PIDS_B 2>/dev/null
		sleep $(($SECONDS_PER_FILE - $OVERLAP_PER_FILE)) &
		SLEEP_PID=$!
	else
		sleep $SECONDS_PER_FILE &
		SLEEP_PID=$!
	fi
	echo $SLEEP_PID > ${PIDFILE_BASE}_SLEEP_PID_A
	rm -f ${PIDFILE_BASE}_SLEEP_SYNC_A
	wait $SLEEP_PID

	if [ ! -z "$PIDS_B" ]; then kill -9 $PIDS_B 2>/dev/null; fi
	rm -f ${PIDFILE_BASE}_KILL_B
	echo "starting group B"
	touch ${PIDFILE_BASE}_SLEEP_SYNC_B
	do_record B
	PIDS_A="$(cat ${PIDFILE_BASE}_A 2>/dev/null)"
	if [ ! -z "$PIDS_A" ]; then
		sleep $OVERLAP_PER_FILE
		echo "killing group A"
		echo $PIDS_A > ${PIDFILE_BASE}_KILL_A
		kill $PIDS_A 2>/dev/null
		sleep $(($SECONDS_PER_FILE - $OVERLAP_PER_FILE)) &
		SLEEP_PID=$!
	else
		sleep $SECONDS_PER_FILE &
		SLEEP_PID=$!
	fi
	echo $SLEEP_PID > ${PIDFILE_BASE}_SLEEP_PID_B
	rm -f ${PIDFILE_BASE}_SLEEP_SYNC_B
	echo waiting B
	wait $SLEEP_PID
	echo done B
done

quit 1

) done
