#!/bin/csh -f

# --------------------------------------------------------------------
# This script writen by Elad & Eddie for Hatamot2 Vault file transfer
# Date: 18.7.11
# Updated by Alexey Terentiev
# Date: 27.02.17 
#
#  eID_2_CRL
# --------------------------------------------------------------------

set PACLI_PATH=/opt/vault/bin
set PACLI_CMD=${PACLI_PATH}/pacli
set UPLOAD_FOLDER=/opt/waterfall2safe
set UPLOAD_FOLDER2=/opt/waterfall2safe/archive
#set DOWNLOAD_FOLDER=/opt/download_folder_replace_if_needed
set VAULT_NAME=tehila
set LOGFILE=/var/log/gov.il/dca/eID_2_Aviv.log 
#set LOGFILE=/dev/tty1
set SAFENAME=Aviv_2_Gamam
set SESSIONID=$$
set PARMFILE=tehila.ini
set USERNAME=EidPP-DCA

 
cd $PACLI_PATH

############
# Pacli Init
############

$PACLI_CMD INIT SESSIONID=$SESSIONID >>& $LOGFILE

set stat=$?
set date=`date`
set cmdname=INIT
set logline="${date},$SESSIONID,${cmdname},STATUS=${stat}"
echo $logline >>& $LOGFILE
if ($stat > 0) then
   echo "exiting script due to last command error" >>& $LOGFILE
   exit 1
endif

###############
# Pacli Default
###############

$PACLI_CMD DEFAULT VAULT="TEHILA" USER=$USERNAME SAFE=$SAFENAME  SESSIONID=$SESSIONID >>& $LOGFILE
set stat=$?
set date=`date`
set cmdname=DEFAULT
set logline="${date},$SESSIONID,${cmdname},STATUS=${stat}"
echo $logline >>& $LOGFILE
if ($stat > 0) then
   echo "exiting script due to last command error" >>& $LOGFILE
   exit 1
endif

######################
# Pacli Definefromfile
######################

$PACLI_CMD DEFINEFROMFILE PARMFILE=$PARMFILE SESSIONID=$SESSIONID >>& $LOGFILE
set stat=$?
set date=`date`
set cmdname=DEFINEFROMFILE
set logline="${date},$SESSIONID,${cmdname},STATUS=${stat}"
echo $logline >>& $LOGFILE
if ($stat > 0) then
   echo "exiting script due to last command error" >>& $LOGFILE
   exit 1
endif

#############
# PACLI LOGON
#############

$PACLI_CMD LOGON LOGONFILE=${USERNAME}.cred AUTOCHANGEPASSWORD=no SESSIONID=$SESSIONID
set stat=$?
set date=`date`
set cmdname=LOGON
set logline="${date},$SESSIONID,${cmdname},STATUS=${stat}"
echo $logline >>& $LOGFILE
if ($stat > 0) then
   echo "exiting script due to last command error" >>& $LOGFILE
   echo "Terminate Session" >>& $LOGFILE
   $PACLI_CMD TERM SESSIONID=$SESSIONID >>& $LOGFILE
   exit 1
endif

################
# PACLI OpenSafe
################
$PACLI_CMD OPENSAFE SESSIONID=$SESSIONID
set stat=$?
set date=`date`
set cmdname=OPENSAFE
set logline="${date},$SESSIONID,${cmdname},STATUS=${stat}"
echo $logline >>& $LOGFILE
if ($stat > 0) then
   echo "exiting script due to last command error" >>& $LOGFILE
   exit 1
endif
#####################
# PACLI STORE FILE  #
######################
cd $UPLOAD_FOLDER
foreach file (*)
 if (!(-d $file)) then
#  cp $file $UPLOAD_FOLDER/old
  $PACLI_CMD STOREFILE FOLDER=root\\to FILE=$file LOCALFOLDER=$UPLOAD_FOLDER LOCALFILE=$file SESSIONID=$SESSIONID
  set stat=$?
  set date=`date`
  set cmdname=STOREFILE
  set logline="${date},$SESSIONID,${cmdname},STATUS=${stat}"
  echo $logline >>& $LOGFILE

  if ($stat > 0) then
   echo "exiting script due to last command error" >>& $LOGFILE
   exit 1
  else
#   rm $file
  endif
 endif
end

#cd $UPLOAD_FOLDER2
#foreach file (*)
#if (!(-d $file)) then
#cp $file $UPLOAD_FOLDER2/old
#$PACLI_CMD STOREFILE FOLDER=root\\from\\archive FILE=$file LOCALFOLDER=$UPLOAD_FOLDER2 LOCALFILE=$file SESSIONID=$SESSIONID
#set stat=$?
#set date=`date`
#set cmdname=STOREFILE
#set logline="${date},$SESSIONID,${cmdname},STATUS=${stat}"
#echo $logline >>& $LOGFILE
#if ($stat > 0) then
#   echo "exiting script due to last command error" >>& $LOGFILE
#   exit 1
#else
#   rm $file

#endif
#endif
#end

##################      
# CLOSING SCRIPT
##################


$PACLI_CMD CLOSESAFE SESSIONID=$SESSIONID >>& $LOGFILE
set stat=$?
set date=`date`
set cmdname="CLOSE SAFE"
set logline="${date},$SESSIONID,${cmdname},STATUS=${stat}"
echo $logline >>& $LOGFILE
if ($stat > 0) then
   echo "exiting script due to last command error" >>& $LOGFILE
   exit 1
endif
$PACLI_CMD LOGOFF SESSIONID=$SESSIONID >>& $LOGFILE
set stat=$?
set date=`date`
set cmdname="LOGOFF"
set logline="${date},$SESSIONID,${cmdname},STATUS=${stat}"
echo $logline >>& $LOGFILE
if ($stat > 0) then
   echo "exiting script due to last command error" >>& $LOGFILE
   exit 1
endif
$PACLI_CMD TERM SESSIONID=$SESSIONID >>& $LOGFILE

set stat=$?
set date=`date`
set cmdname="TERM"
set logline="${date},$SESSIONID,${cmdname},STATUS=${stat}"
echo $logline >>& $LOGFILE
if ($stat > 0) then
   echo "exiting script due to last command error" >>& $LOGFILE
   exit 1
endif
