#! /bin/sh

# name : record
# author: Jiqing Wu
# email: jiqingwu@gmail.com
# version: 0.1
# create: 2009-10-29 
# last Update: 2009-10-29

# 说明：
# 这是我写的用于记录自己时间使用的小工具，
# 你也可以用它快速记录一些事情，类似微博

version()
{
    sed -e 's/^    //' <<EndVersion
    record -- record your activities.
    version 0.1.1
    author: Jiqing Wu (jiqingwu@gmail.com)
    release date: 2009-10-29
    last updated: 2009-10-29
    License: GPL, http://www.gnu.org/copyleft/gpl.html
EndVersion
}

help()
{
    sed -e 's/^    //' <<EndHelp
    Usage: record [option] | [your_record]

    Without arguments, if there is record file of today,
    print all records of today.

    record your_record -- append your_record to the records of today.

    -v, --version: show version.
    -h, --help: show this help.
    -c date, --check date: check the records of the date.
        The format of date is yyyy-mm-dd
EndHelp
}

check()
{
    if [ -z $1 ]; then
        echo "Usage: record --check yyyy-mm-dd"
    else
        old_file=$root_dir/$1
        if [ -f $old_file ]; then
            cat $old_file
        else
            echo "There are not records of $1" 
        fi
    fi
}

# 记录的目标文件为~/RECORD/yyyy-mm-dd
root_dir=$HOME/RECORD
file=$root_dir/`date +%F`

if [ -z $1 ]; then
    #如果不加参数，并且目标文件不存在，显示警告信息
    if [ ! -f $file ]; then
        echo "record what?"
    #如果不加参数，并且目标文件存在，输出记录内容
    else
        cat $file
    fi
    exit 0
fi

# 如果目标文件夹不存在，建立该文件夹
if [ ! -d $root_dir ]; then
    mkdir $root_dir
fi

case $1 in
    --help|-h) help;;
    --version|-v) version;;
    --check|-c) check "$2";;
    *) echo `date +%H:%M` $* >> $file;;
esac

exit 0


