custom-scripts/ingest-mac.sh

360 lines
11 KiB
Bash
Raw Normal View History

2024-06-10 01:01:08 +00:00
#!/bin/bash
######################################################################################
2024-06-23 17:50:40 +00:00
# Name: Photo/Video Ingest Script for Camera and Drone
2024-06-10 01:01:08 +00:00
# Author: voidf1sh (Skylar Grant)
2024-06-23 17:50:40 +00:00
# Environment: M1 MacBook Pro, Sony a5100, DJI Mini 2
2024-06-10 01:01:08 +00:00
#
2024-06-23 17:50:40 +00:00
# Mounts: (Device Mount -- Volume Mount)
# Camera SD: /dev/diskXsY (varies) -- /Volumes/NO NAME
2024-06-23 17:50:40 +00:00
# Drone SD: /dev/diskXsY (varies) -- /Volumes/Untitled
# NAS: //GUEST:@192.168.0.2/media -- /Volumes/media or /Volumes/media-1 (varies)
2024-06-10 01:01:08 +00:00
#
2024-06-23 17:50:40 +00:00
# Ingest Source Paths:
# Camera SD: /DCIM/100MSDCF
# Drone SD: /DCIM/100MEDIA
#
2024-06-10 01:01:08 +00:00
# Git: https://git.vfsh.dev/voidf1sh/custom-scripts
2024-06-23 17:50:40 +00:00
# File: ingest-mac.sh
# Version: 0.1.1
######################################################################################
2024-06-10 01:01:08 +00:00
# Some initial variables to work with
# Set the date for folder creation
CURRENT_DATE=$(date +%F)
2024-06-23 17:50:40 +00:00
# Paths
# IMPORTANT: Paths should NEVER have trailing slashes, and ALWAYS have leading slashes
2024-06-23 16:52:26 +00:00
2024-06-23 17:50:40 +00:00
# Moving mount points to indices, 0=Camera Mode 1=Drone Mode
INGEST_MODE=9 # Default to an invalid mode.
2024-06-23 17:54:09 +00:00
# Mode names to be used in the script later.
2024-06-23 17:50:40 +00:00
declare -a MODES
MODES[0]="Camera"
MODES[1]="Drone"
2024-06-23 17:54:09 +00:00
2024-06-23 16:52:26 +00:00
# SD card mount point
2024-06-23 17:50:40 +00:00
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
2024-06-23 17:50:40 +00:00
declare -a SD_SRC_PATH
SD_SRC_PATH[0]="/DCIM/100MSDCF" # Camera
SD_SRC_PATH[1]="/DCIM/100MEDIA" # Drone
# NAS Mount address as found with `mount`
NAS_ADDRESS="//voidf1sh@voidNAS"
declare NAS_MOUNT_POINT # Will be set later
2024-06-23 16:52:26 +00:00
# Folder where ingests are stored on the Mac
2024-06-23 17:50:40 +00:00
declare -a MAC_INGEST_FOLDER
MAC_INGEST_FOLDER[0]="$HOME/Pictures/Camera Ingest" # Camera
MAC_INGEST_FOLDER[1]="$HOME/Pictures/Drone Ingest" # Drone
2024-06-23 16:52:26 +00:00
# Folder where ingests are stored on the NAS
2024-06-23 17:50:40 +00:00
declare -a NAS_INGEST_FOLDER
NAS_INGEST_FOLDER[0]="/Camera Ingest"
NAS_INGEST_FOLDER[1]="/Drone Ingest"
2024-06-23 16:52:26 +00:00
# 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
######################################################################################
2024-06-23 17:50:40 +00:00
# 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]"
2024-06-23 17:50:40 +00:00
}
# Mode Prompt
mode_prompt() {
# Print the selection prompt
2024-06-23 17:54:09 +00:00
echo "[ voidf1sh's Ingest Script ]"
2024-06-23 17:50:40 +00:00
echo ""
echo "Please select an ingest mode:"
echo ""
2024-06-23 17:54:09 +00:00
echo "[1] ${MODES[0]}"
echo "[2] ${MODES[1]}"
2024-06-23 17:50:40 +00:00
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
2024-06-23 17:50:40 +00:00
echo "!! Invalid Option !!"
exit 1
;;
esac
}
# Create and open Mac folders.
create_folders_mac() {
# Check if the parent ingest directory doesn't exist
2024-06-23 19:43:38 +00:00
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
2024-06-23 19:43:38 +00:00
mkdir -p "$MAC_INGEST_PATH"
# $? is the exit status of the last commmand, with 0 being success
if [[ $? -eq 0 ]]; then
echo "Success!"
else
2024-06-23 19:43:38 +00:00
echo "Failed to create directory at $MAC_INGEST_PATH"
exit 1
fi
else
2024-06-23 19:43:38 +00:00
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 '^/[^/]+')
echo "NAS Mounted at $NAS_MOUNT_POINT."
else
echo "ERR: NAS Not mounted!"
fi
}
2024-06-22 21:18:23 +00:00
# Cleanup images on camera SD.
2024-06-23 20:14:55 +00:00
# TODO: Update this for the new combined script
clean_sd() {
2024-06-22 21:18:23 +00:00
# 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..."
2024-06-22 21:18:23 +00:00
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
}
2024-06-23 20:14:55 +00:00
# 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
}
2024-06-23 20:14:55 +00:00
# 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..."
2024-06-23 16:52:26 +00:00
echo "Syncing ingested images with NAS..."
echo "Debug: NAS_MOUNT_POINT inside function: $NAS_MOUNT_POINT"
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
2024-06-23 16:52:26 +00:00
# Sync /Volumes/voidf1sh/Pictures/Camera Ingest/ with NAS/media/Camera Ingest/
2024-06-23 19:43:38 +00:00
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
}
2024-06-23 17:50:40 +00:00
######################################################################################
# Check for Connections
######################################################################################
check_sd
check_nas
2024-06-23 17:50:40 +00:00
######################################################################################
# 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
2024-06-23 17:50:40 +00:00
# Empty the screen
# clear
2024-06-23 17:50:40 +00:00
# Print the prompt
2024-06-23 17:54:09 +00:00
echo "[ ${MODES[$INGEST_MODE]} Ingest Script ]"
echo ""
2024-06-23 17:50:40 +00:00
echo "Ingest Source Path: ${SD_MOUNT_POINT[$INGEST_MODE]}${SD_SRC_PATH[$INGEST_MODE]}"
echo "Mac Ingest Path: $MAC_INGEST_PATH"
echo "NAS Mount Path: $NAS_MOUNT_POINT"
echo "NAS Ingest Path: $NAS_INGEST_PATH"
echo ""
echo "Please enter an option from below:"
echo ""
2024-06-23 17:50:40 +00:00
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)
2024-06-23 17:50:40 +00:00
# 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!"
;;
2024-06-23 17:50:40 +00:00
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
2024-06-23 17:50:40 +00:00
clear
# open_folders
echo "Untested and not working yet!"
;;
0)
# Back out to mode prompt
clear
mode_prompt
;;
*)
# clear
echo "!! Invalid Option !!"
;;
esac
done