[go: up one dir, main page]

Menu

[r285]: / bin / ct-date  Maximize  Restore  History

Download this file

304 lines (262 with data), 7.6 kB

#!/bin/sh -e
## ct-date for ctafconf in /home/ctaf/.ctafconf/etc/shell
##
## Made by GESTES Cedric
## Login   <goctaf@gmail.com>
##
## Started on  Fri Mar 31 19:54:39 2006 GESTES Cedric
## Last update Wed Sep 24 14:44:48 2008 Cedric GESTES
##
##use: http://www.intuitive.com/wicked/showscript.cgi?030-agenda.sh
##use: http://aplawrence.com/Bofcusm/1455.html
##CTAFCONF

agendafile=~/.config/ctafconf/perso/reminder

#max one month
# yesterday()
# {
#     local back=$1
#     day=$(( `date +%d` - $back ))
#     month=`date +%m`
#     year=`date +%Y`

#     if [ $day -le "0" ]; then
#         month=$((month-1))
#         if [ $month -eq "0" ]; then
#             month=12
#             year=$((year-1))
#         fi
#         set `cal $month $year`
#         shiftd=$(( $day * -1 + 1 ))
#         shift $(($# - $shiftd))
#         day=$1
#     fi
#     day=`normanumber $day`
#     month=`normanumber $month`
# }

#MAXone month
tomorrow()
{
    local next=$1

    day=`date +%d`
    day=`denormanumber $day`
    day=$(( $day + $next ))
    month=`date +%m`
    year=`date +%Y`
    set `cal $month $year`
    shift $(($# - 1))
    monthday=$1

    if [ $day -gt $monthday ]; then
        month=$(($month+1))
        if [ $month -gt "12" ]; then
            month=1
            year=$(($year+1))
        fi
        day=$(($day-$monthday))
    fi
    day=`normanumber $day`
    month=`normanumber $month`
}

#########################################
isDayName()
{
  # return = 0 if all is well, 1 on error

  case $(echo $1 | tr '[[:upper:]]' '[[:lower:]]') in
   sun*|mon*|tue*|wed*|thu*|fri*|sat*) retval=0	;;
   * ) retval=1 ;;
  esac
  return $retval
}

########################################
isMonthName()
{
    case $(echo $1 | tr '[[:upper:]]' '[[:lower:]]') in
      jan*|feb*|mar*|apr*|may*|jun*)	return 0	;;
      jul*|aug*|sep*|oct*|nov*|dec*)	return 0	;;
      * ) return 1 	;;
    esac
}

########################################
normalize()
{
  # return string with first char uppercase, next two lowercase
  echo -n $1 | cut -c1  | tr '[[:lower:]]' '[[:upper:]]'
  echo  $1 | cut -c2-3| tr '[[:upper:]]' '[[:lower:]]'
}

denormanumber()
{
  local num=$1

  first=`echo $num | cut -c1`
  if [ x$first = x0 ]; then
    echo $num | cut -c2-
  else
    echo $num
  fi
}

normanumber()
{
  local num=$(denormanumber $1)

  if [ $num -lt 10 ]; then
    num="0$num"
  fi
  echo $num
}

###########################################
checkDate()
{
  # create the possible default values that'll match today
  weekday=$1
  day=$2
  month=$3
  year=$4
  format1="$weekday"
  format2="$day$month"
  format3="$day$month$year"
  format4="$day"

  # and step through the file comparing dates...
  ancIFS=$IFS
  IFS="|"	# The reads will naturally split at the IFS

  while read date description ; do
      if [ "$date" = "$format1" \
          -o "$date" = "$format2" \
          -o "$date" = "$format3" \
          -o "$date" = "$format4" ]; then
      echo "  - $description"
    fi
  done < $agendafile
  IFS=$ancIFS
}

checkevent()
{
  # create the possible default values that'll match today
  weekday=$1
  day=$2
  month=$3
  year=$4
  format1="$weekday"
  format2="$day$month"
  format3="$day$month$year"
  format4="$day"

  # and step through the file comparing dates...
  ancIFS=$IFS
  IFS="|"	# The reads will naturally split at the IFS

  while read date description ; do
      if [ "$date" = "$format1" \
          -o "$date" = "$format2" \
          -o "$date" = "$format3" \
          -o "$date" = "$format4" ]; then
          IFS=$ancIFS
          return 1;
      fi
  done < $agendafile
  IFS=$ancIFS
  return 0
}


###########ADD ONE DATE####
if [ $# -gt 0 ] && [ x$1 = xadd ]; then
    if [ ! -w $agendafile ] ; then
        echo "$0: cannot write in $agendafile" >&2
        exit 1
    fi

    echo "Enter the date of event"
    echo "Every week   : dayname (sun, mon, tue, wed, thu, fri, sat)"
    echo "Every Month  : day number (eg 12)"
    echo "Every Year   : 12 01"
    echo "Every Century: 12 01 2006"
    echo -n "Date of event (dayname, day, day month or day month year): "
    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 [ "$word1" -gt 0 -o "$word1" -lt 32 ] && [ -z "$word2" ]; then
            date=`normanumber $word1`
        else
            if [ -z "$word2" ] ; then
                echo "Bad dayname format: unknown day name specified" >&2
                exit 1
            fi

        # if [ ! -z "$(echo $word1|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 range 1-31" >&2
                exit 1
            fi
            word1=`normanumber $word1`
            if [ "$word2" -lt 1 -o "$word2" -gt 12 ] ; then
                echo "Bad date format: month number can only be in range 1-12" >&2
                exit 1
            fi
            word2=`normanumber $word2`
#         if ! isMonthName $word2 ; then
#             echo "Bad date format: unknown month name specified." >&2
#             exit 1
#         fi

        #word2="$(normalize $word2)"

            if [ -z "$word3" ] ; then
                date="$word1$word2"
            else
                if [ ! -z "$(echo $word3|sed 's/[[:digit:]]//g')" ] ; then
                    echo "Bad date format: third field should be year." >&2
                    exit 1
                elif [ $word3 -lt 2000 -o $word3 -gt 2500 ] ; then
                    echo "Bad date format: year value should be 2000-2500" >&2
                    exit 1
                fi
                date="$word1$word2$word3"
            fi
        fi
    fi
    echo -n "One line description: "
    read description

    # ready to write to datafile
    echo "$(echo $date|sed 's/ //g')|$description" >> $agendafile
    exit 0

fi

if [ $# -gt 0 ] && [ x$1 = xlist ]; then
    cat $agendafile
    exit 0
fi

if [ $# -gt 0 ] ; then
    echo "usage ct-date [add] [list]"
    echo "edit ~/.config/ctafconf/perso/reminder to remove one date"
    exit 1
fi

if [ ! -e "$agendafile" ] ; then
#  echo "ct-date: no '~/.config/ctafconf/perso/reminder' file found" >&2
#  echo "use ct-date to add events or create that empty file to remove the warning" >&2
  exit 1
fi

# now let's get today's date...
#eval $(date "+weekday=\"%a\" month=\"%b\" day=\"%e\" year=\"%G\"")
#day="$(echo $day|sed 's/ //g')"	# remove possible leading space

eval $(date "+weekday=\"%a\" month=\"%m\" day=\"%d\" year=\"%Y\"")


if ! checkevent $weekday $day $month $year; then
    echo "$day/$month/$year, Today events:"
    checkDate $weekday $day $month $year
fi

#avance la date de test
tomorrow 1
if ! checkevent $weekday $day $month $year; then
    echo "$day/$month/$year, Tomorrow events:"
    #fuck => weekly event are checked only the day they occurs
    checkDate fuck $day $month $year
fi

#avance la date de test
tomorrow 2
if ! checkevent $weekday $day $month $year; then
    echo "$day/$month/$year events:"
    #fuck => weekly event are checked only the day they occurs
    checkDate fuck $day $month $year
fi

#avance la date de test
tomorrow 3
if ! checkevent $weekday $day $month $year; then
    echo "$day/$month/$year events:"
    #fuck => weekly event are checked only the day they occurs
    checkDate fuck $day $month $year
fi

exit 0