#! /bin/bash
#
# Control crontab tasks for oradba packages
#
# Use:
#
#    oradba-crontab --add|--del|--upd|--chk path_to_cron_d_file
#
#

source /lib/lsb/orabase-functions 2>&- || { echo "File /lib/lsb/orabase-functions is not exists" >&2 ; exit 1; }

# Operational variables ----------------------------------------------------------------
BASE_DIR=`dirname $0`
EXE_NAME=`basename $0`
CRON_USER=oracle
CRON_DIR=/etc/oradba/cron.d

# Global variables ---------------------------------------------------------------------
#

# Procedures and functions -------------------------------------------------------------
usage() {
  cat << __EOFF__

Add, remove, change, update crontab jobs for user $CRON_USER.

Use:  $0 [--user USER] --add|--del|--upd script_name [sript_name ...]
   --add                -- Add content of script_name file into users' crontab
   --del | --delete     -- Remove content of script_name file from users' crontab
   --upd | --update     -- Update content of script_name file from users' crontab
   --chk | --check      -- Check existence of content of script_name file into users' crontab

   --user USER          -- Crontab user name (default $CRON_USER) 

   sript_name           -- cron.d formatted file

__EOFF__
}

add_func() {
    local fname="$1"
    local tmpfile=/tmp/crontab$$
    local rc

    crontab -u $CRON_USER -l 2>&- > $tmpfile
    rc=$?
    if ! grep --silent --no-message "^## -= $file" $tmpfile ; then
        # Add new content
        sed --in-place -e '/^# End-of-file #/d' $tmpfile
        echo >> $tmpfile
        echo "## -= $fname : oradba-crontab start =- ##" >> $tmpfile
        cat $CRON_DIR/$fname >> $tmpfile
        echo "## -= $fname : oradba-crontab end =- ##"  >> $tmpfile
        echo >> $tmpfile
        echo -n "# End-of-file #" >> $tmpfile

        # Install new crontab file
        crontab -u $CRON_USER $tmpfile 2>&-
        rc=$?
    fi

    [ -f $tmpfile ] && rm -f $tmpfile

    return $rc
}

del_func() {
    local fname="$1"
    local tmpfile=/tmp/crontab$$
    local rc

    crontab -u $CRON_USER -l 2>&- > $tmpfile
    rc=$?
    if grep --silent --no-message "^## -= $file" $tmpfile ; then
        # Delete content
        bs=`grep --line-number "^## -= $fname : oradba-crontab start =- ##" $tmpfile | cut -d ':' -f 1 || echo 0`
        be=`grep --line-number "^## -= $fname : oradba-crontab end =- ##" $tmpfile | cut -d ':' -f 1 || echo 0`
        [ -z "$bs" ] && bs=0
        [ -z "$be" ] && be=0
        if [ -n "$bs" -a -n "$be" -a $bs -gt 0 -a $be -gt $bs ] ; then
            sed --in-place "$bs,$be d" $tmpfile

            # Install new crontab file
            crontab -u $CRON_USER $tmpfile 2>&-
            rc=$?
        else
            rc=0
        fi
    fi

    [ -f $tmpfile ] && rm -f $tmpfile

    return $rc
}

chk_func() {
    local fname="$1"
    local rc

    crontab -u $CRON_USER -l 2>&- | grep --silent --no-message "^## -= $fname" >/dev/null
    #FIXME: check content with diff
    rc=$?

    return $rc
}

# Main program =========================================================================

# Set output mode
if [ -z "$TERM" -o "$TERM" = "dumb" ]; then
   # Disable output to console
   export ORABASE_QUIET="TRUE"
fi

if [ $# -eq 0 ] ; then
   usage
   exit 0
fi

TEMP=`getopt -o h --long help,add,del,delete,upd,update,chk,check,user: \
             -n "$EXE_NAME" -- "$@"`

[ $? != 0 ] && orabase_error_exit "Terminating..."

eval set -- "$TEMP"

while true ; do
    case "$1" in
        --add) mode=add ; shift ;;
        --del|--delete) mode=del ; shift ;;
        --upd|--update) mode=upd ; shift ;;
        --chk|--check)  mode=chk ; shift ;;
        --user) CRON_USER="$2"; shift 2 ;;
        -h|--help) usage; exit 0 ;;
        --) shift ; break ;;
        *) orabase_error_exit "Internal error!" ;;
    esac
done

for file do
    if [ ! -r "$CRON_DIR/$file" ]; then
        orabase_error "Can't read file $CRON_DIR/$file"
        [ "$mode" = "add" -o "$mode" = "upd" ] && continue
    fi

    case "$mode" in
      add ) add_func "$file"
            rc=$?
            if [ $rc -eq 0 ] ; then
                orabase_okay "Content of file $CRON_DIR/$file added"
            else
                orabase_error "Error while adding content of file $CRON_DIR/$file"
            fi
      ;;
      del ) del_func "$file"
            rc=$?
            if [ $rc -eq 0 ] ; then
                orabase_okay "Content of file $CRON_DIR/$file removed"
            else
                orabase_error "Error while removing content of file $CRON_DIR/$file"
            fi
      ;;
      upd ) del_func "$file"
            add_func "$file"
            rc=$?
            if [ $rc -eq 0 ] ; then
                orabase_okay "Content of file $CRON_DIR/$file updated"
            else
                orabase_error "Error while updating content of file $CRON_DIR/$file"
            fi
      ;;
      chk ) chk_func "$file"
            rc=$?
            if [ $rc -eq 0 ] ; then
              orabase_okay "Content of file $CRON_DIR/$file exists into crontab"
            else
              orabase_error "Content of file $CRON_DIR/$file is not exists into crontab"
            fi
      ;;
      * ) usage; exit 1;
    esac
done

exit 0
