#!/bin/bash

# ------------------------------------------------------------------------
# --- SETUP AND USAGE ----------------------------------------------------
# Run it manually:
# bash /data/www/cgi-bin/ffmpeg-monitor.sh
# - OR -
# http://media.home.lan/convert-video.html
# ------------------------------------------------------------------------

echo "Content-type: text/html"
echo "Cache-Control: no-cache, no-store, must-revalidate"
echo "Pragma: no-cache"
echo "Expires: 0"
echo ""

# Define paths
SCRIPT_PATH="/data/scripts/video-converter.sh"
LOG_FILE="/data/scripts/log/video-conversion-$(date +%Y%m%d).log"

SOURCE_DIR="/data/downloads"
HISTORY_FILE="/data/scripts/processed_video_files.log"

# --- CHECK IF SCRIPT IS RUNNING
PID=$(/usr/bin/pgrep -f "$SCRIPT_PATH")

# --- CHECK IF FILES NEED PROCESSING BASED ON THE LOG
pending_count=$(/usr/bin/comm -23 \
    <(/usr/bin/find "$SOURCE_DIR" \( -path "$SOURCE_DIR/incomplete" -o -path "$SOURCE_DIR/logs" \) -prune -o -type f -regex ".*\.\(mkv\|mp4\|m4v\|avi\|mov\|wmv\|webm\|mpg\|mpeg\|ts\|mts\|m2ts\|ogv\)" -printf "%p\n" | /usr/bin/sort) \
    <(/usr/bin/sort "$HISTORY_FILE") | /usr/bin/wc -l)
    
# --- OUTPUT
echo "<html><head><title>FFmpeg Monitor</title>"
echo "<style>"
echo "  body{ font-family:monospace; background:#1E2939; color:#FAFAF9; padding:20px; }"
echo "  a{ text-decoration:none; color:#53EAFD; }"
echo "  .button{ display:inline-block; padding:10px; border: 2px solid #53EAFD; font-weight:bold; text-decoration:none; background:#364153; color:#53EAFD; }"
echo "  .green{ color:#05DF72; }"
echo "  .cyan{ color:#38D5BE; }"
echo "  .orange{ color:#FDC745; }"
echo "  .red{ color:#FB2C36; }"
echo "</style>"

echo "</head><body>"
echo "<h2>Video converter</h2>"

if [ -n "$PID" ]; then
	# --- SCRIPT IS RUNNING ---
	# This indicates to convert-video.html if the script is running
	echo "<div id='active'></div>"
	echo "<p class='green'>STATUS: RUNNING (PID: $PID)</p>"

	# SHOW ELAPSED TIME
	primary_pid=$(echo $PID | awk '{print $1}')
	elapsed_time=$(ps -p "$primary_pid" -o etime= | xargs)
	echo "<p class='cyan'>Elapsed time: ${elapsed_time}</p>"

	# SHOW REMAINING ITEMS
	if [[ $pending_count -gt 0 ]]; then
		echo "<p class='green'>$pending_count items left to process.</p>"
	fi

	echo "<p class='cyan'>Processing may take minutes or several hours - Depending on the conversion required.<br />"
	echo "You can check this page to see how many files are left to convert.</p>"
else
	# --- SCRIPT IS IDLE ---
	echo "<p class='cyan'>STATUS: IDLE</p>"

	# SHOW PENDING INDICATOR
	echo "<p class='cyan'>PENDING: $pending_count videos</p>"
	if [[ $pending_count -gt 0 ]]; then
		# START BUTTON
		echo "<p class='button'><a href='ffmpeg-video-monitor.sh?action=start' target='_self'>START CONVERTING VIDEOS</a></p>"
	fi
fi

# --- LOGIC TO START THE SCRIPT AFTER THE 'START' BUTTON WAS CLICKED
if [ "$QUERY_STRING" == "action=start" ]; then
	if [ -z "$PID" ]; then
		echo "<div id='active' class='green'>STATUS: STARTING</div>"
	   "$SCRIPT_PATH" >> "$LOG_FILE" 2>&1 &
	fi
fi

echo "</body></html>"