#!/bin/sh

SENDFILE="no"
DOWNLOADFILE="no"
HOSTPOSITION=1
RSYSTEM2="" # define empty value
unset DIR_FLAG

#########################################################################
# Definition of functions

printUsage() {
  echo "scpr is a wrapper for scp tool. It is designed to transfer"
  echo "files between associated Assistant or SOCO2 Platform systems"
  echo ""
  echo "USAGE: scpr"
  echo "scpr [-c system] [ -d | -s ] [ -h] [ -r] source_file destination_file"
  echo " -c system -define system to communicate with"
  echo " -d -download source_file from remote system"
  echo " -h -print usage"
  echo " -r -recursively copy entire directories"
  echo " -s -send source_file from local system to remote system"
  echo "Error code:"
  echo " 200 -no files or wrong parameter/s specified"
  echo " 202 -remote system is not accessible due to network problem"
  echo " 203 -remote system is not accessible due to problem with key pair"
  echo " 205 -internal error"
  echo " 209 -unable to detect remote system, use -c parameter"
  echo "For more error codes check \"sshr -h\" output"
  
}


endOfProgram() {
  set +x
  tracef "== End of execution (scpr) [$(/bin/date)] (err=$1) == "
  tracef ""
  tracef ""
  exit $1
}


#################################################################
#
# MAIN function
#
#################################################################
# import common variable and functions
. /opt/rcon/bin/sshr-base

cutLog
tracef "== Start of execution (scpr) [$(/bin/date)] =="


HOSTPOSITION=1
#Evaluate options
while getopts "n:sdqhrc:" OPTION; do
    case $OPTION in
        n) HOSTPOSITION=$OPTARG;;
	s) SENDFILE="yes";;
        d) DOWNLOADFILE="yes";;
        q) VERBOSE="no";;
        h) printUsage;endOfProgram 0;;
	c) RSYSTEM2=$OPTARG;;
        r) DIR_FLAG='-r';;
    esac
done
shift $(($OPTIND - 1))

#Parameter handling
if [ "$SENDFILE" = "yes" -a "$DOWNLOADFILE" = "yes" ];then
  tracef "ERROR:incorrect usage"
  printUsage
  endOfProgram 200
fi

if [ "$SENDFILE" = "no" -a "$DOWNLOADFILE" = "no" ];then
  tracef "ERROR:incorrect usage"
  printUsage
  endOfProgram 200
fi
SOURCE_FILE=$1
DESTINATION_FILE=$2
tracef "param:source file: \"$SOURCE_FILE\""
tracef "param:destination file: \"$DESTINATION_FILE\""


#Step1: Detect current system type
SYSTEMTYPE=$(detectSystem)
tracef "current system=\"$SYSTEMTYPE\""

#Step2: Detect remote system type
RSYSTEM=$(getRSystem $SYSTEMTYPE "$RSYSTEM2")
RC=$?
if [ $RC -ne 0 ];then
  exit 209 
fi
tracef "param:remote system=\"$RSYSTEM\""

if [ $SYSTEMTYPE = $RSYSTEM ]; then
  tracef "scpr is running on local system"
  tracef "copy locally:"

  cp $DIR_FLAG $SOURCE_FILE $DESTINATION_FILE
  RES=$?

  tracef "local execution finished (ret=$RC)"
  endOfProgram $RC
fi 

#Step3: Detect remote system IP
SERVERIP=$(/opt/rcon/bin/getremintIP -c $RSYSTEM)
tracef "param:remote IP=\"$SERVERIP\""

#Step4: Get name of user which will be used
RUSER=$(getValue $RSYSTEM user)
tracef "param:remote user=\"$RUSER\""

#####################################################################
#Step5: Test of remote connection, execute simple command (pwd)
TESTRES=$(connectivityTest $SERVERIP $RUSER $RSYSTEM)
RES=$?
if [ $RES -ne 0 ];then
   tracef "ERROR:test of connectivity failed, exit (res=$RES)"
   exit $RES
fi

if [ "x$SYSTEMTYPE" = "xhost_intl" ] ; then
    LOCALINTERMDIR=/var/opt/Assistant/data/scpr-dir
    REMOTEINTERMDIR=/.AS/scpr-dir
else
    if [ "x$RSYSTEM" = "xhost_intl" ] ; then
        REMOTEINTERMDIR=/var/opt/Assistant/data/scpr-dir
        LOCALINTERMDIR=/.AS/scpr-dir
    else
        #both Assistant and CSTA see /.AS
        REMOTEINTERMDIR=/.AS/scpr-dir
        LOCALINTERMDIR=/.AS/scpr-dir
    fi
fi
tracef "INFO: localdir is $LOCALINTERMDIR remote dir is $REMOTEINTERMDIR"
mkdir -p $LOCALINTERMDIR
chmod 777 $LOCALINTERMDIR
tempsubdir=`date +%s`-$SYSTEMTYPE
cd $LOCALINTERMDIR
mkdir $tempsubdir
chmod 777 $tempsubdir
cd $tempsubdir

if [ "$DOWNLOADFILE" = "yes" ]; then
  tracef "download file from remote system"
  #phase1 : execute scp remotely to copy the files/dirs to the share location
  fsshr -x -c ${RSYSTEM%_intl} scp $DIR_FLAG $SOURCE_FILE $REMOTEINTERMDIR/$tempsubdir/.
  #phase2 : copy from share to the final location
  #scp -i $PRIVATE_KEYP -F $SSH_CONFIGF $DIR_FLAG $RUSER@$SERVERIP:$SOURCE_FILE $DESTINATION_FILE
  scp $DIR_FLAG * $DESTINATION_FILE
  RES=$?
fi

if [ "$SENDFILE" = "yes" ]; then
  tracef "send file to remote system"
  #phase1 : copy from source to the share
  scp $DIR_FLAG $SOURCE_FILE .
  #phase2 : execute scp remotely to copy the files/dirs to the destination
  fsshr -x -c ${RSYSTEM%_intl} scp $DIR_FLAG $REMOTEINTERMDIR/$tempsubdir/$(ls -1 *) $DESTINATION_FILE
  #scp -i $PRIVATE_KEYP -F $SSH_CONFIGF $DIR_FLAG $SOURCE_FILE $RUSER@$SERVERIP:$DESTINATION_FILE
  RES=$?
fi

#remove temp dir
cd $LOCALINTERMDIR
rm -rf $tempsubdir

endOfProgram $RES
