#!/bin/sh log_directory=~/log log_sample="Lundi:\n \n Mardi:\n \n Mercredi:\n \n Jeudi:\n \n Vendredi:\n " SPM=60 MPH=60 HPD=24 mode_available="view edit list toMail" usage () { echo "usage : ${0##*/} MODE [DATE|select] " echo -e "\twith MODE in : $mode_available" echo -e "\tDATE format is YYYY_MM_DD" echo "" } read_date () { if [ -z $1 ]; then return 0 fi day=${1#*_*_} tmp=${1%_*} month=${tmp#*_} year=${tmp%_*} old=$(date -d "$year-$month-$day" +%s) new=$(date +%s) diff=$new-$old return $((diff / SPM / MPH / HPD)) } check_valid_argument () { needle=$1 for hay in $2; do [[ $hay == $needle ]] && return 1 done return 0 } create_file () { if [ ! -e $1 ] then mkdir -p $log_directory echo -e $log_sample > $1 fi } view () { read_date $1 date_arg=$? curr_day=$(date --date="$date_arg days ago" +%u) day_to_monday=$(($curr_day -1)) date_to_edit=$(date --date="$((date_arg + day_to_monday)) days ago" +%y_%m_%d) file=$log_directory/$date_to_edit create_file $file more $log_directory/$date_to_edit } edit () { read_date $1 date_arg=$? curr_day=$(date --date="$date_arg days ago" +%u) day_to_monday=$(($curr_day -1)) date_to_edit=$(date --date="$((date_arg + day_to_monday)) days ago" +%y_%m_%d) file=$log_directory/$date_to_edit create_file $file ${EDITOR} $log_directory/$date_to_edit } list () { logs=$(ls $log_directory | sort -r ) echo "${logs}" } toMail () { read_date $1 date_arg=$? curr_day=$(date --date="$date_arg days ago" +%u) day_to_monday=$(($curr_day -1)) day_to_friday=$((5 - $curr_day)) start_date=$(date --date="$((date_arg + day_to_monday)) days ago" +%y-%m-%d) stop_date=$(date --date="$((date_arg + day_to_friday)) days" +%y-%m-%d) echo "Weekly $start_date -> $stop_date" view } check_valid_argument $1 "$mode_available" valid=$? if [[ $# -ge 1 && $valid = 1 ]] then date=$2 if [[ "$2" = "select" ]] then PS3='Choose Your Date ' logs=$(ls $log_directory | sort -r) select date in $logs quit do echo echo "date $date selected" echo break done if [[ "$date" == "quit" ]] then echo "Exiting" exit 0 fi fi eval $1 $date else usage fi