#!/bin/bash
# Interactive front end for timelapse on Pi via webcam

# Configuration Variables
INSTALL_DIR="/usr/local/bin"
FILE_LIST=("tl-capture" "tl-clean" "tl-control")
SYNC_SRC_PATH="/tmp/TL/to-sync"
SYNC_DES_PATH="/mnt/media/TL/to-process"
# Cron
CRON_DIR="/etc/cron.d"
CRON_FILE="$CRON_DIR/timelapse"
CRON_FREQUENCY="*/15 * * * *" # Set by prompt
CRON_LOG_PATH="/var/log/${FILE_LIST[0]}.log"
CRON_JOB="root $INSTALL_DIR/${FILE_LIST[0]} >> $CRON_LOG_PATH 2>&1"

# Check for root/sudo
if [ "$EUID" -ne 0 ]; then
    echo "This script must be run as root or with sudo."
    exit 1
else
    echo "Running as root or with sudo."
fi

# Functions

# Start Time Lapse
start_tl() {
    read -p "Image interval in minutes (1-59): " interval
    # Make sure the interval is valid 1-59
    if [[ "$interval" =~ ^[1-9]$|^[1-5][0-9]$ ]]; then
        CRON_FREQUENCY="*/$interval * * * *"
    else
        echo "Invalid interval!"
        return 1
    fi
    echo "Creating cron file: $CRON_FILE"
    touch "$CRON_FILE"
    if [[ $? -eq 0 ]]; then
        echo "Success"
    else
        echo "Unable to create the file. Exiting"
        exit 1
    fi
    echo -e "Writing job to file: $CRON_FILE\n$CRON_JOB"
    cat <<EOF > $CRON_FILE
# Take an image every 15 minutes and save it for a time lapse
$CRON_FREQUENCY $CRON_JOB
EOF
    if [[ $? -eq 0 ]]; then
        echo "Success"
    else
        echo "Unable to write to the file. Exiting"
        exit 1
    fi
}

# Stop Time Lapse
stop_tl() {
    echo "Removing cron file: $CRON_FILE"
    rm $CRON_FILE
    if [[ $? -eq 0 ]]; then
        echo "Success"
    else
        echo "Unable to remove to the file. Exiting"
        exit 1
    fi
}

# NAS Sync
sync_nas() {
    echo "Syncing $SYNC_SRC_PATH to $SYNC_DES_PATH..."
    rsync -av --progress --remove-source-files "$SYNC_SRC_PATH" "$SYNC_DES_PATH" $1
    if [[ $? -eq 0 ]]; then
        echo "Success"
    else
        echo "Unable to complete sync. Exiting"
        exit 1
    fi
}

# Delete synced images
clean_pi() {
    echo "Are you sure??"
    read -n 1 -S
    echo "Sike!"
}

# Menu Prompt
while true; do
    # Empty the screen
    # clear
    # Print the prompt
    echo "[ Time Lapse Control Panel ]"
    echo ""
    echo "[1] Start Time Lapse"
    echo "[2] Stop Time Lapse"
    echo "[3] Dry Run Sync to NAS"
    echo "[4] Sync to NAS"
    echo "[5] !! Delete ALL synced images !!"
    echo ""
    echo "[0] Exit"

    # Wait for input
    read -p "Option: " opt

    # Execute the correct commands based on input.
    case "$opt" in
        1)
            # Start Time Lapse
            clear
            start_tl
            ;;
        2)
            # Stop Time Lapse
            clear
            stop_tl
            ;;
        3)
            # Dry Run NAS Sync
            clear
            sync_nas --dry-run
            # echo "Untested and not working yet!"
            ;;
        4)
            # NAS Sync
            clear
            sync_nas
            ;;
        5)
            # !! Delete ALL synced images on Pi !!
            clear
            clean_pi
            ;;
        0)
            # Exit
            clear
            exit
            ;;
        *)
            # clear
            echo "!! Invalid Option !!"
            ;;
    esac
done