#!/bin/bash
# Default status for variables
PROGRAM_NAME=$(basename "$0")
remote_sync=false # --remote
root_only=false # --root
output_dir="/tmp" # -o
remote_dir="/tmp" # --sync-dir
device="/dev/video0" # fswebcam -d
frames=1 # fswebcam -F
skips=5 # fswebcam -S

# Generate the date for the filename
current_date=$(date +"%Y-%m-%d_%H-%M")
# Form the filename
out_filename="TL-$current_date.jpg"
# Concat the file path parts into one variable
output_path="${output_dir}/${out_filename}"


usage() {
    echo -e "usage: $PROGRAM_NAME -r resolution [-o output_dir] [-d device] [-F frames] [-S skips] [--root] [--remote --sync-dir remote_dir]"
    echo -e "  -r resolution            Image resolution, e.g. 1920x1080"
    echo -e "  -o output_dir            Path to image storage directory, default /tmp"
    echo -e "  -d device                Device path to webcam, default /dev/video0"
    echo -e "  -F frames                Number of image frames to capture, default 1"
    echo -e "  -S skips                 Number of frames to skip before capturing, default 5"
    echo -e "  --root                   Checks if the script is running as root"
    echo -e "  --remote                 Enable remote syncing of images, must specify --sync-dir as well"
    echo -e "  --sync-dir remote_dir    Path to remote sync destination"
    exit 1
}

# Parse arguments
while [[ "$#" -gt 0 ]]; do
    case "$1" in
        -r)
            resolution="$2"
            shift 2
            ;;
        -o)
            output_dir="$2"
            shift 2
            ;;
        -d)
            device="$2"
            shift 2
            ;;
        -F)
            frames="$2"
            shift 2
            ;;
        -S)
            skips="$2"
            shift 2
            ;;
        --root)
            root_only=true
            shift
            ;;
        --remote)
            remote_sync=true
            shift
            ;;
        --sync-dir)
            remote_dir="$2"
            shift 2
            ;;
        -h|--help)
            usage
            ;;
        *)
            echo "Unknown option: $1"
            usage
            ;;
    esac
done

# Check for required arguments
if [ -z "$resolution" ]; then
    echo "Error: resolution is required"
    usage
fi

# Implement the behavior based on the flags and options
echo "Resolution: $resolution"
echo "Output Directory: $output_dir"
echo "Device: $device"
echo "Frames: $frames"
echo "Skips: $skips"
echo "Running as Root: $root_only"
echo "Remote Sync: $remote_sync"
if [ "$remote_sync" = true ]; then
    if [ -z "$sync_dir" ]; then
        echo "Error: --sync-dir must be specified when --remote is used"
        usage
    else
        echo "Sync Directory: $sync_dir"
    fi
fi

##################################################
# Functions
##################################################
check_for_root() {
    # 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
}

capture() {
    echo -e "Capturing image and saving it to $output_path"
    fswebcam -r "$resolution" -d "$device" "$output_path" -F $frames -S $skips
}

sync() {
    echo "Syncing $output_dir to $remote_dir..."
    rsync -av --progress --remove-source-files "$output_path" "$remote_dir/" $1
    if [[ $? -eq 0 ]]; then
        echo -e "Success"
    else
        echo -e "Unable to complete sync. Exiting"
        exit 1
    fi
}

# Calling functions
# If needed, check for root
if [[ $root_only == true ]]; then
    check_for_root
fi

capture

# If needed, sync the image to remote storage
if [[ $remote_sync == true ]]; then
    sync
fi