Add a script for log edition

This commit is contained in:
Mathieu Maret 2011-09-16 10:30:02 +02:00
parent 03458bd434
commit f24288b7df
1 changed files with 118 additions and 0 deletions

118
scripts/log_editor.sh Executable file
View File

@ -0,0 +1,118 @@
#!/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] "
echo "with mode in :"
for i in $mode_available;
do
echo -e "$i "
done
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)
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
eval $1 $2
else
usage
fi