#!/bin/bash 

# customize here the appropriate backup location
BACKUPROOT=./backup
ARCHDIR=./arch
TAPEIR=./tape

TABLESET="NA"
PIT=""
OS=`uname`


nextBUID() {

    local TABLESET=$1
    local BRANCH=$2

    cd ${BACKUPROOT}
    
    LOW=0
    for LASTID in `ls ${TABLESET}-${BRANCH}-bu*.tar.gz | sed "s/${TABLESET}-${BRANCH}-bu//" | sed 's/.tar.gz//'`
    do
	if ((  $LASTID > $LOW ))
	then
	    LOW=$LASTID
	fi
    done
    BUID=$(( LOW + 1 ))
    echo $BUID 
}


while getopts "t:a:b:r:p:l" opt; do 
    case $opt in

	t)	    
	    TABLESET=${OPTARG}
	    ;;	    
	p)
	    PIT=${OPTARG}
	    ;;
	b)
	    BRANCH=${OPTARG}
	    ;;

	a)
	    # echo "Option backup was triggered with $OPTARG" >&2 

	    # BUDATE=`date +%Y%m%d-%H%M`

	    BUID=$(nextBUID $TABLESET $BRANCH)
	    
	    echo "BUID=$BUID"
	    
	    # BUID=1
	    # for f in `ls ${BACKUPROOT}/${TABLESET}-${BRANCH}-bu*.tar.gz 2> /dev/null`
	    #do
	    #	echo "f = $f, inc BUID ..."
	    #	BUID=$(( $BUID + 1 ))
	    # done

	    # BUID=`ls ${BACKUPROOT}/${TABLESET}-${BRANCH}-*.tar.gz | wc -l 2> /dev/null`

	    echo "Starting backup .."
	    tar -cvPf ${BACKUPROOT}/${TABLESET}-${BRANCH}-bu${BUID}.tar ${OPTARG} 2>&1 

	    rm -rf ${BACKUPROOT}/${TABLESET}-${BRANCH}-bu${BUID}.tar.gz
	    gzip ${BACKUPROOT}/${TABLESET}-${BRANCH}-bu${BUID}.tar  2>&1

	    if [ $? == 0 ]
	    then
		echo "Backup finished successful"
	    else
		echo "Backup failed"
		exit 1
	    fi
	    ;; 
	l) 
	    # echo "Option List was triggered!" >&2 
	    for f in `ls -t ${BACKUPROOT}/${TABLESET}-${BRANCH}-bu*.tar.gz 2> /dev/null`
	    do

                # linux

                if [ "$OS" == "FreeBSD" ] \
                       || [ "$OS" == "Darwin" ]
                then
                    # osx / freebsd
                    BUTS=`stat -f%SB -t "%Y-%m-%d %H:%M:%S" $f`
                elif [ "$OS" == "Linux" ]
                then
                    # linux 
                    BUTS=`stat -c %x $f | sed 's/\(.*\)\..*/\1/'`
                else
                    echo "Unknown OS"
                    exit 1
                fi

		# BUTS=`stat --printf="%w" $f | sed 's/\..*//'`
		# echo Compare $BUTS and $PIT
		if [[ $BUTS < "$PIT" ]] || [ "$PIT" = "" ]
		then
		    BUID="bu"`basename $f | sed "s/\($TABLESET\-$BRANCH\-bu\)\([0-9][0-9]*\)\(\.tar\.gz\)/\2/"`
		    # BUID=`basename $f | sed 's/\.tar\.gz//'`

		    LASTLOG=`find $ARCHDIR $TAPEDIR -name "$TABLESET\-$BRANCH*"  -type f -exec ls -t \{\} \+ | head -1`

		    if [ "$OS" == "FreeBSD" ] \
			   || [ "$OS" == "Darwin" ]
                    then
			# osx / freebsd
			LOGTS=`stat -f%SB -t "%Y-%m-%d %H:%M:%S" $LASTLOG`
                    elif [ "$OS" == "Linux" ]
                    then
			# linux 
			LOGTS=`stat -c %x $LASTLOG | sed 's/\(.*\)\..*/\1/'`
                    else
			echo "Unknown OS"
			exit 1
                    fi
		    
		    if [ ! "$LOGTS" ]
		    then
			LOGTS=None
		    fi
		   
		    
		    echo ${BUID},${BUTS},${LOGTS}
		fi
	    done 
	    ;; 
	r)
	    echo "Starting restore .."
	    tar -xvPf ${BACKUPROOT}/${TABLESET}-${BRANCH}-${OPTARG}.tar.gz 2>&1
	    if [ $? == 0 ]
	    then
		echo "Restore finished successful"
	    else
		echo "Restore failed"
		exit 1
	    fi
	    ;; 
	\?) 
	    echo "Invalid option: -$OPTARG" >&2 
	    ;; 
	:) 
	    echo "Option -$OPTARG requires an argument." >&2 
	    exit 1 
	    ;; 
    esac 
done
