Share your bash scripts!

Share your bash scripts, mine is:

#!/bin/bash

while getopts "sShD:" flag; do
  case $flag in
    s) # silent mode
    TWITCHONLINE_SILENT_MODE='thisisdoesntmeananythingsinceitonlychecksifthevariableexistssoimjustgoingtotellyoutowatchaZentreyastream'
    ;;
    h) # show help
    self_disable=TRUE
    echo "twitchonline: check if a twitch streamer is online
    -h            : show this help
    -S [streamer] : setup automatic stream checking for [streamer], see this apps code to change timing
    -D [streamer] : remove systemd units for automatic checking for [streamer]"
    ;;
    S) # generate files
      self_disable=TRUE
      printf "[Unit]\nDescription=\"Execute twitchonline and check for $2\"\n[Service]\nType=oneshot\nExecStart=/bin/bash $HOME/.local/bin/twitchonline.sh $2\n[Install]\nWantedBy=twitchonline_$2.timer" > $HOME/.local/share/systemd/user/twitchonline_$2.service
      #TODO add support to add custom times
      printf "[Unit]\nDescription=Run every 15 minutes from 10:30 AM to 8:30 PM\n[Timer]\nOnCalendar=10:30\nOnCalendar=10:45\nOnCalendar=11:00\nOnCalendar=11:15\nOnCalendar=11:30\nOnCalendar=11:45\nOnCalendar=12:00\nOnCalendar=12:15\nOnCalendar=12:30\nOnCalendar=12:45\nOnCalendar=13:00\nOnCalendar=13:15\nOnCalendar=13:30\nOnCalendar=13:45\nOnCalendar=14:00\nOnCalendar=14:15\nOnCalendar=14:30\nOnCalendar=14:45\nOnCalendar=15:00\nOnCalendar=15:15\nOnCalendar=15:30\nOnCalendar=15:45\nOnCalendar=16:00\nOnCalendar=16:15\nOnCalendar=16:30\nOnCalendar=16:45\nOnCalendar=17:00\nOnCalendar=17:15\nOnCalendar=17:30\nOnCalendar=17:45\nOnCalendar=18:00\nOnCalendar=18:15\nOnCalendar=18:30\nOnCalendar=18:45\nOnCalendar=19:00\nOnCalendar=19:15\nOnCalendar=19:30\nOnCalendar=19:45\nOnCalendar=20:00\nOnCalendar=20:15\nAccuracySec=1min\nUnit=twitchonline_$2.service\n[Install]\nWantedBy=timers.target" > $HOME/.local/share/systemd/user/twitchonline_$2.timer
      systemctl --user enable twitchonline_$2.timer
      systemctl --user enable twitchonline_$2.service
      systemctl --user daemon-reload
    ;;
    D) # delete files
      self_disable=TRUE
      systemctl --user disable twitchonline_$2.timer
      systemctl --user disable twitchonline_$2.service
      systemctl --user daemon-reload
      rm $HOME/.local/share/systemd/user/twitchonline_$2.timer
      rm $HOME/.local/share/systemd/user/twitchonline_$2.service
      
    ;;
    \?)
    echo "twitchonline: invalid option, see help for instructions"
    ;;
  esac
done

function toconsole() {
  if [ -z "${TWITCHONLINE_SILENT_MODE}" ]; then
    echo $1
  fi
}


if [ -z "${self_disable}" ]; then
  todaysDateNumber=$(date '+%j')
  sloc="$(dirname "$(readlink -f $0)")"
  doesExist=$(cat "$sloc/$1stream_state.txt" | grep -i -m 1 "$todaysDateNumber")
  if [ -z "${doesExist}" ]; then
    if ! grep "busy" "$sloc/$1prog_state.txt"; then
      echo "busy" >> "$sloc/$1prog_state.txt"
      wget "https://twitchtracker.com/$1" -O "$sloc/$1streamer.html" > /dev/null 2>&1
      if grep 'LIVE</span>' "$sloc/$1streamer.html"; then
          toconsole "stream found."
          if [ "$(notify-send "$1 is online!" "https://www.twitch.tv/$1/" -u CRITICAL -a "$1-detector" -A 'Open Stream' -A 'Nope')" -eq '0' ]; then
            xdg-open "https://www.twitch.tv/$1/"
          fi
          rm "$sloc/$1stream_state.txt"
          echo "$todaysDateNumber" >> "$sloc/$1stream_state.txt"
      else
        toconsole "No stream, exiting."
      fi
    else
      toconsole "program already active, exiting."
    fi
  else
  toconsole "already returned a stream for \"$1\" today"
  fi
  rm "$sloc/$1streamer.html" > /dev/null 2>&1
  rm "$sloc/$1prog_state.txt" > /dev/null 2>&1
fi

I did notice that when I deployed this program, I had to add the folders for some of these scripts, so i will add that in the future, or at least when i feel like doing it.

I will look at your script when I have more time, but for now I’ll just share one of my own.

#!/bin/sh
#/home/bird/Documents/Scripts/screenlayout.sh "output.DP-1.priority.1 output.HDMI-A-1.priority.0" "family"
#/home/bird/Documents/Scripts/screenlayout.sh "output.DP-1.priority.0 output.HDMI-A-1.priority.1" "hdmi"

kscreen-doctor $1
sleep 1
while [ -z "$sink" ]
do
    sink=$(wpctl status | grep -i 'vol:' | grep -im1 $2 | grep -ow '.[0-9][0-9]')
    sleep 1
done
wpctl set-default $sink

In the past I had some problems with my sound output not changing to HDMI when I would switch the display settings, so I made a neat little script to automate it. It’s used as described in the comments at the top and I set these commands as a keyboard shortcut; ctrl+alt+left and ctrl+alt+right respectively. No idea if the audio sink changes any better by default nowadays, because I am used to using my script :laughing:

I might share another script later, but that one needs more explanation.

Nice! Mine is a script that checks if a twitch streamer is online, although it isnt very stable and often has problems (every few days)

updated version that lets you set your own times

#!/bin/bash

while getopts "sShD:" flag; do
  case $flag in
    s) # silent mode
    TWITCHONLINE_SILENT_MODE='thisisdoesntmeananythingsinceitonlychecksifthevariableexistssoimjustgoingtotellyoutowatchaZentreyastream'
    ;;
    h) # show help
    self_disable=TRUE
    echo "twitchonline: check if a twitch streamer is online
    -h            : show this help
    -S [streamer] : setup automatic stream checking for [streamer], see this apps code to change timing
    -D [streamer] : remove systemd units for automatic checking for [streamer]"
    ;;
    S) # generate files
      self_disable=TRUE
      
      # make sure directories exist
      mkdir "$HOME/.local/share/systemd/"
      mkdir "$HOME/.local/share/systemd/user/"
      
      # create service unit
      printf "[Unit]\nDescription=\"Execute twitchonline and check for $2\"\n[Service]\nType=oneshot\nExecStart=/bin/bash $HOME/.local/bin/twitchonline.sh $2\n[Install]\nWantedBy=twitchonline_$2.timer" #> $HOME/.local/share/systemd/user/twitchonline_$2.service
      
      # create the timer unit
      FIRST_THIRD="[Unit]\nDescription=Run every 15 minutes from 10:30 AM to 8:30 PM\n[Timer]\nOnCalendar="
      LAST_THIRD="\nAccuracySec=1min\nUnit=twitchonline_$2.service\n[Install]\nWantedBy=timers.target" > $HOME/.local/share/systemd/user/twitchonline_$2.timer
      printf "\nenter the times of day you want to check (in military time, seperatated by commas, do it this way or it wont work)\n>>> "
      read OTHER_THIRD
      printf "${FIRST_THIRD}${OTHER_THIRD//,/\\nOnCalendar=}${LAST_THIRD}" > $HOME/.local/share/systemd/user/twitchonline_$2.timer
      
      # enable the modules for $2
      systemctl --user enable twitchonline_$2.timer
      systemctl --user enable twitchonline_$2.service
      systemctl --user daemon-reload
    ;;
    D) # delete files
      self_disable=TRUE
      # delete and disable modules for $2
      systemctl --user disable twitchonline_$2.timer
      systemctl --user disable twitchonline_$2.service
      systemctl --user daemon-reload
      rm $HOME/.local/share/systemd/user/twitchonline_$2.timer
      rm $HOME/.local/share/systemd/user/twitchonline_$2.service
    ;;
    \?)
    echo "twitchonline: invalid option, see help for instructions"
    ;;
  esac
done

function toconsole() {
  if [ -z "${TWITCHONLINE_SILENT_MODE}" ]; then
    echo $1
  fi
}


if [ -z "${self_disable}" ]; then
  todaysDateNumber=$(date '+%j')
  sloc="$(dirname "$(readlink -f $0)")"
  doesExist=$(cat "$sloc/$1stream_state.txt" | grep -i -m 1 "$todaysDateNumber")
  if [ -z "${doesExist}" ]; then
    if ! grep "busy" "$sloc/$1prog_state.txt"; then
      echo "busy" >> "$sloc/$1prog_state.txt"
      wget "https://twitchtracker.com/$1" -O "$sloc/$1streamer.html" > /dev/null 2>&1
      if grep 'LIVE</span>' "$sloc/$1streamer.html"; then
          toconsole "stream found."
          if [ "$(notify-send "$1 is online!" "https://www.twitch.tv/$1/" -u CRITICAL -a "$1-detector" -A 'Open Stream' -A 'Nope')" -eq '0' ]; then
            xdg-open "https://www.twitch.tv/$1/"
          fi
          rm "$sloc/$1stream_state.txt"
          echo "$todaysDateNumber" >> "$sloc/$1stream_state.txt"
      else
        toconsole "No stream, exiting."
      fi
    else
      toconsole "program already active, exiting."
    fi
  else
    toconsole "already returned a stream for \"$1\" today"
  fi
  rm "$sloc/$1streamer.html" > /dev/null 2>&1
  rm "$sloc/$1prog_state.txt" > /dev/null 2>&1
fi

accidentally commented something important

#!/bin/bash

while getopts "sShD:" flag; do
  case $flag in
    s) # silent mode
    TWITCHONLINE_SILENT_MODE='thisisdoesntmeananythingsinceitonlychecksifthevariableexistssoimjustgoingtotellyoutowatchaZentreyastream'
    ;;
    h) # show help
    self_disable=TRUE
    echo "twitchonline: check if a twitch streamer is online
    -h            : show this help
    -S [streamer] : setup automatic stream checking for [streamer], see this apps code to change timing
    -D [streamer] : remove systemd units for automatic checking for [streamer]"
    ;;
    S) # generate files
      self_disable=TRUE
      
      # make sure directories exist
      mkdir "$HOME/.local/share/systemd/"
      mkdir "$HOME/.local/share/systemd/user/"
      
      # create service unit
      printf "[Unit]\nDescription=\"Execute twitchonline and check for $2\"\n[Service]\nType=oneshot\nExecStart=/bin/bash $HOME/.local/bin/twitchonline.sh $2\n[Install]\nWantedBy=twitchonline_$2.timer" > $HOME/.local/share/systemd/user/twitchonline_$2.service
      
      # create the timer unit
      FIRST_THIRD="[Unit]\nDescription=Run every 15 minutes from 10:30 AM to 8:30 PM\n[Timer]\nOnCalendar="
      LAST_THIRD="\nAccuracySec=1min\nUnit=twitchonline_$2.service\n[Install]\nWantedBy=timers.target" > $HOME/.local/share/systemd/user/twitchonline_$2.timer
      printf "\nenter the times of day you want to check (in military time, seperatated by commas, do it this way or it wont work)\n>>> "
      read OTHER_THIRD
      printf "${FIRST_THIRD}${OTHER_THIRD//,/\\nOnCalendar=}${LAST_THIRD}" > $HOME/.local/share/systemd/user/twitchonline_$2.timer
      
      # enable the modules for $2
      systemctl --user enable twitchonline_$2.timer
      systemctl --user start twitchonline_$2.timer
      systemctl --user enable twitchonline_$2.service
      systemctl --user daemon-reload
    ;;
    D) # delete files
      self_disable=TRUE
      # delete and disable modules for $2
      
      systemctl --user stop twitchonline_$2.timer
      systemctl --user stop twitchonline_$2.service
      systemctl --user disable twitchonline_$2.timer
      systemctl --user disable twitchonline_$2.service
      systemctl --user daemon-reload
      rm $HOME/.local/share/systemd/user/twitchonline_$2.timer
      rm $HOME/.local/share/systemd/user/twitchonline_$2.service
    ;;
    \?)
    echo "twitchonline: invalid option, see help for instructions"
    ;;
  esac
done

function toconsole() {
  if [ -z "${TWITCHONLINE_SILENT_MODE}" ]; then
    echo $1
  fi
}


if [ -z "${self_disable}" ]; then
  todaysDateNumber=$(date '+%j')
  sloc="$(dirname "$(readlink -f $0)")"
  doesExist=$(cat "$sloc/$1stream_state.txt" | grep -i -m 1 "$todaysDateNumber")
  if [ -z "${doesExist}" ]; then
    if ! grep "busy" "$sloc/$1prog_state.txt"; then
      echo "busy" >> "$sloc/$1prog_state.txt"
      wget "https://twitchtracker.com/$1" -O "$sloc/$1streamer.html" > /dev/null 2>&1
      if grep 'LIVE</span>' "$sloc/$1streamer.html"; then
          toconsole "stream found."
          if [ "$(notify-send "$1 is online!" "https://www.twitch.tv/$1/" -u CRITICAL -a "$1-detector" -A 'Open Stream' -A 'Nope')" -eq '0' ]; then
            xdg-open "https://www.twitch.tv/$1/"
          fi
          rm "$sloc/$1stream_state.txt"
          echo "$todaysDateNumber" >> "$sloc/$1stream_state.txt"
      else
        toconsole "No stream, exiting."
      fi
    else
      toconsole "program already active, exiting."
    fi
  else
    toconsole "already returned a stream for \"$1\" today"
  fi
  rm "$sloc/$1streamer.html" > /dev/null 2>&1
  rm "$sloc/$1prog_state.txt" > /dev/null 2>&1
fi

also fixed a systemctl thing forgot to add

fixed systemctl units having missing parameters

#!/bin/bash

while getopts "sS:hD:m:q:" flag; do
  case $flag in
    s) # silent mode
    TWITCHONLINE_SILENT_MODE='thisisdoesntmeananythingsinceitonlychecksifthevariableexistssoimjustgoingtotellyoutowatchaZentreyastream'
    ;;
    h) # show help
      
    self_disable=TRUE
    echo "twitchonline: check if a twitch streamer is online
    -h            : show this help
    -S [streamer] : setup automatic stream checking for [streamer], see this apps code to change timing
    -D [streamer] : remove systemd units for automatic checking for [streamer]"
    ;;
    S) # generate files
      self_disable=TRUE
      # make sure directories exist
      mkdir "$HOME/.local/share/systemd/"
      mkdir "$HOME/.local/share/systemd/user/"
      
      # create service unit
      printf "[Unit]\nDescription=\"Execute twitchonline and check for $OPTARG\"\n[Service]\nType=oneshot\nExecStart=/bin/bash $HOME/.local/bin/twitchonline.sh $OPTARG\n[Install]\nWantedBy=twitchonline_$OPTARG.timer" > $HOME/.local/share/systemd/user/twitchonline_$OPTARG.service
      
      # create the timer unit
      FIRST_THIRD="[Unit]\nDescription=Run every 15 minutes at chosen times of day\n[Timer]\nOnCalendar="
      LAST_THIRD="\nAccuracySec=1min\nUnit=twitchonline_$OPTARG.service\n[Install]\nWantedBy=timers.target"
      printf "enter '1' for daytime\n      '2' for anytime\n      '3' for custom\n>>> "
      
      # lets take a card from the KDE playbook and make this program configurable
      read TIME_CHOICE
      case $TIME_CHOICE in
        1) OTHER_THIRD="7:00,7:15,7:30,7:45,8:00,8:15,8:30,8:45,9:00,9:15,9:30,9:45,10:00,10:15,10:30,10:45,11:00,11:15,11:30,11:45,12:00,12:15,12:30,12:45,13:00,13:15,13:30,13:45,14:00,14:15,14:30,14:45,15:00,15:15,15:30,15:45,16:00,16:15,16:30,16:45,17:00,17:15,17:30,17:45,18:00,18:15,18:30,18:45,19:00,19:15,19:30,19:45,20:00,20:15,20:30" ;;
        2) OTHER_THIRD="0:00,0:15,0:30,0:45,1:00,1:15,1:30,1:45,2:00,2:15,2:30,2:45,3:00,3:15,3:30,3:45,4:00,4:15,4:30,4:45,5:00,5:15,5:30,5:45,6:00,6:15,6:30,6:45,7:00,7:15,7:30,7:45,8:00,8:15,8:30,8:45,9:00,9:15,9:30,9:45,10:00,10:15,10:30,10:45,11:00,11:15,11:30,11:45,12:00,12:15,12:30,12:45,13:00,13:15,13:30,13:45,14:00,14:15,14:30,14:45,15:00,15:15,15:30,15:45,16:00,16:15,16:30,16:45,17:00,17:15,17:30,17:45,18:00,18:15,18:30,18:45,19:00,19:15,19:30,19:45,20:00,20:15,20:30,20:45,21:00,21:15,21:30,21:45,22:00,22:15,22:30,22:45,23:00,23:15,23:30,23:45" ;;
        3)
          printf "enter the times of day you want to check (in military time, seperatated by commas, do it this way or it wont work)\n>>> "
          read OTHER_THIRD
          if [ "$OTHER_THIRD" = "" ]; then
            echo "Error: input invalid"
            exit 1
          fi
        ;;
        *) echo "twitchonline: invalid option, please try again"; exit 1;;
      esac
      printf "${FIRST_THIRD}${OTHER_THIRD//,/\\nOnCalendar=}${LAST_THIRD}" > $HOME/.local/share/systemd/user/twitchonline_$OPTARG.timer
      
      printf "\nDo you want to open the stream in twitch (1) or streamlink (2)\n>>> "
      read chosen_client
      case $chosen_client in
        1) chosen_client="twitch";stream_qaulity="720p";;
        2) chosen_client="streamlink"
          printf "\nWhat qaulity do you want? 160p (1), 360p (2), 480p (3), 720p (4) is available sporadically--same with 720p60 (5), 1080p60 (6)\n>>> "
          read stream_qaulity
          case $stream_qaulity in
            1) stream_qaulity='160p';;
            2) stream_qaulity='360p';;
            3) stream_qaulity='480p';;
            4) stream_qaulity='720p';;
            5) stream_qaulity='720p60';;
            6) stream_qaulity='1080p60';;
            *) echo "twitchonline: invalid option, please try again" exit 1;;
          esac
        ;;
        *) echo "twitchonline: invalid option, please try again" exit 1;;
      esac
      
      # enable the modules for $OPTARG
      systemctl --user enable twitchonline_$OPTARG.timer
      systemctl --user start twitchonline_$OPTARG.timer
      systemctl --user enable twitchonline_$OPTARG.service
      systemctl --user daemon-reload
      
      printf "000\n$chosen_client\n$stream_qaulity" > "$(dirname "$(readlink -f $0)")/${OPTARG}stream_state.txt"
      
    ;;
    D) # delete files
      self_disable=TRUE
      echo $OPTARG
      
      # delete and disable modules for $OPTARG
      
      systemctl --user stop twitchonline_$OPTARG.timer
      systemctl --user stop twitchonline_$OPTARG.service
      systemctl --user disable twitchonline_$OPTARG.timer
      systemctl --user disable twitchonline_$OPTARG.service
      systemctl --user daemon-reload
      rm $HOME/.local/share/systemd/user/twitchonline_$OPTARG.timer
      rm $HOME/.local/share/systemd/user/twitchonline_$OPTARG.service
    ;;
    \?)
      self_disable=TRUE
      echo "twitchonline: invalid option, see help for instructions"
    ;;
  esac
done

function toconsole() {
  if [ -z "${TWITCHONLINE_SILENT_MODE}" ]; then
    echo $1
  fi
}

# chaos ensues
if [ -z "${self_disable}" ]; then
  sloc="$(dirname "$(readlink -f $0)")"
  todaysDateNumber=$(date '+%j')
  doesExist=$( sed '1!d' "$sloc/$1stream_state.txt" | grep -i -m 1 "$todaysDateNumber")
  if [ -z "${mode}" ]; then mode="$(sed '2!d' "$sloc/$1stream_state.txt")"; if ["${twitch}"==""]; then mode='twitch'; fi; fi
  if [ -z "${qaulity}" ]; then qaulity="$(sed '3!d' "$sloc/$1stream_state.txt")"; if ["${qaulity}"==""]; then qaulity='480p'; fi; fi
  
  if [ -z "${doesExist}" ]; then
    if ! grep "busy" "$sloc/$1prog_state.txt"; then
      printf "busy" >> "$sloc/$1prog_state.txt"
      wget "https://twitchtracker.com/$1" -O "$sloc/$1streamer.html" > /dev/null 2>&1
      if grep 'LIVE</span>' "$sloc/$1streamer.html"; then
          toconsole "stream found."
          if [ "$(notify-send "$1 is online!" "https://www.twitch.tv/$1/" -u CRITICAL -a "$1-detector" -A 'Open Stream' -A 'Nope')" -eq '0' ]; then
            case $mode in
              twitch)
                xdg-open "https://www.twitch.tv/$1/"
              ;;
              streamlink)
                nohup streamlink "https://www.twitch.tv/$1/" $qaulity & > /dev/null 2>&1
              ;;
              *)
                echo "twitchonline: unsupported mode, exiting"
                exit 1
              ;;
            esac
          fi
          streamstate="$(cat "$sloc/$1stream_state.txt")"
          yesterdaysDateNumber="$(sed '1!d' "$sloc/$1stream_state.txt")"
          rm "$sloc/$1stream_state.txt"
          printf "${streamstate//$yesterdaysDateNumber/$todaysDateNumber}"  >> "$sloc/$1stream_state.txt"
      else
        toconsole "No stream, exiting."
      fi
    else
      toconsole "program already active, exiting."
      exit 0
    fi
  else
    toconsole "already returned a stream for \"$1\" today"
  fi
  rm "$sloc/$1prog_state.txt" > /dev/null 2>&1
  rm "$sloc/$1streamer.html" > /dev/null 2>&1
fi

fixed the streamlink process being killed by systemd

#!/bin/bash

while getopts "sS:hD:m:q:" flag; do
  case $flag in
    s) # silent mode
    TWITCHONLINE_SILENT_MODE='thisisdoesntmeananythingsinceitonlychecksifthevariableexistssoimjustgoingtotellyoutowatchaZentreyastream'
    ;;
    h) # show help
      
    self_disable=TRUE
    echo "twitchonline: check if a twitch streamer is online
    -h            : show this help
    -S [streamer] : setup automatic stream checking for [streamer], see this apps code to change timing
    -D [streamer] : remove systemd units for automatic checking for [streamer]"
    ;;
    S) # generate files
      self_disable=TRUE
      # make sure directories exist
      mkdir "$HOME/.local/share/systemd/"
      mkdir "$HOME/.local/share/systemd/user/"
      
      # create service unit
      printf "[Unit]\nDescription=\"Execute twitchonline and check for $OPTARG\"\n[Service]\nType=oneshot\nKillMode=process\nExecStart=/bin/bash $HOME/.local/bin/twitchonline.sh $OPTARG\n[Install]\nWantedBy=twitchonline_$OPTARG.timer" > $HOME/.local/share/systemd/user/twitchonline_$OPTARG.service
      
      # create the timer unit
      FIRST_THIRD="[Unit]\nDescription=Run every 15 minutes at chosen times of day\n[Timer]\nOnCalendar="
      LAST_THIRD="\nAccuracySec=1min\nUnit=twitchonline_$OPTARG.service\n[Install]\nWantedBy=timers.target"
      printf "enter '1' for daytime\n      '2' for anytime\n      '3' for custom\n>>> "
      
      # lets take a card from the KDE playbook and make this program configurable
      read TIME_CHOICE
      case $TIME_CHOICE in
        1) OTHER_THIRD="7:00,7:15,7:30,7:45,8:00,8:15,8:30,8:45,9:00,9:15,9:30,9:45,10:00,10:15,10:30,10:45,11:00,11:15,11:30,11:45,12:00,12:15,12:30,12:45,13:00,13:15,13:30,13:45,14:00,14:15,14:30,14:45,15:00,15:15,15:30,15:45,16:00,16:15,16:30,16:45,17:00,17:15,17:30,17:45,18:00,18:15,18:30,18:45,19:00,19:15,19:30,19:45,20:00,20:15,20:30" ;;
        2) OTHER_THIRD="0:00,0:15,0:30,0:45,1:00,1:15,1:30,1:45,2:00,2:15,2:30,2:45,3:00,3:15,3:30,3:45,4:00,4:15,4:30,4:45,5:00,5:15,5:30,5:45,6:00,6:15,6:30,6:45,7:00,7:15,7:30,7:45,8:00,8:15,8:30,8:45,9:00,9:15,9:30,9:45,10:00,10:15,10:30,10:45,11:00,11:15,11:30,11:45,12:00,12:15,12:30,12:45,13:00,13:15,13:30,13:45,14:00,14:15,14:30,14:45,15:00,15:15,15:30,15:45,16:00,16:15,16:30,16:45,17:00,17:15,17:30,17:45,18:00,18:15,18:30,18:45,19:00,19:15,19:30,19:45,20:00,20:15,20:30,20:45,21:00,21:15,21:30,21:45,22:00,22:15,22:30,22:45,23:00,23:15,23:30,23:45" ;;
        3)
          printf "enter the times of day you want to check (in military time, seperatated by commas, do it this way or it wont work)\n>>> "
          read OTHER_THIRD
          if [ "$OTHER_THIRD" = "" ]; then
            echo "Error: input invalid"
            exit 1
          fi
        ;;
        *) echo "twitchonline: invalid option, please try again"; exit 1;;
      esac
      printf "${FIRST_THIRD}${OTHER_THIRD//,/\\nOnCalendar=}${LAST_THIRD}" > $HOME/.local/share/systemd/user/twitchonline_$OPTARG.timer
      
      printf "\nDo you want to open the stream in twitch (1) or streamlink (2)\n>>> "
      read chosen_client
      case $chosen_client in
        1) chosen_client="twitch";stream_qaulity="720p";;
        2) chosen_client="streamlink"
          printf "\nWhat qaulity do you want? 160p (1), 360p (2), 480p (3), 720p (4) is available sporadically--same with 720p60 (5), 1080p60 (6)\n>>> "
          read stream_qaulity
          case $stream_qaulity in
            1) stream_qaulity='160p';;
            2) stream_qaulity='360p';;
            3) stream_qaulity='480p';;
            4) stream_qaulity='720p';;
            5) stream_qaulity='720p60';;
            6) stream_qaulity='1080p60';;
            *) echo "twitchonline: invalid option, please try again"; exit 1;;
          esac
        ;;
        *) echo "twitchonline: invalid option, please try again"; exit 1;;
      esac
      
      # enable the modules for $OPTARG
      systemctl --user enable twitchonline_$OPTARG.timer
      systemctl --user start twitchonline_$OPTARG.timer
      systemctl --user enable twitchonline_$OPTARG.service
      systemctl --user daemon-reload
      
      printf "000\n$chosen_client\n$stream_qaulity" > "$(dirname "$(readlink -f $0)")/${OPTARG}stream_state.txt"
      
    ;;
    D) # delete files
      self_disable=TRUE
      echo $OPTARG
      
      # delete and disable modules for $OPTARG
      
      systemctl --user stop twitchonline_$OPTARG.timer
      systemctl --user stop twitchonline_$OPTARG.service
      systemctl --user disable twitchonline_$OPTARG.timer
      systemctl --user disable twitchonline_$OPTARG.service
      systemctl --user daemon-reload
      rm $HOME/.local/share/systemd/user/twitchonline_$OPTARG.timer
      rm $HOME/.local/share/systemd/user/twitchonline_$OPTARG.service
    ;;
    \?)
      self_disable=TRUE
      echo "twitchonline: invalid option, see help for instructions"
    ;;
  esac
done

function toconsole() {
  if [ -z "${TWITCHONLINE_SILENT_MODE}" ]; then
    echo $1
  fi
}

# chaos ensues
if [ -z "${self_disable}" ]; then
  sloc="$(dirname "$(readlink -f $0)")"
  todaysDateNumber=$(date '+%j')
  doesExist=$( sed '1!d' "$sloc/$1stream_state.txt" | grep -i -m 1 "$todaysDateNumber")
  mode="$(sed '2!d' "$sloc/$1stream_state.txt")";
  qaulity="$(sed '3!d' "$sloc/$1stream_state.txt")";
  
  if [ -z "${doesExist}" ]; then
    if ! grep "busy" "$sloc/$1prog_state.txt"; then
      printf "busy" >> "$sloc/$1prog_state.txt"
      wget "https://twitchtracker.com/$1" -O "$sloc/$1streamer.html" > /dev/null 2>&1
      if grep 'LIVE</span>' "$sloc/$1streamer.html"; then
          toconsole "stream found."
          if [ "$(notify-send "$1 is online!" "https://www.twitch.tv/$1/" -u CRITICAL -a "$1-detector" -A 'Open Stream' -A 'Nope')" -eq '0' ]; then
            case $mode in
              twitch) xdg-open "https://www.twitch.tv/$1/" ;;
              streamlink) nohup streamlink "https://www.twitch.tv/$1/" $qaulity& ;;
              *) echo "twitchonline: unsupported mode, exiting";  exit 1 ;;
            esac
          fi
          streamstate="$(cat "$sloc/$1stream_state.txt")"
          yesterdaysDateNumber="$(sed '1!d' "$sloc/$1stream_state.txt")"
          rm "$sloc/$1stream_state.txt"
          printf "${streamstate//$yesterdaysDateNumber/$todaysDateNumber}"  >> "$sloc/$1stream_state.txt"
      else
        toconsole "No stream, exiting."
      fi
    else
      toconsole "program already active, exiting."
      exit 0
    fi
  else
    toconsole "already returned a stream for \"$1\" today"
    exit 0
  fi
  rm "$sloc/$1prog_state.txt" > /dev/null 2>&1
  rm "$sloc/$1streamer.html" > /dev/null 2>&1
fi