2024-11-19 23:01:22 +02:00
#!/bin/bash
2024-11-20 15:48:35 +02:00
# Set the mount point
2024-11-29 16:25:32 +02:00
mount_point = "/mnt/usb"
2024-11-19 23:01:22 +02:00
2024-11-20 15:48:35 +02:00
# Function to log both to the screen and syslog
2024-11-19 23:01:22 +02:00
log_message( ) {
2024-11-20 15:48:35 +02:00
local msg = " USB config: $1 "
2024-11-19 23:01:22 +02:00
echo " $msg " # Echo to the screen
2024-11-20 15:48:35 +02:00
logger " USB config: $msg " # Log to the system log
2024-11-19 23:01:22 +02:00
}
2024-11-20 15:48:35 +02:00
#Blink
2024-11-19 23:01:22 +02:00
blink( ) {
2024-11-20 15:48:35 +02:00
echo 1 > /sys/class/gpio/gpio34/value;
sleep " $1 " ;
echo 0 > /sys/class/gpio/gpio34/value;
2024-11-19 23:01:22 +02:00
}
escape_sed( ) {
2024-11-20 15:48:35 +02:00
echo " $1 " | sed -e 's/[]\/$*.^[]/\\&/g'
2024-11-19 23:01:22 +02:00
}
2024-11-20 15:48:35 +02:00
# Check if the mount point exists and if a USB drive is plugged in
2024-11-29 16:25:32 +02:00
usb_device = $( lsblk -o NAME,FSTYPE,SIZE,TYPE,MOUNTPOINT | grep -E "vfat|ext4|ntfs|exfat" | grep -E "sd[a-z][0-9]" | awk '{print $1}' | sed 's/[^a-zA-Z0-9]//g' | head -n 1)
2024-11-20 15:48:35 +02:00
2024-11-29 16:25:32 +02:00
if [ -d " $mount_point " ] ; then
sudo rmdir " $mount_point "
2024-11-19 23:01:22 +02:00
log_message "/mnt/usb deleted."
fi
2024-11-20 15:48:35 +02:00
# If no USB device is found, exit
2024-11-29 16:25:32 +02:00
if [ -z " $usb_device " ] ; then
2024-11-19 23:01:22 +02:00
log_message "No USB drive found."
exit 0
fi
# Create the mount point if it doesn't exist
2024-11-29 16:25:32 +02:00
if [ ! -d " $mount_point " ] ; then
sudo mkdir -p " $mount_point "
2024-11-19 23:01:22 +02:00
fi
# Construct the full device path
2024-11-29 16:25:32 +02:00
usb_device = " /dev/ $usb_device "
2024-11-19 23:01:22 +02:00
2024-11-20 15:48:35 +02:00
# Debugging: Log and echo the extracted device name
2024-11-29 16:25:32 +02:00
log_message " Extracted device name: $usb_device "
2024-11-19 23:01:22 +02:00
# Check if the USB drive is already mounted
2024-11-29 16:25:32 +02:00
if mount | grep " $usb_device " > /dev/null; then
2024-11-19 23:01:22 +02:00
log_message "USB drive is already mounted."
else
# Mount the USB drive to the specified mount point
2024-11-29 16:25:32 +02:00
sudo mount " $usb_device " " $mount_point "
2024-11-19 23:01:22 +02:00
if [ $? -eq 0 ] ; then
2024-11-29 16:25:32 +02:00
log_message " USB drive mounted successfully at $mount_point . "
2024-11-19 23:01:22 +02:00
else
log_message "Failed to mount USB drive."
blink "4" && sleep "0.5"
exit 1
fi
fi
2024-11-29 16:25:32 +02:00
wpa_supplicant_conf = "/etc/wpa_supplicant/wpa_supplicant.conf"
usb_config = "/tmp/femtofox-config.txt"
2024-11-22 14:48:41 +02:00
2024-11-20 15:48:35 +02:00
# Check if the mounted USB drive contains a file femtofox-config.txt
2024-11-29 16:25:32 +02:00
if [ -f " $mount_point /femtofox-config.txt " ] ; then
2024-11-19 23:01:22 +02:00
log_message "femtofox-config.txt found on USB drive."
2024-11-29 16:25:32 +02:00
# Remove Windows-style carriage returns and save a temporary copy of femtofox-config.txt
tr -d '\r' < " $mount_point /femtofox-config.txt " > $usb_config
2024-11-19 23:01:22 +02:00
2024-11-20 15:48:35 +02:00
# Initialize variables
2024-11-29 16:25:32 +02:00
wifi_ssid = ""
wifi_psk = ""
wifi_country = ""
2024-12-01 21:35:10 +02:00
meshtastic_lora_radio = ""
2024-11-29 16:25:32 +02:00
found_config = "false"
update_wifi = "false"
2024-12-01 21:35:10 +02:00
update_meshtastic = ""
# Escape and read the fields from the USB config file if they exist
while IFS = '=' read -r key value; do
value = $( echo " $value " | tr -d '"' )
case " $key " in
wifi_ssid) wifi_ssid = $( escape_sed " $value " ) ; ;
wifi_psk) wifi_psk = $( escape_sed " $value " ) ; ;
wifi_country) wifi_country = $( escape_sed " $value " ) ; ;
meshtastic_lora_radio) meshtastic_lora_radio = $( escape_sed " $value " ) ; ;
timezone) timezone = $( escape_sed " $value " ) ; ;
meshtastic_url) meshtastic_url = $( escape_sed " $value " ) ; ;
meshtastic_legacy_admin) meshtastic_legacy_admin = $( escape_sed " $value " ) ; ;
meshtastic_admin_key) meshtastic_admin_key = $( escape_sed " $value " ) ; ;
esac
done < <( grep -E '^(wifi_ssid|wifi_psk|wifi_country|meshtastic_lora_radio|timezone|meshtastic_url|meshtastic_legacy_admin|meshtastic_admin_key)=' " $usb_config " )
2024-11-19 23:01:22 +02:00
2024-11-29 16:25:32 +02:00
# Update wpa_supplicant.conf with the new values, if specified
if [ [ -n " $wifi_country " ] ] ; then
2024-12-01 21:35:10 +02:00
# Update or add the country field
if grep -q "^country=" " $wpa_supplicant_conf " ; then
sed -i " s/^country=.*/country= $wifi_country / " " $wpa_supplicant_conf "
log_message " Updated Wi-Fi country in wpa_supplicant.conf to $wifi_country . "
else
echo " country= $wifi_country " >> " $wpa_supplicant_conf "
log_message " Added Wi-Fi country to wpa_supplicant.conf as $wifi_country . "
fi
2024-11-29 16:25:32 +02:00
found_config = "true"
update_wifi = "true"
2024-11-19 23:01:22 +02:00
fi
2024-12-01 21:35:10 +02:00
2024-11-29 16:25:32 +02:00
if [ [ -n " $wifi_ssid " ] ] ; then
2024-12-01 21:35:10 +02:00
# Update the ssid in the network block
sed -i " /ssid=/s/\".*\"/\" $wifi_ssid \"/ " " $wpa_supplicant_conf "
log_message " Updated Wi-Fi SSID in wpa_supplicant.conf to $wifi_ssid . "
2024-11-29 16:25:32 +02:00
found_config = "true"
update_wifi = "true"
2024-11-19 23:01:22 +02:00
fi
2024-12-01 21:35:10 +02:00
2024-11-29 16:25:32 +02:00
if [ [ -n " $wifi_psk " ] ] ; then
2024-12-01 21:35:10 +02:00
# Update the psk in the network block
sed -i " /psk=/s/\".*\"/\" $wifi_psk \"/ " " $wpa_supplicant_conf "
log_message "Updated Wi-Fi PSK in wpa_supplicant.conf."
2024-11-29 16:25:32 +02:00
found_config = "true"
update_wifi = "true"
2024-11-29 00:35:20 +02:00
fi
2024-12-01 21:35:10 +02:00
#get meshtastic_lora_radio model, if specified, and copy appropriate yaml to /etc/meshtasticd/config.d/
if [ [ -n " $meshtastic_lora_radio " ] ] ; then
2024-11-29 00:35:20 +02:00
rm -f /etc/meshtasticd/config.d/femtofox*
2024-11-29 16:25:32 +02:00
found_config = "true"
2024-12-01 21:35:10 +02:00
meshtastic_lora_radio = $( echo " $meshtastic_lora_radio " | tr '[:upper:]' '[:lower:]' )
case " $meshtastic_lora_radio " in
2024-11-29 00:35:20 +02:00
'ebyte-e22-900m30s' )
cp /etc/meshtasticd/available.d/femtofox_EByte-E22-900M30S_Ebyte-E22-900M22S.yaml /etc/meshtasticd/config.d
; ;
'ebyte-e22-900m22s' )
cp /etc/meshtasticd/available.d/femtofox_EByte-E22-900M30S_Ebyte-E22-900M22S.yaml /etc/meshtasticd/config.d
; ;
'e22-900mm22s' )
cp /etc/meshtasticd/available.d/femtofox_EByte-E22-900MM22S.yaml /etc/meshtasticd/config.d
; ;
'heltec-ht-ra62' )
cp /etc/meshtasticd/available.d/femtofox_Heltec-HT-RA62_Seeed-WIO-SX1262.yaml /etc/meshtasticd/config.d
; ;
'seeed-wio-sx1262' )
cp /etc/meshtasticd/available.d/femtofox_Heltec-HT-RA62_Seeed-WIO-SX1262.yaml /etc/meshtasticd/config.d
; ;
'waveshare-sx126x-xxxm' )
cp /etc/meshtasticd/available.d/femtofox_Waveshare-SX126X-XXXM_AI-Thinker-RA-01SH.yaml /etc/meshtasticd/config.d
; ;
'ai-thinker-ra-01sh' )
cp /etc/meshtasticd/available.d/femtofox_Waveshare-SX126X-XXXM_AI-Thinker-RA-01SH.yaml /etc/meshtasticd/config.d
; ;
2024-12-01 21:35:10 +02:00
'none' )
; ;
2024-11-29 00:35:20 +02:00
*)
2024-12-01 21:35:10 +02:00
log_message " Invalid LoRa radio name: $meshtastic_lora_radio , ignoring. "
2024-11-29 16:25:32 +02:00
found_config = "false"
2024-11-29 00:35:20 +02:00
; ;
esac
2024-11-29 16:25:32 +02:00
if [ " $found_config " = "true" ] ; then
2024-11-29 00:35:20 +02:00
systemctl restart meshtasticd
2024-12-01 21:35:10 +02:00
log_message " Set LoRa radio to $meshtastic_lora_radio , restarting Meshtasticd and proceeding. "
2024-11-29 00:35:20 +02:00
fi
2024-11-19 23:01:22 +02:00
fi
2024-11-29 16:25:32 +02:00
if [ [ -n " $timezone " ] ] ; then
#sed -i "/timezone=/s/\".*\"/\"$timezone\"/" "$wpa_supplicant_conf"
timezone = $( echo " $timezone " | sed 's/\\//g' )
log_message " Updating system timezone to $timezone . "
2024-11-30 17:37:23 +02:00
rm /etc/localtime
ln -sf /usr/share/zoneinfo/$timezone /etc/localtime
2024-11-29 16:25:32 +02:00
found_config = "true"
fi
2024-12-01 21:35:10 +02:00
if [ [ -n " $meshtastic_url " ] ] ; then
meshtastic_url = $( echo " $meshtastic_url " | sed 's/\\//g' )
log_message " Updating Meshtastic URL to $meshtastic_url . "
found_config = "true"
update_meshtastic = " --seturl $meshtastic_url "
fi
if [ [ -n " $meshtastic_admin_key " ] ] ; then
if [ " $update_meshtastic " = "" ] ; then
meshtastic_admin_key = $( echo " $meshtastic_admin_key " | sed 's/\\//g' )
log_message " Updating Meshtastic admin key to $meshtastic_admin_key . "
found_config = "true"
update_meshtastic = " --set security.admin_key $meshtastic_admin_key "
else
log_message "meshtastic_admin_key: Cannot update Meshtastic URL and security settings in single operation. Remove Meshtastic URL from femtofox-config.txt to make changes to security settings. Ignoring..."
fi
fi
if [ [ -n " $meshtastic_legacy_admin " ] ] ; then
if [ " $update_meshtastic " = "" ] || [ [ " $update_meshtastic " = = *security* ] ] ; then
meshtastic_legacy_admin = $( echo " $meshtastic_legacy_admin " | sed 's/\\//g' )
log_message " Updating Meshtastic legacy admin to $meshtastic_legacy_admin . "
found_config = "true"
update_meshtastic += " --set security.admin_channel_enabled $meshtastic_legacy_admin "
else
log_message "meshtastic_legacy_admin: Cannot update Meshtastic URL and security settings in single operation. Remove Meshtastic URL from femtofox-config.txt to make changes to security settings. Ignoring..."
fi
fi
2024-11-29 00:35:20 +02:00
2024-11-29 16:25:32 +02:00
if [ " $found_config " = true ] ; then #if we found a config file containing valid data
if [ " $update_wifi " = true ] ; then #if wifi config found, restart wifi
2024-11-29 00:35:20 +02:00
sudo systemctl restart wpa_supplicant
sudo wpa_cli -i wlan0 reconfigure
2024-11-29 16:25:32 +02:00
log_message "wpa_supplicant.conf updated and wifi restarted, proceeding."
2024-11-29 00:35:20 +02:00
fi
2024-11-29 16:25:32 +02:00
2024-12-01 21:35:10 +02:00
if [ " $update_meshtastic " != "" ] ; then
log_message " Connecting to Meshtastic radio and submitting $update_meshtastic "
meshtastic --host $update_meshtastic
fi
for _ in { 1..10} ; do #do our successful config boot code
blink "0.125" && sleep 0.125
done
2024-11-29 16:25:32 +02:00
else #if no valid data in config file
2024-11-19 23:01:22 +02:00
log_message "femtofox-config.txt does not contain valid configuration info, ignoring."
for _ in { 1..5} ; do
blink "1.5" && sleep 0.5
done
2024-12-01 21:35:10 +02:00
exit 1
2024-11-19 23:01:22 +02:00
fi
2024-11-20 15:48:35 +02:00
2024-11-19 23:01:22 +02:00
else
log_message "USB drive mounted but femtofox-config.txt not found, ignoring."
for _ in { 1..3} ; do
blink "1.5" && sleep 0.5
done
2024-12-01 21:35:10 +02:00
exit 1
2024-11-20 02:32:52 +02:00
fi
2024-12-01 21:35:10 +02:00
rm $usb_config #remove temporary copy of femtofox-config.txt
exit 0