manual.sh minor bug fix

Issues were occurring where manual.sh could not accept incorrect channel input.  Now, if an invalid channel is input, you will simply be diverted to your first channel.  This can be changed later in many ways, one of which is to simply kill the script; I did not choose this since going to channel 1 kind of serves as an "error report" since you didn't make it to the channel you requested.
This commit is contained in:
mutto233
2018-06-30 18:16:38 -04:00
committed by GitHub
parent e633703fb2
commit 94f4223972

View File

@@ -1,164 +1,174 @@
#!/bin/bash #!/bin/bash
# Credits: irodimus # Credits: irodimus
# file: manual.sh # file: manual.sh
#---- #----
# Simple script to change to specific channel given - triggering start / stop. # Simple script to change to specific channel given - triggering start / stop.
#---- #----
#---- #----
# To Use: # To Use:
# Run script by including the channel you'd like to run as an argument: ex. ./manual.sh 2, ./manual.sh 9 # Run script by including the channel you'd like to run as an argument: ex. ./manual.sh 2, ./manual.sh 9
# #
# Configure something (a tv remote or alexa) to trigger this script. Make sure you move this script just # Configure something (a tv remote or alexa) to trigger this script. Make sure you move this script just
# outside of the pseudo-channel directories: # outside of the pseudo-channel directories:
# ------------------- # -------------------
# -channels/ # -channels/
# --pseudo-channel_1/ # --pseudo-channel_1/
# ---startstop.sh # ---startstop.sh
# --pseudo-channel_2/ # --pseudo-channel_2/
# ---startstop.sh # ---startstop.sh
# --pseudo-channel_3/ # --pseudo-channel_3/
# ---startstop.sh # ---startstop.sh
# --manual.sh <--- on the same level as the 3 channels. # --manual.sh <--- on the same level as the 3 channels.
#---- #----
# Make sure that each channel dir ends with a "_" + an incrementing number as seen above. # Make sure that each channel dir ends with a "_" + an incrementing number as seen above.
#----BEGIN EDITABLE VARS---- #----BEGIN EDITABLE VARS----
SCRIPT_TO_EXECUTE='startstop.sh' SCRIPT_TO_EXECUTE='startstop.sh'
OUTPUT_PREV_CHANNEL_PATH=. OUTPUT_PREV_CHANNEL_PATH=.
OUTPUT_PREV_CHANNEL_FILE=".prevplaying" OUTPUT_PREV_CHANNEL_FILE=".prevplaying"
CHANNEL_DIR_INCREMENT_SYMBOL="_" CHANNEL_DIR_INCREMENT_SYMBOL="_"
#----END EDITABLE VARS------- #----END EDITABLE VARS-------
FIRST_RUN=false FIRST_RUN=false
# Scan the dir to see how many channels there are, store them in an arr. # Scan the dir to see how many channels there are, store them in an arr.
CHANNEL_DIR_ARR=( $(find . -maxdepth 1 -type d -name '*'"$CHANNEL_DIR_INCREMENT_SYMBOL"'[[:digit:]]*' -printf "%P\n" | sort -t"$CHANNEL_DIR_INCREMENT_SYMBOL" -n) ) CHANNEL_DIR_ARR=( $(find . -maxdepth 1 -type d -name '*'"$CHANNEL_DIR_INCREMENT_SYMBOL"'[[:digit:]]*' -printf "%P\n" | sort -t"$CHANNEL_DIR_INCREMENT_SYMBOL" -n) )
# Since leading zeros may be an issue, we need to correctly sort the channels. The best way to do this seems to be in python # Since leading zeros may be an issue, we need to correctly sort the channels. The best way to do this seems to be in python
# So a script will take in the channels as they are, then output them in the correct, sorted order in Channels_Sorted.txt. # So a script will take in the channels as they are, then output them in the correct, sorted order in Channels_Sorted.txt.
# We will run the script, then read in the results. # We will run the script, then read in the results.
sudo python ./Channel_Sorter.py ${CHANNEL_DIR_ARR[@]} sudo python ./Channel_Sorter.py ${CHANNEL_DIR_ARR[@]}
filename="./Channels_Sorted.txt" filename="./Channels_Sorted.txt"
i=0 i=0
while read -r line while read -r line
do do
name="$line" name="$line"
CHANNEL_DIR_SORTED[i]=$name CHANNEL_DIR_SORTED[i]=$name
i=$((i+1)) i=$((i+1))
done < "$filename" done < "$filename"
# We need to add on top of this a "buffer" where we remove all leading zeros to compare everything on the same level # We need to add on top of this a "buffer" where we remove all leading zeros to compare everything on the same level
# This simply leaves us with the number at the end. NOTE: We should have already sorted things, so this should not be a problem # This simply leaves us with the number at the end. NOTE: We should have already sorted things, so this should not be a problem
for i in "${!CHANNEL_DIR_ARR[@]}" for i in "${!CHANNEL_DIR_ARR[@]}"
do do
CHANNEL_DIR_NUMBERS[i]=$(echo ${CHANNEL_DIR_SORTED[i]} | sed "s/^pseudo-channel${CHANNEL_DIR_INCREMENT_SYMBOL}0*//") CHANNEL_DIR_NUMBERS[i]=$(echo ${CHANNEL_DIR_SORTED[i]} | sed "s/^pseudo-channel${CHANNEL_DIR_INCREMENT_SYMBOL}0*//")
if [ -z ${CHANNEL_DIR_NUMBERS[i]} ]; then if [ -z ${CHANNEL_DIR_NUMBERS[i]} ]; then
CHANNEL_DIR_NUMBERS[i]=0 CHANNEL_DIR_NUMBERS[i]=0
fi fi
done done
# If the previous channel txt file doesn't exist already create it (first run?) # If the previous channel txt file doesn't exist already create it (first run?)
if [ ! -e "$OUTPUT_PREV_CHANNEL_PATH/$OUTPUT_PREV_CHANNEL_FILE" ]; then if [ ! -e "$OUTPUT_PREV_CHANNEL_PATH/$OUTPUT_PREV_CHANNEL_FILE" ]; then
#FIRST_RUN_NUM=$((${#CHANNEL_DIR_ARR[@]})) #FIRST_RUN_NUM=$((${#CHANNEL_DIR_ARR[@]}))
echo 1 > "$OUTPUT_PREV_CHANNEL_PATH/$OUTPUT_PREV_CHANNEL_FILE" echo 1 > "$OUTPUT_PREV_CHANNEL_PATH/$OUTPUT_PREV_CHANNEL_FILE"
echo "First run being conducted" echo "First run being conducted"
FIRST_RUN=true FIRST_RUN=true
fi fi
# If this script see's there are multiple channels, # If this script see's there are multiple channels,
# then read file, get prevchannel and nextchannel, and trigger next channel: # then read file, get prevchannel and nextchannel, and trigger next channel:
if [ "${#CHANNEL_DIR_ARR[@]}" -gt 1 ]; then if [ "${#CHANNEL_DIR_ARR[@]}" -gt 1 ]; then
echo "+++++ There are ${#CHANNEL_DIR_ARR[@]} channels detected." echo "+++++ There are ${#CHANNEL_DIR_ARR[@]} channels detected."
#NEXT_CHANNEL=$1 #NEXT_CHANNEL=$1
#NEXT_CHANNEL_DIR=( $(find . -maxdepth 1 -type d -name '*'"$CHANNEL_DIR_INCREMENT_SYMBOL""$NEXT_CHANNEL" -printf "%P\n") ) #NEXT_CHANNEL_DIR=( $(find . -maxdepth 1 -type d -name '*'"$CHANNEL_DIR_INCREMENT_SYMBOL""$NEXT_CHANNEL" -printf "%P\n") )
# We now need to read in the next channel and ALSO strip it of any leading zeros for correct comparison # We now need to read in the next channel and ALSO strip it of any leading zeros for correct comparison
NEXT_CHANNEL=$(echo $1 | sed "s/^0*//") NEXT_CHANNEL=$(echo $1 | sed "s/^0*//")
if [ -z $NEXT_CHANNEL ]; then if [ -z $NEXT_CHANNEL ]; then
NEXT_CHANNEL=0 NEXT_CHANNEL=0
fi fi
PREV_CHANNEL_FOUND=false PREV_CHANNEL_FOUND=false
#PREV_CHANNEL=$(<$OUTPUT_PREV_CHANNEL_PATH/$OUTPUT_PREV_CHANNEL_FILE) #PREV_CHANNEL=$(<$OUTPUT_PREV_CHANNEL_PATH/$OUTPUT_PREV_CHANNEL_FILE)
# PREV_CHANNEL_DIR=( $(find . -maxdepth 1 -type d -name '*'"$CHANNEL_DIR_INCREMENT_SYMBOL""$PREV_CHANNEL" -printf "%P\n") ) # PREV_CHANNEL_DIR=( $(find . -maxdepth 1 -type d -name '*'"$CHANNEL_DIR_INCREMENT_SYMBOL""$PREV_CHANNEL" -printf "%P\n") )
# We are now going to do the same thing here, just with previous channel # We are now going to do the same thing here, just with previous channel
PREV_CHANNEL=$(echo $(<$OUTPUT_PREV_CHANNEL_PATH/$OUTPUT_PREV_CHANNEL_FILE) | sed "s/^0*//") PREV_CHANNEL=$(echo $(<$OUTPUT_PREV_CHANNEL_PATH/$OUTPUT_PREV_CHANNEL_FILE) | sed "s/^0*//")
if [ -z $PREV_CHANNEL ]; then if [ -z $PREV_CHANNEL ]; then
PREV_CHANNEL=0 PREV_CHANNEL=0
fi fi
echo "+++++ It looks like the previous channel was: $PREV_CHANNEL" echo "+++++ It looks like the previous channel was: $PREV_CHANNEL"
echo "+++++ The next channel is: $NEXT_CHANNEL" echo "+++++ The next channel is: $NEXT_CHANNEL"
# This is our modified way of searching for the correct directory for the next channel # This is our modified way of searching for the correct directory for the next channel
for i in "${!CHANNEL_DIR_NUMBERS[@]}" for i in "${!CHANNEL_DIR_NUMBERS[@]}"
do do
item_compare=${CHANNEL_DIR_NUMBERS[i]} item_compare=${CHANNEL_DIR_NUMBERS[i]}
if [ $item_compare -eq $NEXT_CHANNEL ]; then if [ $item_compare -eq $NEXT_CHANNEL ]; then
echo "NEXT CHANNEL MATCH: ${CHANNEL_DIR_SORTED[$i]}" echo "NEXT CHANNEL MATCH: ${CHANNEL_DIR_SORTED[$i]}"
NEXT_CHANNEL_DIR=${CHANNEL_DIR_SORTED[$i]} NEXT_CHANNEL_DIR=${CHANNEL_DIR_SORTED[$i]}
break break
fi fi
done
#echo "TESTING"
# This is our modified way of searching for the correct directory for the previous channel #echo $i
for i in "${!CHANNEL_DIR_NUMBERS[@]}" #echo ${#CHANNEL_DIR_NUMBERS[@]}
do if [ $i -eq $((${#CHANNEL_DIR_NUMBERS[@]} - 1)) ]; then
item_compare=${CHANNEL_DIR_NUMBERS[i]} echo "No NEXT CHANNEL MATCH found, reverting to first element"
if [ $item_compare -eq $PREV_CHANNEL ]; then NEXT_CHANNEL_DIR=${CHANNEL_DIR_SORTED[0]}
echo "PREVIOUS CHANNEL MATCH: ${CHANNEL_DIR_SORTED[$i]}" NEXT_CHANNEL=${CHANNEL_DIR_NUMBERS[0]}
PREV_CHANNEL_DIR=${CHANNEL_DIR_SORTED[$i]} fi
break
fi done
done
# This is our modified way of searching for the correct directory for the previous channel
for i in "${!CHANNEL_DIR_NUMBERS[@]}"
# Write next channel to previous channel file to reference later do
echo "$NEXT_CHANNEL" > "$OUTPUT_PREV_CHANNEL_PATH/$OUTPUT_PREV_CHANNEL_FILE" item_compare=${CHANNEL_DIR_NUMBERS[i]}
if [ $item_compare -eq $PREV_CHANNEL ]; then
# Finally let's trigger the startstop script in both the previous channel and the next channel dirs. echo "PREVIOUS CHANNEL MATCH: ${CHANNEL_DIR_SORTED[$i]}"
# This will stop the previous channels playback & trigger the next channels playback PREV_CHANNEL_DIR=${CHANNEL_DIR_SORTED[$i]}
break
if [ "$FIRST_RUN" = false ]; then fi
cd "$OUTPUT_PREV_CHANNEL_PATH"/"$PREV_CHANNEL_DIR" && ./"$SCRIPT_TO_EXECUTE" done
cd ../"$NEXT_CHANNEL_DIR" && ./"$SCRIPT_TO_EXECUTE"
else
# Write next channel to previous channel file to reference later
cd "$OUTPUT_PREV_CHANNEL_PATH"/"$NEXT_CHANNEL_DIR" && ./"$SCRIPT_TO_EXECUTE" echo "$NEXT_CHANNEL" > "$OUTPUT_PREV_CHANNEL_PATH/$OUTPUT_PREV_CHANNEL_FILE"
fi # Finally let's trigger the startstop script in both the previous channel and the next channel dirs.
# This will stop the previous channels playback & trigger the next channels playback
sleep 1
if [ "$FIRST_RUN" = false ]; then
cd "$OUTPUT_PREV_CHANNEL_PATH"/"$PREV_CHANNEL_DIR" && ./"$SCRIPT_TO_EXECUTE"
fi cd ../"$NEXT_CHANNEL_DIR" && ./"$SCRIPT_TO_EXECUTE"
else
cd "$OUTPUT_PREV_CHANNEL_PATH"/"$NEXT_CHANNEL_DIR" && ./"$SCRIPT_TO_EXECUTE"
fi
sleep 1
fi
exit 0 exit 0