summaryrefslogblamecommitdiffstats
path: root/qc
blob: 0e3210974fe5359d95f2f4254e98ce975bb8e55e (plain) (tree)

















































































































































































                                                                          
#!/bin/bash
#
## Wrapper script for quart
#
## Takes care of ~/.quart.d
#
## .quart.d
## |-- archive
## |   |-- 190706_Saturday_220700.org
## |   |-- 190707_Sunday_220701.org
## |-- today.org

#
##
## Initialize values
##
#

ED="${EDITOR:-emacs -nw}"
QHOME="${QUART_HOME:-$HOME/.quart.d}"
ARCHIVE="$QHOME"/archive
TODAY_FILE="$QHOME"/today.org
DATE_CMD="date +%y%m%d_%A_%H%m%S"

if [[ -z $(which quart) ]]; then
    echo "quart is not installed"
    exit 1
fi

QUART_CMD=$(which quart)

function archive {
    if [[ ! -d "$ARCHIVE" ]]; then
        echo "archive directory does not exist"
        exit 1
    fi

    if [[ -f "$TODAY_FILE" ]]; then
        mv "$TODAY_FILE" "$ARCHIVE/$($DATE_CMD)"
        return 1
    fi        

    return 0
}

function make_today {
    if [[ ! -d "$QHOME" ]]; then
        echo "quart home directory does not exist"
        exit 1
    fi

    cat > "$TODAY_FILE" << EOF
#+START: 9:00

# quart schedule created at $(date "+%A %d of %B the year of our lord %Y")

# Avaliable task flags:
# ! Urgent, ? Maybe, @ External
# Use `=` to signal a task repeat

EOF
}

function edit_today {
    if [[ ! -f "$TODAY_FILE" ]]; then
        echo "run new first"
        exit 1
    fi

    $ED "$TODAY_FILE"
}

function summary_today {
    if [[ ! -f "$TODAY_FILE" ]]; then
        return 0
    fi

    "$QUART_CMD" "$TODAY_FILE" -s
}

function make_qhome {
    if [[ -d "$QHOME" ]]; then
        echo "QUART_HOME already exists in $QHOME"
    else
        mkdir "$QHOME"
        echo "QUART_HOME initialized in $QHOME"
    fi

    if [[ -d "$ARCHIVE" ]]; then
        echo "archive directory already exists"
    else
        mkdir "$ARCHIVE"
        echo "archive directory created"
    fi
    
}

if [ $# -lt 1 ]; then
    if [[ ! -d "$QHOME" ]]; then
        echo "run init first"
        exit 1
    fi

    if [[ ! -f "$TODAY_FILE" ]]; then
        echo "run new first"
        exit 1
    fi

    echo -e "Current schedule:\n"
    "$QUART_CMD" "$TODAY_FILE" -s
    exit 0
fi

if [[ "$1" == "-" ]]; then
    shift
    "$QUART_CMD" "$TODAY_FILE" $@
    exit $?
fi

case $1 in
    init|i)
        make_qhome
        exit 0
        # create ~/.quart.d director structure
        ;;
    new|n)
        shift
        archive
        make_today
        edit_today
        summary_today
        exit 0
        # backup the old schedule and create a new one
        ;;
    edit|e)
        shift
        edit_today
        summary_today
        exit 0
        # edit the current schedule
        ;;
    remove|rm)
        shift
        archive
        exit 0
        # rm current schedule, after backing it up
        ;;
    cat|c)
        shift
        cat "$TODAY_FILE"
        exit 0
        ;;
    help|h|-h|--help)
        shift
        cat << EOF
qc -- bash wrapper for quart

Usage:
$0 [OPTION]
$0 - [quart option]

OPTIONS:
  init   | i       initialize quart home
  new    | n       create new schedule (archives current one)
  remove | rm      remove current schedule (and archive it)
  edit   | e       edit current schedule
  cat    | c       print current schedule
  help   | h       show this help
  -                pass arguments to quart
EOF
        exit 0
        ;;
    *)
        echo "qc: '$1' is not a valid option"
        shift
        exit 1
        ;;
esac