birdcam-pi/automount.sh

57 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
vars="/home/pi/birdcam/vars"
mountpoint="$(cat $vars | grep mountpoint | cut -d'=' -f2)"
mountoptions="$(cat $vars | grep mountoptions | cut -d'=' -f2)"
device="$(cat $vars | grep usbdevice | cut -d'=' -f2)"
playersh="$(cat $vars | grep randomsh | cut -d'=' -f2)"
playerproc="$(cat $vars | grep playerproc | cut -d'=' -f2)"
if [ ! -d "$mountpoint" ]; then
sudo mkdir $mountpoint
fi
while true; do
dev_plug_state="$(lsblk | grep $device)"
dev_mount_state="$(mount | grep $device)"
if [ -n "$dev_plug_state" ] && [ -z "$dev_mount_state" ]; then
echo "Mount /dev/$device on $mountpoint with options $mountoptions"
sudo mount /dev/$device $mountpoint -o $mountoptions
echo "Done!"
elif [ -n "$dev_plug_state" ] && [ -n "$dev_mount_state" ]; then
echo "/dev/$device is already mounted on $mountpoint"
elif [ -z "$dev_plug_state" ] && [ -z "$dev_mount_state" ]; then
echo "/dev/$device not plugged in and is not mounted!"
elif [ -z "$dev_plug_state" ] && [ -n "$dev_mount_state" ]; then
echo "/dev/$device is not plugged in and is mounted on $mountpoint"
playershstat="$(pgrep $playersh)"
if [ -n "$playershstat" ]; then
echo "Terminating $playersh"
pkill $playersh
echo "Done!"
playerprocstat="$(pgrep $playerproc)"
if [ -n "$playerprocstat" ]; then
echo "Terminating $playerproc"
pkill $playerproc
echo "Done!"
fi
fi
echo "Umounting $mountpoint"
sudo umount $mountpoint
echo "Done!"
fi
sleep 1
done