custom-scripts/ingest-mac.sh

356 lines
10 KiB
Bash
Executable File

#!/bin/bash
######################################################################################
# Name: Photo/Video Ingest Script for Camera and Drone
# Author: voidf1sh (Skylar Grant)
# Environment: M1 MacBook Pro, Sony a5100, DJI Mini 2
#
# Mounts: (Device Mount -- Volume Mount)
# Camera SD: /dev/diskXsY (varies) -- /Volumes/NO NAME
# Drone SD: /dev/diskXsY (varies) -- /Volumes/Untitled
# NAS: //GUEST:@192.168.0.2/media -- /Volumes/media or /Volumes/media-1 (varies)
#
# Ingest Source Paths:
# Camera SD: /DCIM/100MSDCF
# Drone SD: /DCIM/100MEDIA
#
# Git: https://git.vfsh.dev/voidf1sh/custom-scripts
# File: ingest-mac.sh
# Version: 0.1.1
######################################################################################
# Some initial variables to work with
# Set the date for folder creation
CURRENT_DATE=$(date +%F)
# Paths
# IMPORTANT: Paths should NEVER have trailing slashes, and ALWAYS have leading slashes
# Moving mount points to indices, 0=Camera Mode 1=Drone Mode
INGEST_MODE=9 # Default to an invalid mode.
# Mode names to be used in the script later.
declare -a MODES
MODES[0]="Camera"
MODES[1]="Drone"
# SD card mount point
declare -a SD_MOUNT_POINT
SD_MOUNT_POINT[0]="/Volumes/NO NAME" # Camera
SD_MOUNT_POINT[1]="/Volumes/Untitled" # Drone
# Camera's path to images
declare -a SD_SRC_PATH
SD_SRC_PATH[0]="/DCIM/100MSDCF" # Camera
SD_SRC_PATH[1]="/DCIM/100MEDIA" # Drone
# NAS Network Address
NAS_ADDRESS="//GUEST:@192.168.0.2/media"
# Folder where ingests are stored on the Mac
declare -a MAC_INGEST_FOLDER
MAC_INGEST_FOLDER[0]="$HOME/Pictures/Camera Ingest" # Camera
MAC_INGEST_FOLDER[1]="$HOME/Pictures/Drone Ingest" # Drone
# Folder where ingests are stored on the NAS
declare -a NAS_INGEST_FOLDER
NAS_INGEST_FOLDER[0]="/Camera Ingest"
NAS_INGEST_FOLDER[1]="/Drone Ingest"
# File extensions to be imported
FILE_EXTS0=("JPG" "ARW" "MP4") # Camera TODO: the camera doesn't save to MP4, figure it out.
FILE_EXTS1=("JPG" "DNG" "MP4") # Drone
######################################################################################
# Functions
######################################################################################
# Set mode configuration
set_mode() {
INGEST_MODE=$1
# Where to ingest to on the Mac
MAC_INGEST_PATH="${MAC_INGEST_FOLDER[$INGEST_MODE]}/$CURRENT_DATE"
# Where to ingest to on the NAS
NAS_INGEST_PATH="${NAS_INGEST_FOLDER[$INGEST_MODE]}/$CURRENT_DATE"
# echo "Ingest mode set to ${MODES[$INGEST_MODE]} [$INGEST_MODE]"
}
# Mode Prompt
mode_prompt() {
# Print the selection prompt
echo "[ voidf1sh's Ingest Script ]"
echo ""
echo "Please select an ingest mode:"
echo ""
echo "[1] ${MODES[0]}"
echo "[2] ${MODES[1]}"
echo ""
echo "[0] Exit"
# Wait for input
read -p "Mode: " mode
# Execute the correct commands based on input.
case "$mode" in
1)
# Camera Mode
clear
set_mode 0
;;
2)
# Drone Mode
clear
set_mode 1
;;
0)
# Exit the script
echo "!! Quitting !!"
sleep 0.8s
clear
exit
;;
*)
# clear
echo "!! Invalid Option !!"
exit 1
;;
esac
}
# Create and open Mac folders.
create_folders_mac() {
# Check if the parent ingest directory doesn't exist
if [[ ! -d "$MAC_INGEST_PATH" ]]; then
echo "Creating ingest folder at $MAC_INGEST_PATH"
# Since the parent doesn't exist, we create it and all children at once
mkdir -p "$MAC_INGEST_PATH"
# $? is the exit status of the last commmand, with 0 being success
if [[ $? -eq 0 ]]; then
echo "Success!"
else
echo "Failed to create directory at $MAC_INGEST_PATH"
exit 1
fi
else
echo "Ingest folder already exists: $MAC_INGEST_PATH"
fi
}
# Check if the SD card is mounted
check_sd() {
if mount | grep -q "${SD_MOUNT_POINT[$INGEST_MODE]}"; then
touch /tmp/mounted
else
echo "ERR: SD Card Not mounted!"
exit 1
fi
}
# Check if the NAS is mounted
check_nas() {
if mount | grep -q "$NAS_ADDRESS"; then
# Get the NAS mount point since it can vary
NAS_MOUNT_POINT=$(mount | grep "$NAS_ADDRESS" | awk '{print $3}' | grep -E '^/Volumes/media(-1)?$')
else
echo "ERR: NAS Not mounted!"
exit 1
fi
}
# Cleanup images on camera SD.
# TODO: Update this for the new combined script
clean_sd() {
# Check if the SD card is mounted
if mount | grep -q "/Volumes/NO NAME"; then
echo "SD Card Mounted at $SD_MOUNT_POINT."
else
echo "ERR: SD Card Not mounted!"
exit 1
fi
echo "Traversing to Camera..."
cd "$SD_MOUNT_POINT$SD_SRC_PATH" || { echo "Failed to navigate to SD card path."; return 1; }
echo "Current Directory: $(pwd)"
echo "Are you sure you want to delete all JPG, ARW, and MP4 files in $SD_MOUNT_POINT$SD_SRC_PATH? (y/N)"
read -r confirmation
if [[ "$confirmation" =~ ^[Yy]$ ]]; then
echo "Removing JPG files..."
rm *.JPG
echo "Removing ARW files..."
rm *.ARW
echo "Removing MP4 files..."
rm *.MP4
echo "Cleanup completed!"
else
echo "Cleanup aborted."
fi
cd - || { echo "Failed to return to the previous directory."; return 1; }
echo "Press any key to return to the menu..."
read -n 1 -s
}
# SD->Mac Ingest
ingest_mac() {
local src="${SD_MOUNT_POINT[$INGEST_MODE]}/${SD_SRC_PATH[$INGEST_MODE]}"
create_folders_mac
echo "Beginning copy..."
if [[ $INGEST_MODE -eq 0 ]]; then
# Iterate over FILE_EXTS0 and copy
for ext in "${FILE_EXTS0[@]}"; do
# Check for and enumerate files
files=("$src/"*."$ext")
if [[ -f "${files[0]}" ]]; then
mkdir -p "$MAC_INGEST_PATH/$ext/"
# $? is the exit status of the last commmand, with 0 being success
if [[ $? -eq 0 ]]; then
echo "Created ${ext}s folder."
else
echo "Failed to create ${ext}s folder."
exit 1
fi
echo "Copying ${ext}s..."
rsync -avz --progress "$src/"*."$ext" "$MAC_INGEST_PATH/$ext/"
else
echo "No ${ext}s, skipping."
fi
done
elif [[ $INGEST_MODE -eq 1 ]]; then
# Iterate over FILE_EXTS1 and copy
for ext in "${FILE_EXTS1[@]}"; do
# Check for and enumerate files
files=("$src/"*."$ext")
if [[ -f "${files[0]}" ]]; then
mkdir -p "$MAC_INGEST_PATH/$ext/"
# $? is the exit status of the last commmand, with 0 being success
if [[ $? -eq 0 ]]; then
echo "Created ${ext}s folder."
else
echo "Failed to create ${ext}s folder."
exit 1
fi
echo "Copying ${ext}s..."
rsync -avz --progress "$src/"*."$ext" "$MAC_INGEST_PATH/$ext/"
else
echo "No ${ext}s, skipping."
fi
done
fi
echo "Ingest complete!"
echo "Press any key to return to the menu..."
read -n 1 -s
}
# Mac->NAS Sync
ingest_nas() {
check_nas
local dry_run=""
# Detect if dry run
if [[ $1 -eq 1 ]]; then
dry_run=" --dry-run"
fi
echo "Beginning copy..."
echo "Syncing ingested images with NAS..."
echo "Source: ${MAC_INGEST_FOLDER[$INGEST_MODE]}/"
echo "Destination: $NAS_MOUNT_POINT${NAS_INGEST_FOLDER[$INGEST_MODE]}/"
echo "Press any key to continue..."
read -n 1 -s
# Sync /Volumes/voidf1sh/Pictures/Camera Ingest/ with NAS/media/Camera Ingest/
rsync -azv --progress "${MAC_INGEST_FOLDER[$INGEST_MODE]}/" "$NAS_MOUNT_POINT${NAS_INGEST_FOLDER[$INGEST_MODE]}/" $dry_run
echo "Ingest complete!"
echo "Press any key to return to the menu..."
read -n 1 -s
}
######################################################################################
# Check for Connections
######################################################################################
check_sd
######################################################################################
# Mode Prompt
######################################################################################
# Empty the screen
clear
mode_prompt
######################################################################################
# Prompt Loop
######################################################################################
# Bash allows for linebreaks in string literals and will
# break lines accordingly in the shell
while true; do
# Empty the screen
# clear
# Print the prompt
echo "[ ${MODES[$INGEST_MODE]} Ingest Script ]"
echo ""
echo "Ingest Source Path: ${SD_MOUNT_POINT[$INGEST_MODE]}${SD_SRC_PATH[$INGEST_MODE]}"
echo "Mac Ingest Path: $MAC_INGEST_PATH"
echo "NAS Ingest Path: $NAS_MOUNT_POINT$NAS_INGEST_PATH"
echo ""
echo "Please enter an option from below:"
echo ""
echo "[1] SD->Mac Ingest"
echo "[2] Sync to NAS"
echo "[3] Dry Run Sync to NAS"
echo "[4] !! Delete ALL images on SD card!!"
echo "[5] !! Delete ALL ingested images on Mac!!"
echo "[6] Open folders"
echo ""
echo "[0] Change Mode / Exit"
# Wait for input
read -p "Option: " opt
# Execute the correct commands based on input.
case "$opt" in
1)
# SD->Mac Ingest
clear
ingest_mac
;;
2)
# Mac->NAS Ingest
clear
ingest_nas
# echo "Untested and not working yet!"
;;
3)
# Dry Run Mac->NAS Ingest
clear
ingest_nas 1
# echo "Untested and not working yet!"
;;
4)
# !! Delete ALL media on SD card!!
clear
clean_sd
;;
5)
# !! Delete ALL ingested media on Mac!!
clear
# clean_mac
echo "Untested and not working yet!"
;;
6)
# Open folders
clear
# open_folders
echo "Untested and not working yet!"
;;
0)
# Back out to mode prompt
clear
mode_prompt
;;
*)
# clear
echo "!! Invalid Option !!"
;;
esac
done