#!/bin/bash -eu # Change the value on the right side of the equal sign to the name of the server hosting the archive. ARCHIVE_HOST_NAME=archiveserver LOGFILE=/tmp/archiveloop.log function log () { echo "$( date )" >> "$LOGFILE" echo "$1" >> "$LOGFILE" } function archive_is_reachable () { local reachable=true ping -q -w 1 -c 1 "$ARCHIVE_HOST_NAME" > /dev/null 2>&1 || reachable=false if [ "$reachable" = false ] then false return fi true } function connect_usb_drives_to_host() { log "Connecting usb to host..." modprobe g_mass_storage log "Connected usb to host." } function wait_for_archive_to_be_reachable () { log "Waiting for archive to be reachable..." while [ true ] do if archive_is_reachable then log "Archive is reachable." break fi sleep 1 done } function archive_clips () { log "Archiving..." /root/bin/archive-teslacam-clips log "Finished archiving." } function wait_for_archive_to_be_unreachable () { log "Waiting for archive to be unreachable..." while [ true ] do if ! archive_is_reachable then log "Archive is unreachable." break fi sleep 1 done } export -f connect_usb_drives_to_host log "Starting..." if archive_is_reachable then archive_clips wait_for_archive_to_be_unreachable else connect_usb_drives_to_host fi while [ true ] do wait_for_archive_to_be_reachable archive_clips wait_for_archive_to_be_unreachable done