#!/bin/bash
# Customizable variables
# Device
DEVICE="/dev/video0"
# Temporary output directory
TO_SYNC_DIR="/tmp/TL/to-sync"
SYNCED_DIR="/tmp/TL/synced"
# NAS Info
NAS_MOUNT_DIR="/mnt/media"
NAS_SYNC_DIR="/TL/synced"

# Pre-script configurations
# Generate the date for the filename
DATE=$(date +"%Y-%m-%d_%H-%M")
# Form the filename
FILENAME="TL-$DATE.jpg"

# Functions

setup_dirs() {
    # Check if To-Sync Directory exists and create it if not
    if [[ ! -d $TO_SYNC_DIR ]]; then
        echo "To-Sync directory doesn't exist, creating it now..."
        mkdir -p $TO_SYNC_DIR
        if [[ $? -eq 0 ]]; then
            echo "Success!"
        else
            echo "Unable to create directory, exiting!"
            exit 1
        fi
    fi

    # Check of Synced Directory exists and create it if not
    if [[ ! -d $SYNCED_DIR ]]; then
        echo "Synced directory doesn't exist, creating it now..."
        mkdir -p $SYNCED_DIR
        if [[ $? -eq 0 ]]; then
            echo "Success!"
        else
            echo "Unable to create directory, exiting!"
            exit 1
        fi
    fi
}

capture() {
    # Capture an image for the timelapse and save it to /tmp/TL-YYYY-MM-DD_HH-MM.jpg
    fswebcam -r 1920x1080 -d /dev/video0 $TO_SYNC_DIR/$FILENAME.jpg -F 1 -S 5
}

sync() {
    local src="$TO_SYNC_DIR/"
    # Sync TL images from Pi to NAS
    rsync -avz 
}

setup_dirs
capture