TeamBlast

calcweek.sh

Dec 5th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.58 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # calcweek.sh by dan saunders <[email protected]>
  4.  
  5. # Define some defaults
  6. SEASONS="spring summer fall winter"
  7. YEAR="$(date +%Y)"
  8. DAY="$(date +%j)"
  9.  
  10. # If there is a first argument, make sure it's a season
  11. if [[ -n "$1" ]] && [[ "$SEASONS" =~ "$1" ]]; then
  12.     SEASON="$1"
  13.  
  14.     # Check for a second argument - a date input
  15.     if [[ -n "$2" ]]; then
  16.         # Do some dirty cleverness by throwing the input through date - it will complain if it is an invalid format ;)
  17.         date +%m/%d/%Y -d "$2" > /dev/null 2>&1
  18.         [[ "$?" -ne "0" ]] && echo "That's not a valid date format, silly!" && exit
  19.        
  20.         # Extract that stuff!
  21.         DATE=$(date +%m/%d/%Y -d "$2" 2>/dev/null)
  22.         YEAR="$(date -d "$DATE" +%Y)"
  23.         DAY="$(date -d "$DATE" +%j)"
  24.     fi
  25. else
  26.     # Ok, so they didn't provide a date. Time to use trial and error to search for the correct semester
  27.     # In order to loop through a sequence of words, I had to do some dirty work using read
  28.     SEASON=$(echo $SEASONS | while read line; do
  29.         for word in $line; do
  30.             [[ -f "./$word$YEAR.conf" ]] && source "./$word$YEAR.conf"
  31.             [[ -f "/var/public/fall2015/unix/sfp0/$word$YEAR.conf" ]] && source "/var/public/fall2015/unix/sfp0/$word$YEAR.conf"
  32.             [[ -z "$SEMESTER" ]] && continue
  33.  
  34.             if [[ "$SEMESTER_START" -le "$DAY" ]] && [[ "$DAY" -le "$SEMESTER_END" ]]; then
  35.                 echo $word
  36.                 break
  37.             fi
  38.         done
  39.     done)
  40. fi
  41.  
  42. # Load the configuration, or at least attempt to
  43. [[ -f "./$SEASON$YEAR.conf" ]] && source "./$SEASON$YEAR.conf"
  44. [[ -f "/var/public/fall2015/unix/sfp0/$SEASON$YEAR.conf" ]] && source "/var/public/fall2015/unix/sfp0/$SEASON$YEAR.conf"   
  45. [[ -z "$SEMESTER" ]] && echo "Could not find configuration for \"$SEASON$YEAR\". Maybe you're looking into the future?" && exit
  46.  
  47. # Is the date during the given semester?
  48. if [[ "$DAY" -le "$SEMESTER_START" ]]; then
  49.     # It's before, so we must return 0
  50.     echo 0
  51.     exit
  52. elif [[ "$SEMESTER_END" -le "$DAY" ]]; then
  53.     # It's after, so we must return 16
  54.     echo 16
  55.     exit
  56. fi
  57.  
  58. # Check for break weeks. Since bash doesn't have a loop by index feature, use cleverness to make it
  59. for i in "${!BREAK[@]}"; do
  60.     # Checks every other index to determine if it's a break week; the even and odd indexes are the same - it's not one
  61.     if [[ $((i%2)) -eq 0 ]] && [[ "${BREAK[$i]}" -le "$DAY" ]] && [[ "$DAY" -le "${BREAK[$((i+1))]}" ]] && [[ "${BREAK[$i]}" -ne "${BREAK[$((i+1))]}" ]]; then
  62.         echo break week
  63.         exit
  64.     fi
  65. done
  66.  
  67. # The final step, display the week number, and in the process remove leading zeroes that we don't need
  68. echo $(($(date -d "$(date -d "$YEAR-01-01 + $(($DAY - 1)) days")" +%V) - $STARTWEEK))
Advertisement
Add Comment
Please, Sign In to add comment