#!/bin/bash


# Can't always trust $PWD
CURRWD=$(pwd)

# Make sure nbfake host and logfile are set. Fall back to old-style variable names

if [[ -z "$NBFAKE_HOST" && -n "$TARGETHOST" ]];     then NBFAKE_HOST=$TARGETHOST;     fi

if [[ -z "$NBFAKE_LOG" && -n "$TARGETHOST_LOGF" ]]; then NBFAKE_LOG=$TARGETHOST_LOGF; fi

# Set default nbfake log

if [[ -z "$NBFAKE_LOG" ]]; then
    NBFAKE_LOG=NBFAKE-$(date +%GWW%V.%u_%T)
fi

cat <<__EOF >&2
#======================================================================
# NBFAKE logging command to: $NBFAKE_LOG
#     $*
#======================================================================
__EOF

if [[ -z "$NBFAKE_HOST" ]]; then
  sh -c "cd $CURRWD;export DISPLAY=$DISPLAY; export PATH=$PATH; nohup $* >> $NBFAKE_LOG 2>&1 &"
else
  ssh -n -f $NBFAKE_HOST "sh -c \"cd $CURRWD;export DISPLAY=$DISPLAY; export PATH=$PATH; nohup $* >> $NBFAKE_LOG 2>&1 &\""
fi
