#======================================================================== # Buffadd - Prompts the user to add a new event for the Buffsched script. # # Author: Scott Buffington # # Change History: # Scott Buffington - 10/30/2006 - Created # Scott Buffington - 11/28/2006 - Added Biweekly Support. # Scott Buffington - 12/06/2006 - Cosmetic updates. # Scott Buffington - 02/17/2007 - Added Solaris 10 Support and ticker # and commented and cleaned code. # Scott Buffington - 08/27/2007 - Added AIX Support. # Scott Buffington - 12/10/2007 - Changed translate statement to # overcome Solaris 10 bug. # Scott Buffington - 12/20/2007 - Upgraded ticker with spinner. # # License: GPL, http://www.gnu.org/copyleft/gpl.html # # Version: 1.9.2 # #======================================================================== #======================================================================== # These should be the only variables that you might want to change. #======================================================================== NUMDAYS=14 BIDUR=24 schedulefile="$HOME/.schedule" #======================================================================== # tput commands to control highlighting # Note: some terminals do not support all tput options #======================================================================== RVR=`tput smso` # reverse END_RVR=`tput rmso` # end reverse BLNK=`tput blink` # blink UNDER=`tput smul` # underline END_UNDER=`tput rmul` # underline CLEAR=`tput clear` # clear screen BELL=`tput bel` # bell sound BOLD=`tput bold` # bold DIM=`tput dim` # dim INVIS=`tput invis` # invisible END=`tput sgr0` # turn off dim, blink, bold, dim, invisible #======================================================================== # Get the current date for various features. #======================================================================== monthcal=`date +%m | awk '{print int($0)}'` yearcal=`date +%Y` currDay=`date "+%d"` #======================================================================== # Operating System Check. #======================================================================== case `uname` in SunOS) export OSTYPE=HPSun export AWKPATH="/usr/xpg4/bin" export SEDPATH="/usr/xpg4/bin" export LOGONNAME=`finger $LOGNAME |awk 'NR==1 {print $7 " " $8;exit}'` ;; AIX) export OSTYPE=AIX export AWKPATH="/usr/bin" export SEDPATH="/usr/bin" export LOGONNAME=`finger $LOGNAME |awk 'NR==1 {print $7 " " $8;exit}'` ;; HP-UX) export OSTYPE=HPSun export AWKPATH="/usr/bin" export SEDPATH="/usr/bin" export LOGONNAME=`finger $LOGNAME |awk 'NR==1 {print $9 " " $10;exit}'` ;; Darwin) export OSTYPE=OSX export AWKPATH="/usr/bin" export SEDPATH="/usr/bin" export LOGONNAME=`finger $LOGNAME |awk 'NR==1 {print $4 " " $5;exit}'` ;; Linux) export OSTYPE=Linux export AWKPATH="/usr/bin" export SEDPATH="/bin" export LOGONNAME=`finger $LOGNAME |awk 'NR==1 {print $4 " " $5;exit}'` ;; *) clear echo "This utility has not been tested on this Operating System yet." echo "Valid systems are AIX, HP-UX, Solaris 10, Mac OS X, and Linux. If you want" echo "BuffSched, you can help make BuffSched work on your system. Altering" echo "the first case statement can get BuffSched working. For assistance," echo "contact Scott. The goal is to have BuffSched work on all Unices." exit 1 ;; esac #======================================================================== # Function to check if day name entered is correct. #======================================================================== isDayName() { # return = 0 if all is well, 1 if an error case $(echo $1 | tr [A-Z] [a-z]) in sun*|mon*|tue*|wed*|thu*|fri*|sat*) retval=0;; * ) retval=1;; esac return $retval } #======================================================================== # Function to check if month name entered is correct. #======================================================================== isMonthName() { case $(echo $1 | tr [A-Z] [a-z]) in jan*|feb*|mar*|apr*|may*|jun*) return 0 ;; jul*|aug*|sep*|oct*|nov*|dec*) return 0 ;; * ) return 1 ;; esac } #======================================================================== # Function to assure standard of day or date entered. #======================================================================== normalize() { # return string with first character uppercase, next two lowercase echo $1 | cut -c1 | tr [a-z] [A-Z] echo $1 | cut -c2-3 | tr [A-Z] [a-z] } #======================================================================== # Function to add Biweekly events. #======================================================================== doBiweekly() { #Ready to write to data file echo "$(echo $date |sed 's/ //g') | $description" >> $schedulefile case $1 in Jan) monthnum=1;; Feb) monthnum=2;; Mar) monthnum=3;; Apr) monthnum=4;; May) monthnum=5;; Jun) monthnum=6;; Jul) monthnum=7;; Aug) monthnum=8;; Sep) monthnum=9;; Oct) monthnum=10;; Nov) monthnum=11;; Dec) monthnum=12;; esac day=$2 year=$3 duration=0; until [ $duration -ge $BIDUR ] do duration="$(echo "$duration + 1" |bc)" case $OSTYPE in HPSun) lastday="$(cal $monthnum $year |grep -v "^$" |tail -1 |awk '{print $NF}')" ;; AIX) lastday="$(cal $monthnum $year |grep -v "^$" |tail -1 |awk '{print $NF}')" ;; OSX) lastday="$(cal $monthnum $year |grep -v "^$" |tail -1 |awk '{print $NF}')" ;; Linux) lastday="$(cal $monthnum $year |grep -v "^$" |tail -2 |awk '{print $NF}')" ;; esac day="$(echo $day|sed 's/ //g')" cnt=0; until [ $cnt -ge $NUMDAYS ] do cnt="$(echo "$cnt + 1" |bc)" lastday="$(echo $lastday|sed 's/ //g')" if [ "$day" -eq "$lastday" ] then monthnum="$(echo "$monthnum + 1" |bc)" case $monthnum in 1) month=Jan;; 2) month=Feb;; 3) month=Mar;; 4) month=Apr;; 5) month=May;; 6) month=Jun;; 7) month=Jul;; 8) month=Aug;; 9) month=Sep;; 10) month=Oct;; 11) month=Nov;; 12) month=Dec;; 13) month=Jan monthnum=1;; esac day=1 if [ "$month" = Jan ] then year="$(echo "$year + 1" |bc)" fi else day="$(echo "$day + 1" |bc)" fi if [ "$cnt" -eq $NUMDAYS ] then date="$day$month$year" #Ready to write to data file echo "$(echo $date |sed 's/ //g') | $description" >> $schedulefile fi done done } #======================================================================== # Function to add Consecutive day events. #======================================================================== doConsec() { #Ready to write to data file echo "$(echo $date |sed 's/ //g') | $description" >> $schedulefile case $1 in Jan) monthnum=1;; Feb) monthnum=2;; Mar) monthnum=3;; Apr) monthnum=4;; May) monthnum=5;; Jun) monthnum=6;; Jul) monthnum=7;; Aug) monthnum=8;; Sep) monthnum=9;; Oct) monthnum=10;; Nov) monthnum=11;; Dec) monthnum=12;; esac month=$1 day=$2 year=$3 duration=0; consecDays="$(echo "$consecDays - 1" |bc)" until [ $duration -ge $consecDays ] do duration="$(echo "$duration + 1" |bc)" case $OSTYPE in HPSun) lastday="$(cal $monthnum $year |grep -v "^$" |tail -1 |awk '{print $NF}')" ;; AIX) lastday="$(cal $monthnum $year |grep -v "^$" |tail -1 |awk '{print $NF}')" ;; OSX) lastday="$(cal $monthnum $year |grep -v "^$" |tail -1 |awk '{print $NF}')" ;; Linux) lastday="$(cal $monthnum $year |grep -v "^$" |tail -2 |awk '{print $NF}')" ;; esac day="$(echo $day|sed 's/ //g')" cnt=0; until [ $cnt -ge 1 ] do cnt="$(echo "$cnt + 1" |bc)" lastday="$(echo $lastday|sed 's/ //g')" if [ "$day" -eq "$lastday" ] then monthnum="$(echo "$monthnum + 1" |bc)" case $monthnum in 1) month=Jan;; 2) month=Feb;; 3) month=Mar;; 4) month=Apr;; 5) month=May;; 6) month=Jun;; 7) month=Jul;; 8) month=Aug;; 9) month=Sep;; 10) month=Oct;; 11) month=Nov;; 12) month=Dec;; 13) month=Jan monthnum=1;; esac day=1 if [ "$month" = Jan ] then year="$(echo "$year + 1" |bc)" fi else day="$(echo "$day + 1" |bc)" fi if [ "$cnt" -eq 1 ] then date="$day$month$year" #Ready to write to data file echo "$(echo $date |sed 's/ //g') | $description" >> $schedulefile fi done done } #======================================================================== # Function to return month number and year. #======================================================================== getDate() { # syntax: getDate monthGap themonth=$(($monthcal+$1)) theyear=$yearcal if [ $themonth -gt 12 ] then themonth=$(($themonth-12)) theyear=$(($theyear+1)) fi if [ $themonth -lt 1 ] then themonth=$((12-$themonth)) theyear=$(($theyear-1)) fi echo $themonth " " $theyear } #======================================================================== # Function to mark the current day on the calendar with brackets. #======================================================================== MarkToday() { $AWKPATH/awk -v cday=$currDay '{ fill=(int(cday)>9?"":" "); a=$0; sub(" "fill int(cday)" ","["fill int(cday)"]",a); print a }' } #======================================================================== # Function to print out the caledars at the top. #======================================================================== doCalUS() { cal $(getDate -1) echo cal $monthcal $yearcal | awk '{ print " " $0 " " }' | MarkToday echo cal $(getDate 1) } #======================================================================== # Function to produce spinner while adding BiWeekly event. #======================================================================== spinNer() { SPIN[0]="/" SPIN[1]="-" SPIN[2]="\\" SPIN[3]="|" PROCESS=$1 spin_num=0 if [ "$OSTYPE" = HPSun ] || [ "$OSTYPE" = AIX ] ; then echo "${SPIN[$spin_num]} \c\b\b" else echo -e "${SPIN[$spin_num]} \c\b\b" fi while ( ps -e | grep $PROCESS | grep -v "grep" 1>/dev/null ) do if [ "$spin_num" -gt "3" ]; then spin_num=0 fi if [ "$OSTYPE" = HPSun ] || [ "$OSTYPE" = AIX ] ; then echo "\b\b${SPIN[$spin_num]} \c" else echo -e "\b\b${SPIN[$spin_num]} \c" fi spin_num=`expr $spin_num + 1` sleep 1 done if [ "$OSTYPE" = HPSun ] || [ "$OSTYPE" = AIX ] ; then echo "... Done\c" else echo -e "... Done\c" fi echo "" } #======================================================================== # Main Program Processing #======================================================================== clear echo " ${RVR}BuffSched: The Unix Reminder Service${END_RVR}" case $OSTYPE in HPSun) eval $(date "+weekday=\"%a\" month=\"%b\" monthnum=\"%m\" day=\"%e\" year=\"%Y\"") doCalUS | pr -3 -t -i100 |tbl lastday="$(cal $monthnum $year |grep -v "^$" |tail -1 |awk '{print $NF}')" ;; AIX) eval $(date "+weekday=\"%a\" month=\"%b\" monthnum=\"%m\" day=\"%e\" year=\"%Y\"") cal $monthcal $yearcal | sed "s/ ${currDay} / ${RVR}${currDay}${END_RVR} /" lastday="$(cal $monthnum $year |grep -v "^$" |tail -1 |awk '{print $NF}')" ;; OSX) eval $(date "+weekday=\"%a\" month=\"%b\" monthnum=\"%m\" day=\"%e\" year=\"%G\"") doCalUS | pr -3 -t -i100 | colrm 22 24 | colrm 44 44 lastday="$(cal $monthnum $year |grep -v "^$" |tail -1 |awk '{print $NF}')" ;; *) eval $(date "+weekday=\"%a\" month=\"%b\" monthnum=\"%m\" day=\"%e\" year=\"%G\"") doCalUS | pr -3 -t -i100 | colrm 22 24 | colrm 44 44 lastday="$(cal $monthnum $year |grep -v "^$" |tail -2 |awk '{print $NF}')" ;; esac if [ ! -w $HOME ] ; then echo "$0: cannot write in your home directory ($HOME)" >&2 exit 1 fi echo "Hello $LOGONNAME, follow the instructions to enter an event." echo "Date of event (day month, day month year or dayname): " read word1 word2 word3 junk if isDayName $word1 ; then if [ ! -z "$word2" ] ; then echo "Bad dayname format: just specify the day name by itself." >&2 exit 1 fi date="$(normalize $word1)" else if [ -z "$word2" ] ; then echo "Bad dayname format: unknown day name specified" >&2 exit 1 fi if [ ! -z "$(echo $word1|$SEDPATH/sed 's/[[:digit:]]//g')" ] ; then echo "Bad date format: please specify day first, by day number." >&2 exit 1 fi if [ "$word1" -lt 1 -o "$word1" -gt 31 ] ; then echo "Bad date format: day number can only be in a range 1-31" >&2 exit 1 fi isMonthName $word2 if [ $? -ne 0 ] ; then echo "Bad date format: unknown month name specified." &>2 exit 1 fi word2="$(normalize $word2)" word2="$(echo $word2|sed 's/ //g')" if [ -z "$word3" ] ; then date="$word1$word2" else if [ ! -z "$(echo $word3|$SEDPATH/sed 's/[[:digit:]]//g')" ] ; then echo "Bad date format: third field should be year." >&2 exit 1 elif [ $word3 -lt 2006 -o $word3 -gt 2500 ] ; then echo "Bad date format: year value should be 2006-3500" >&2 exit 1 fi date="$word1$word2$word3" fi fi if isDayName $word1 ; then echo "One-line description: " read description #Ready to write to data file echo "$(echo $date |sed 's/ //g') | $description" >> $schedulefile exit 0 else if [ -z "$word3" ] ; then echo "One-line description: " read description #Ready to write to data file echo "$(echo $date |sed 's/ //g') | $description" >> $schedulefile exit 0 else echo "Do you want to schedule this biweekly? (y/n)" read BIWEEKLY if [ "$BIWEEKLY" = y -o "$BIWEEKLY" = Y ];then echo "One-line description: " read description doBiweekly $word2 $word1 $word3 & echo "Stand by..." spinNer $! else echo "Do you want to schedule this over consecutive days? (y/n)" read CONSEC if [ "$CONSEC" = y -o "$CONSEC" = Y ];then consecDays=0 echo "How many consecutive days would you like to schedule this event over?" read consecDays echo "One-line description: " read description doConsec $word2 $word1 $word3 & echo "Standy by..." spinNer $! exit 0 fi echo "One-line description: " read description #Ready to write to data file echo "$(echo $date |sed 's/ //g') | $description" >> $schedulefile exit 0 fi fi fi exit 0