#!/usr/bin/env rc

# Dbkp - Backup and restore denote directory
# Usage: Dbkp                    (create backup)
#        Dbkp l                  (list backups)
#        Dbkp YYYYMMddTHHmmss    (restore backup)

backupdir=$HOME/Dbkp
mnt=$DENOTE_9MOUNT
if(~ $#mnt 0) mnt=$HOME/mnt/denote

denotedir=`{cat $mnt/dir}
if(~ $#denotedir 0 || ~ $denotedir '') {
	echo 'error: could not read denote directory' >[1=2]
	exit 1
}
denotebase=`{basename $denotedir}

mkdir -p $backupdir

if(~ $#* 0) {
	# Backup mode
	timestamp=`{9 date | awk 'BEGIN {
		m["Jan"]=1; m["Feb"]=2; m["Mar"]=3; m["Apr"]=4
		m["May"]=5; m["Jun"]=6; m["Jul"]=7; m["Aug"]=8
		m["Sep"]=9; m["Oct"]=10; m["Nov"]=11; m["Dec"]=12
	}{
		split($4, t, ":")
		printf "%s%02d%02dT%s%s%s\n", $6, m[$2], $3, t[1], t[2], t[3]
	}'}

	backupfile=$backupdir/$timestamp'-'$denotebase'-dbkp.tar.gz'
	timestampdir=$backupdir/$timestamp
	mkdir -p $timestampdir
	cp -rf $denotedir/* $timestampdir

	echo 'Creating backup:' $backupfile
	cd $backupdir
	9 tar cvzf $backupfile $timestamp
	rm -rf $timestampdir

	if(test -f $backupfile) {
		echo 'Backup created successfully'
	}
	if not {
		echo 'error: backup failed' >[1=2]
		exit 1
	}
}
if not if(~ $#* 1) {
	if(~ $1 l) {
		ls $backupdir | awk -F'[-.]' '{print $1}' | awk -F'/' '{print $NF}'
		exit 0
	}

	timestamp=$1
	if(! echo $timestamp | 9 grep '^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]T[0-9][0-9][0-9][0-9][0-9][0-9]$' >/dev/null >[2=1]) {
		echo 'error: invalid timestamp format (expected YYYYMMddTHHmmss)' >[1=2]
		exit 1
	}

	backupfile=$backupdir/$timestamp'-'$denotebase'-dbkp.tar.gz'
	if(! test -f $backupfile) {
		echo 'error: backup file not found:' $backupfile >[1=2]
		ls $backupdir/*-$denotebase-dbkp.tar.gz >[1=2]
		exit 1
	}

	restoredir=$denotedir/restore
	mkdir -p $restoredir
	echo 'Restoring backup to:' $restoredir
	cd $restoredir
	9 tar xvzf $backupfile

	if(test -d $restoredir/$timestamp) {
		echo 'Backup restored successfully to:' $restoredir/$timestamp
	}
	if not {
		echo 'error: restore failed' >[1=2]
		exit 1
	}
}
if not {
	echo 'usage: Dbkp                   (create backup)' >[1=2]
	echo '       Dbkp l                 (list backups)' >[1=2]
	echo '       Dbkp YYYYMMddTHHmmss   (restore backup)' >[1=2]
	exit 1
}
