Files
teslausb/windows_archive/setup-teslausb
2018-10-16 12:48:07 -05:00

149 lines
4.4 KiB
Bash

#!/bin/bash -eu
TESLAUSB_REPO=rtgoodwin
TESLAUSB_BRANCH=master
if [ "$(whoami)" != "root" ]
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
}
check_variable "archiveserver"
check_variable "sharename"
check_variable "shareuser"
check_variable "sharepassword"
check_variable "campercent"
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
echo "export pushover_enabled=true" > /root/bin/.teslaCamPushoverCredentials
echo "export pushover_user_key=$pushover_user_key" >> /root/.teslaCamPushoverCredentials
echo "export pushover_app_key=$pushover_app_key" >> /root/.teslaCamPushoverCredentials
fi
fi
}
serverunreachable=false
ping -c 1 -w 1 "$archiveserver" 1>/dev/null 2>&1 || serverunreachable=true
if [ "$serverunreachable" = true ]
then
echo "STOP: The archive server $archiveserver is unreachable. Try specifying its IP address instead."
exit 1
fi
archiveserverip="$(getent hosts $archiveserver | cut -d' ' -f1)"
available_space="$(($(df --output=avail / | tail -1) - 1000000))"
if [ "$available_space" -lt 0 ]
then
echo "STOP: The MicroSD card is too small."
exit 1
fi
function add_drive () {
local name="$1"
local label="$2"
local size="$3"
local filename="$4"
fallocate -l "$size"K "$filename"
mkfs.vfat "$filename" -F 32 -n "$label"
local mountpoint=/mnt/"$name"
mkdir "$mountpoint"
echo "$filename $mountpoint vfat noauto,users,umask=000 0 0" >> /etc/fstab
}
pushd ~
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
mkdir /mnt/archive
echo "" >> /etc/fstab
echo "//$archiveserverip/$sharename /mnt/archive cifs vers=3,credentials=/root/.teslaCamArchiveCredentials,iocharset=utf8,file_mode=0777,dir_mode=0777 0" >> /etc/fstab
echo "username=$shareuser" > /root/.teslaCamArchiveCredentials
echo "password=$sharepassword" >> /root/.teslaCamArchiveCredentials
mkdir /root/bin
wget https://raw.githubusercontent.com/"$TESLAUSB_REPO"/teslausb/"$TESLAUSB_BRANCH"/windows_archive/archiveloop
sed s/ARCHIVE_HOST_NAME=archiveserver/ARCHIVE_HOST_NAME=$archiveserver/ ~/archiveloop > /root/bin/archiveloop
rm ~/archiveloop
chmod +x /root/bin/archiveloop
pushd /root/bin
wget https://raw.githubusercontent.com/"$TESLAUSB_REPO"/teslausb/"$TESLAUSB_BRANCH"/windows_archive/archive-teslacam-clips
chmod +x archive-teslacam-clips
popd
check_pushover_enabled
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 &
log "All done"
exit 0
EOF
cat ~/rc.local > /etc/rc.local
rm ~/rc.local
cam_disk_size="$(( $available_space * $campercent / 100 ))"
cam_disk_file_name="/cam_disk.bin"
add_drive "cam" "CAM" "$cam_disk_size" "$cam_disk_file_name"
if [ "$campercent" -lt 100 ]
then
musicpercent="$(( 100 - $campercent ))"
music_disk_size="$(( $available_space * $musicpercent / 100 ))"
music_disk_file_name="/music_disk.bin"
add_drive "music" "MUSIC" "$music_disk_size" "$music_disk_file_name"
echo "options g_mass_storage file=$cam_disk_file_name,$music_disk_file_name removable=1,1 ro=0,0 stall=0 iSerialNumber=123456" > /etc/modprobe.d/g_mass_storage.conf
else
echo "options g_mass_storage file=$cam_disk_file_name removable=1 ro=0 stall=0 iSerialNumber=123456" > /etc/modprobe.d/g_mass_storage.conf
fi
cp /etc/hosts ~
sed s/raspberrypi/teslausb/g ~/hosts > /etc/hosts
cp /etc/hostname ~
sed s/raspberrypi/teslausb/g ~/hostname > /etc/hostname