Add command line arguments and usage info
This commit is contained in:
		
							parent
							
								
									aa10b1ea0d
								
							
						
					
					
						commit
						b693f153ea
					
				
							
								
								
									
										156
									
								
								timelapse/tl-capture
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										156
									
								
								timelapse/tl-capture
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							| @ -1,41 +1,141 @@ | |||||||
| #!/bin/bash | #!/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 | ||||||
| 
 | 
 | ||||||
| # Customizable variables |  | ||||||
| # Paths |  | ||||||
| IMG_DIR="/tmp/TL" |  | ||||||
| NAS_MOUNT_DIR="/mnt/media" |  | ||||||
| SYNC_DES_PATH="/tl/to-process" |  | ||||||
| # Command variables |  | ||||||
| RESOLUTION="1920x1080" |  | ||||||
| DEVICE="/dev/video0" |  | ||||||
| FRAMES="1" |  | ||||||
| SKIPS="5" |  | ||||||
| 
 |  | ||||||
| # Pre-script configurations |  | ||||||
| # Generate the date for the filename | # Generate the date for the filename | ||||||
| DATE=$(date +"%Y-%m-%d_%H-%M") | current_date=$(date +"%Y-%m-%d_%H-%M") | ||||||
| # Form the filename | # Form the filename | ||||||
| FILENAME="TL-$DATE.jpg" | out_filename="TL-$current_date.jpg" | ||||||
|  | # Concat the file path parts into one variable | ||||||
|  | output_path="${output_dir}/${out_filename}" | ||||||
| 
 | 
 | ||||||
| # Functions | 
 | ||||||
| # Capture an image and save it locally | usage() { | ||||||
| capture() { |     echo -e "usage: $PROGRAM_NAME -r resolution [-o output_dir] [-d device] [-F frames] [-S skips] [--root] [--remote --sync-dir remote_dir]" | ||||||
|     # Capture an image for the timelapse and save it to /tmp/TL-YYYY-MM-DD_HH-MM.jpg |     echo -e "  -r resolution            Image resolution, e.g. 1920x1080" | ||||||
|     echo "Capturing image and saving it to $IMG_DIR/$FILENAME" |     echo -e "  -o output_dir            Path to image storage directory, default /tmp" | ||||||
|     fswebcam -r "$RESOLUTION" -d "$DEVICE" "$IMG_DIR/$FILENAME" -F 1 -S 5 |     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 | ||||||
| } | } | ||||||
| # Sync images from local to a remote mounted storage device | 
 | ||||||
| sync() { | # Parse arguments | ||||||
|     echo "Syncing $IMG_DIR to $NAS_MOUNT_DIR$SYNC_DES_PATH..." | while [[ "$#" -gt 0 ]]; do | ||||||
|     rsync -av --progress --remove-source-files "$IMG_DIR/" "$NAS_MOUNT_DIR$SYNC_DES_PATH/" $1 |     case "$1" in | ||||||
|     if [[ $? -eq 0 ]]; then |         -r) | ||||||
|         echo "Success" |             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 |     else | ||||||
|         echo "Unable to complete sync. Exiting" |         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 |         exit 1 | ||||||
|     fi |     fi | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| # Actually run everything | # Calling functions | ||||||
|  | # If needed, check for root | ||||||
|  | if [[ $root_only == true ]]; then | ||||||
|  |     check_for_root | ||||||
|  | fi | ||||||
|  | 
 | ||||||
| capture | capture | ||||||
|  | 
 | ||||||
|  | # If needed, sync the image to remote storage | ||||||
|  | if [[ $remote_sync == true ]]; then | ||||||
|     sync |     sync | ||||||
|  | fi | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user