#!/bin/bash -eu USER_ENABLED_PUSHOVER=false REPO=${REPO:-cimryan} BRANCH=${BRANCH:-master} if ! [ $(id -u) = 0 ] then echo "STOP: Run sudo -i." exit 1 fi function check_variable () { local var_name="$1" if [ -z "${!var_name+x}" ] then echo "STOP: Define the variable $var_name like this: export $var_name=value" exit 1 fi } function check_pushover_enabled () { if [ ! -z "${pushover_enabled+x}" ] then if [ ! -n "${pushover_user_key+x}" ] || [ ! -n "${pushover_app_key+x}" ] then echo "STOP: You're trying to setup Pushover but didn't provide your User and/or App key." echo "Define the variables like this:" echo "export pushover_user_key=put_your_userkey_here" echo "export pushover_app_key=put_your_appkey_here" exit 1 elif [ "${pushover_user_key}" = "put_your_userkey_here" ] || [ "${pushover_app_key}" = "put_your_appkey_here" ] then echo "STOP: You're trying to setup Pushover, but didn't replace the default User and App key values." exit 1 else USER_ENABLED_PUSHOVER=true echo "export pushover_enabled=true" > /root/.teslaCamPushoverCredentials echo "export pushover_user_key=$pushover_user_key" >> /root/.teslaCamPushoverCredentials echo "export pushover_app_key=$pushover_app_key" >> /root/.teslaCamPushoverCredentials fi fi } function check_available_space () { echo "Verifying that there is sufficient space available on the MicroSD card..." local available_space="$( parted -m /dev/mmcblk0 u b print free | tail -1 | cut -d ":" -f 4 | sed 's/B//g' )" if [ "$available_space" -lt 4294967296 ] then echo "STOP: The MicroSD card is too small." exit 1 fi echo "There is sufficient space available." } function get_ancillary_setup_scripts () { pushd /tmp wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/create-backingfiles-partition.sh chmod +x ./create-backingfiles-partition.sh wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/create-backingfiles.sh chmod +x ./create-backingfiles.sh wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/make-root-fs-readonly.sh chmod +x ./make-root-fs-readonly.sh popd } function fix_cmdline_txt_modules_load () { echo "Fixing the modules-load parameter in /boot/cmdline.txt..." cp /boot/cmdline.txt ~ cat ~/cmdline.txt | sed 's/[[:space:]]\+modules-load=[^ [:space:]]\+//' | sed 's/rootwait/rootwait modules-load=dwc2/' > /boot/cmdline.txt rm ~/cmdline.txt echo "Fixed cmdline.txt." } BACKINGFILES_MOUNTPOINT=/backingfiles function create_usb_drive_backing_files () { mkdir "$BACKINGFILES_MOUNTPOINT" /tmp/create-backingfiles-partition.sh "$BACKINGFILES_MOUNTPOINT" echo "Mounting the partition for the backing files..." mount /backingfiles echo "Mounted the partition for the backing files." /tmp/create-backingfiles.sh "$campercent" "$BACKINGFILES_MOUNTPOINT" } function configure_archive_scripts () { echo "Configuring the archive scripts..." mkdir /root/bin pushd /root/bin wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/archiveloop wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/archive-teslacam-clips if [ $RSYNC_ENABLE = true ] then wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/rsync_archive/archive-clips.sh wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/rsync_archive/connect-archive.sh wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/rsync_archive/disconnect-archive.sh else wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/archive-clips.sh wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/connect-archive.sh wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/disconnect-archive.sh fi chmod +x archiveloop chmod +x archive-teslacam-clips chmod +x archive-clips.sh chmod +x connect-archive.sh chmod +x disconnect-archive.sh popd echo "Configured the archive scripts." pushd /root wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/remountfs_rw chmod +x remountfs_rw popd echo "Downloaded script to remount filesystems read/write if needed (/root/remountfs_rw)." } function configure_pushover_scripts() { pushd /root/bin wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/send-pushover chmod +x send-pushover popd } function configure_rc_local () { echo "Configuring /etc/rc.local to run the archive scripts at startup..." echo "#!/bin/bash -eu" > ~/rc.local tail -n +2 /etc/rc.local | sed '$d' >> ~/rc.local cat << 'EOF' >> ~/rc.local LOGFILE=/tmp/rc.local.log function log () { echo "$( date )" >> "$LOGFILE" echo "$1" >> "$LOGFILE" } log "Launching archival script..." /root/bin/archiveloop "$archiveserver" & log "All done" exit 0 EOF cat ~/rc.local >> /etc/rc.local rm ~/rc.local echo "Configured rc.local." } function configure_hostname () { echo "Configuring the hostname..." local new_host_name="teslausb" cp /etc/hosts ~ sed "s/raspberrypi/$new_host_name/g" ~/hosts > /etc/hosts cp /etc/hostname ~ sed "s/raspberrypi/$new_host_name/g" ~/hostname > /etc/hostname echo "Configured the hostname." } function make_root_fs_readonly () { /tmp/make-root-fs-readonly.sh } echo "Verifying environment variables..." if [ ! -n "${cifs_version+x}" ] then cifs_version=3 fi if [ $RSYNC_ENABLE = true ] then check_variable "RSYNC_USER" check_variable "RSYNC_SERVER" export archiveserver=$RSYNC_SERVER check_variable "RSYNC_PATH" else # Else for now, TODO allow both for more redundancy? check_variable "sharename" check_variable "shareuser" check_variable "sharepassword" fi check_variable "archiveserver" check_variable "campercent" check_pushover_enabled pushd /root/bin if [ $RSYNC_ENABLE = true ] then wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/rsync_archive/verify-archive-configuration.sh wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/rsync_archive/configure-archive.sh else wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/verify-archive-configuration.sh wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/configure-archive.sh wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/write-archive-credentials-to.sh chmod +x write-archive-credentials-to.sh fi chmod +x verify-archive-configuration.sh chmod +x configure-archive.sh wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/get-archiveserver-ip-address.sh chmod +x get-archiveserver-ip-address.sh popd /root/bin/verify-archive-configuration.sh check_available_space get_ancillary_setup_scripts pushd ~ configure_archive_scripts configure_pushover_scripts fix_cmdline_txt_modules_load echo "" >> /etc/fstab create_usb_drive_backing_files /root/bin/configure-archive.sh configure_rc_local configure_hostname make_root_fs_readonly echo "All done."