Use function for all prompts

This commit is contained in:
paukstelis
2021-04-16 15:52:28 -04:00
parent 47b8e5752c
commit 6fa8c2a12d

View File

@@ -5,6 +5,18 @@ if (( $EUID != 0 )); then
exit exit
fi fi
# from stackoverflow.com/questions/3231804
prompt_confirm() {
while true; do
read -r -n 1 -p "${1:-Continue?} [y/n]: " REPLY
case $REPLY in
[yY]) echo ; return 0 ;;
[nN]) echo ; return 1 ;;
*) printf " \033[31m %s \n\033[0m" "invalid input"
esac
done
}
if [ $SUDO_USER ]; then user=$SUDO_USER; fi if [ $SUDO_USER ]; then user=$SUDO_USER; fi
SCRIPTDIR=$(dirname $(readlink -f $0)) SCRIPTDIR=$(dirname $(readlink -f $0))
PIDEFAULT="/home/$user/oprint/bin/octoprint" PIDEFAULT="/home/$user/oprint/bin/octoprint"
@@ -115,10 +127,7 @@ if grep -q 'firstRun: true' $BFOLD/config.yaml; then
exit 1 exit 1
fi fi
read -p "Begin auto-detect printer serial number for udev entry? (y/n)" -n 1 -r prompt_confirm "Begin auto-detect printer serial number for udev entry?" || echo "OK. Restart when you are ready"; exit 0
echo #new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo echo
#clear out journalctl - probably a better way to do this #clear out journalctl - probably a better way to do this
journalctl --rotate > /dev/null 2>&1 journalctl --rotate > /dev/null 2>&1
@@ -130,29 +139,26 @@ then
TEMPUSB=$(timeout 1s journalctl -kf | sed -n -e 's/^.*\(cdc_acm\|ftdi_sio\|ch341\) \([0-9].*[0-9]\): \(tty.*\|FTD.*\|ch341-uart.*\).*/\2/p') TEMPUSB=$(timeout 1s journalctl -kf | sed -n -e 's/^.*\(cdc_acm\|ftdi_sio\|ch341\) \([0-9].*[0-9]\): \(tty.*\|FTD.*\|ch341-uart.*\).*/\2/p')
counter=$(( $counter + 1 )) counter=$(( $counter + 1 ))
done done
fi #fi
if [ -z "$UDEV" ]; then if [ -z "$UDEV" ]; then
echo "Printer Serial Number not detected" echo "Printer Serial Number not detected"
read -p "Do you want to use the physical USB port to assign the udev entry? If you use this any USB hubs and printers detected this way must stay plugged into the same USB positions on your machine as they are right now (y/n)." -n 1 -r prompt_confirm "Do you want to use the physical USB port to assign the udev entry? If you use this any USB hubs and printers detected this way must stay plugged into the same USB positions on your machine as they are right now" || echo "OK. Please try again."; exit 0
if [[ $REPLY =~ ^[Yy]$ ]]; then #if [[ $REPLY =~ ^[Yy]$ ]]; then
echo echo
USB=$TEMPUSB USB=$TEMPUSB
echo "Your printer will be setup at the following usb address:" echo "Your printer will be setup at the following usb address:"
echo $USB echo $USB
echo echo
else
echo "You are welcome to try again"
exit 1
fi
else else
echo "Serial number detected as: $UDEV" echo "Serial number detected as: $UDEV"
fi fi
echo echo
#Octobuntu cameras
#USB cameras
if [[ -n $INSTALL ]]; then if [[ -n $INSTALL ]]; then
read -p "Would you like to auto detect an associated USB camera (experimental; y/n)?" -n 1 -r if prompt_confirm "Would you like to auto detect an associated USB camera (experimental)?"
if [[ $REPLY =~ ^[Yy]$ ]]
then then
echo echo
#clear out journalctl - probably a better way to do this #clear out journalctl - probably a better way to do this
@@ -186,9 +192,8 @@ if [[ -n $INSTALL ]]; then
fi fi
fi fi
echo echo
read -p "Ready to write all changes. Do you want to proceed? " -n 1 -r
echo if prompt_confirm "Ready to write all changes. Do you want to proceed?"
if [[ $REPLY =~ ^[Yy]$ ]];
then then
cat $SCRIPTDIR/octoprint_generic.service | \ cat $SCRIPTDIR/octoprint_generic.service | \
sed -e "s/OCTOUSER/$OCTOUSER/" \ sed -e "s/OCTOUSER/$OCTOUSER/" \