#!/bin/bash
#
# __copy1__
# __copy2__
#
CMD=$(basename $0)
CMDVER="1.0"
CMDSTR="$CMD v$CMDVER (2016/04)"

usage()
{
	echo "$CMDSTR

usage: $CMD [options] workdir commandline

options:
  -l file|--log=file	set logfile to 'file' (now: $logfile)
  -k file|--lock=file	set lockfile to 'file' (now: $lockfile)
  -r dir|--vardir=dir	set vardir, for logfile and lockfile
  			(now: $vardir)

  -v|--verbose		be verbose (writes to stdout instead
  			of logfile)
  -q|--quiet		be quiet (default)
  -D[n]|--debug[=n]	set debug, optionally with level
  --			stop processing options

logfile and lockfile defaults are basename of first element of
commandline, followed by prefix .log or .lock

vardir is prepended to logfile and lockfile, if they aren't full
pathnames
" >&2
	[ $# != 0 ] && echo -e "\n$@\n" >&2
	exit 127
}

cleanup()
{
	[ -z "${KU_LOCKFILE:-}" ] || ku_lock_remove
}

show_help()
{
	echo -e "
$CMDSTR

Run task, logging to logfile and using lockfile to avoid concurrent
tasks in the same working dir.

You need to specify, at least:

  - workingdir, the directory full path where the task will run

  - commandline, the commands that will be launched, beware to
  	escape special chars
"
	return 0
}






# (MAIN)

trap 'VERBOSE=true; ku_log "\n*INTR*\n"; exit 255' 1 2 3
trap 'VERBOSE=true; ku_log "\nunexpected error $? at $LINENO\n"' ERR
trap 'cleanup' EXIT
set -e
set -u

VERBOSE=${VERBOSE:-false}
DEBUG=${DEBUG:-false}
DBGLEVEL=${DBGLEVEL:-0}
logfile=
lockfile=
vardir=

while [ $# != 0 ]
do
  case $1 in
    -h|--help)		show_help; exit 0 ;;
    -v|--verbose)	VERBOSE=true ;;
    -q|--quiet)		VERBOSE=false ;;
    -D|--debug)		DEBUG=true ;;
    -D[0-9]|--debug=[0-9])
    			DEBUG=true; DBGLEVEL=$(echo "X$1" | sed -e 's/^X-D//' -e 's/^X--debug=//')
			;;
    -l)	[ $# = 0 ] && usage
	shift
    	logfile=$1
	;;
    --logfile=*)
    	logfile=$(echo "X$1" | sed -e 's/--logfile=//')
	;;
    -k)	[ $# = 0 ] && usage
	shift
    	lockfile=$1
	;;
    --lockfile=*)
    	lockfile=$(echo "X$1" | sed -e 's/--lockfile=//')
	;;
    -r)	[ $# = 0 ] && usage
	shift
    	vardir=$1
	;;
    --vardir=*)
    	vardir=$(echo "X$1" | sed -e 's/--vardir=//')
	;;
    --)			break ;;
    -*|"")		usage "unknown parm: '$1'" ;;

    *) break ;;
  esac
  shift
done
case $VERBOSE in
  true|false) ;;
  *) VERBOSE=false ;;
esac

[ $# -lt 2 ] && usage
workdir=$1
shift

vardir=${vardir:-$workdir}
case $logfile in
  /*) ;;
  "") logfile=$vardir/$(basename "$1").log ;;
  *)  logfile="$vardir/$logfile" ;;
esac
case $lockfile in
  /*) ;;
  "") lockfile=$vardir/$(basename "$1").lock ;;
  *)  lockfile="$vardir/$lockfile" ;;
esac

[ -d "$workdir" ]	|| usage "workdir '$workdir' not found"
cd "$workdir"

set -u

. /lib/ku-base/log.sh
LOGSYSLOG=false
LOGFILE=$logfile
LOGUSER=$(id -un)
LOGGROUP=$(id -gn)

KU_LOCKFILE=$lockfile
. /lib/ku-base/lock.sh

# just wait a little, in case we are called by tools like
# inotify, that responds too quickly
sleep 1

# avoid problems from messy env
export DISPLAY=


ku_cap_logfile
ku_log "started: '$workdir' $*"
$VERBOSE || exec >>"$LOGFILE" 2>&1

ku_lock || {
	ku_log "already running"
	exit 0
}

# run commandline
stat=0
"$@" || stat=$?

ku_lock_remove
ku_log "end status=$stat"
exit $stat

