config/scripts/log_editor.sh

155 lines
2.9 KiB
Bash
Raw Normal View History

2011-09-16 10:30:02 +02:00
#!/bin/sh
log_directory=~/log
2011-10-03 09:45:55 +02:00
log_sample="\nLundi:\n
2011-09-16 10:30:02 +02:00
\n
Mardi:\n
\n
Mercredi:\n
\n
Jeudi:\n
\n
Vendredi:\n
2011-10-03 09:45:55 +02:00
\n
\n
\n
\n
TODO:\n
2011-09-16 10:30:02 +02:00
"
SPM=60
MPH=60
HPD=24
mode_available="view edit list toMail todo sendMail"
2011-09-16 10:30:02 +02:00
usage () {
echo "usage : ${0##*/} MODE [DATE|select] "
echo -e "\twith MODE in : $mode_available"
echo -e "\tDATE format is YYYY_MM_DD"
2011-09-16 10:30:02 +02:00
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
}
2011-09-16 10:30:02 +02:00
list () {
2011-10-03 09:45:55 +02:00
logs=$(ls $log_directory | grep ".*_.*_.*"| sort -r )
2011-09-16 10:30:02 +02:00
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))
2011-10-03 09:45:55 +02:00
start_date=$(date --date="$((date_arg + day_to_monday)) days ago" +%Y-%m-%d)
stop_date=$(date --date="$((date_arg - day_to_friday)) days ago" +%Y-%m-%d)
2011-09-16 10:30:02 +02:00
echo "Weekly $start_date -> $stop_date"
2011-10-03 09:45:55 +02:00
view $1
2011-09-16 10:30:02 +02:00
}
sendMail () {
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 ago" +%Y-%m-%d)
view $1 |mail -s "Weekly $start_date -> $stop_date" $USER@dxo.com
}
2011-10-03 09:45:55 +02:00
todo () {
${EDITOR} $log_directory/TODO
}
2011-09-16 10:30:02 +02:00
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
2011-09-16 10:30:02 +02:00
else
usage
fi