
#!/bin/bash
# ========================================================================
# imiupload.sh
# This will upload a complete directory with imi files
# ========================================================================
# --- to adapt, directories:
IMI_BASEDIR=/home/nl19471/imi2esb
#
#     <BASEDIR>/etc      : could contain other settings
#     <BASEDIR>/queue    : should contain file.imi to be sent to ESB
#     <BASEDIR>/log      : logfile of transmit
#     <BASEDIR>/history  : when a file is successfully sent it will be moved to here
IMI_ETC=${IMI_BASEDIR}/etc
IMI_QUEUE=${IMI_BASEDIR}/queue
IMI_LOG=${IMI_BASEDIR}/log
IMI_HISTORY=${IMI_BASEDIR}/history
#
# --- define to cleanup history file for files older than x days, "" if not needed
IMI_HISTDAYS="1"
#
# --- ESB CAT environment
IMI_TIMEOUT="10"
IMI_URL="https://161.89.112.93/upload"
# --- credentials
IMI_USER=""

# --- disable proxy
export http_proxy=
export https_proxy=

# --- load server settings for the above IMI_xxx variables
[ -f ${IMI_ETC}/settings.info ] && . ${IMI_ETC}/settings.info

if [[ -z "$IMI_URL" || -z "$IMI_USER" ]] ; then
   [ -z "$IMI_URL"  ] && echo "Error: fill in IMI_URL  in ${IMI_ETC}/settings.info"
   [ -z "$IMI_USER" ] && echo "Error: fill in IMI_USER in ${IMI_ETC}/settings.info"
   exit 0
fi

# --- some sanity checks, make sure directories are there
[ ! -d "$IMI_BASEDIR" ]  &&  mkdir  $IMI_BASEDIR
[ ! -d "$IMI_ETC"     ]  &&  mkdir  $IMI_ETC
[ ! -d "$IMI_QUEUE"   ]  &&  mkdir  $IMI_QUEUE
[ ! -d "$IMI_HISTORY" ]  &&  mkdir  $IMI_HISTORY
[ ! -d "$IMI_LOG"     ]  &&  mkdir  $IMI_LOG

curlopts="--connect-timeout $IMI_TIMEOUT -A "Mozilla" -k -s --anyauth --user ${IMI_USER}"

cd $IMI_QUEUE
# --- count number of imi files
nofiles=$(ls *.imi 2>/dev/null|wc -l)

# --- only transmit files if IMI_URL is set in the server settings
if [ "$nofiles" = "0" ] ; then
   echo "# uploading imi files to url : $IMI_URL"
   echo "- no waiting imi files were found for upload"
elif [ -z "$IMI_URL" ] ; then
   echo "- ERROR: no IMI_URL set in the server settings"
   echo "- skipping upload to of imi files"
   echo "- moving $nofiles imi files to history directory."
   mv *.imi $IMI_HISTORY/
else
   # --- upload all imi files
   nook=0
   noerr=0
   echo "# uploading imi files to url : $IMI_URL"
   for f in $(ls -d *.imi *.att.*.* 2>/dev/null)
   do
      STATUS=$(curl -w '%{http_code}' $curlopts --upload-file $f ${IMI_URL}/$f|tail -1)
      if [ "$STATUS" == "201" ]; then
         echo "- file $f successfully uploaded"
         mv $f $IMI_HISTORY/
         ((nook=nook+1))
      else
         echo "- file $f failed to upload ($STATUS)"
         ((noerr=noerr+1))
         if [ "$noerr" = "1" ] ; then
            echo "opts=$curlopts"
            echo "cmd: curl -w '%{http_code}' $curlopts --upload-file $f ${IMI_URL}/$f"
            echo "- generating debug information"
            curl -v -v $curlopts --trace-ascii /dev/stdout --upload-file $f ${IMI_URL}/$f >${IMI_LOG}/lasterror.log 2>&1
            cat ${IMI_LOG}/lasterror.log|while read line
            do
               echo "> $line"
            done
            echo "skipping upload, IMI/ESB service not responding"
            exit 0
         fi
      fi
   done
   echo "# upload ended, $nook files were sent, $noerr errors found"
   # --- trigger remote processing of uploaded imi files
   echo "done" >${IMI_ETC}/dummy.upload
   curlopts="--connect-timeout $IMI_TIMEOUT -k -s --upload-file ${IMI_ETC}/dummy.upload"
   #echo "cmd: curl -w '%{http_code}' $curlopts -basic --user ${IMI_USER} ${IMI_URL}/done"
   STATUS=$(curl -w '%{http_code}' $curlopts -basic --user ${IMI_USER} ${IMI_URL}/done)
   echo "- sent done"
fi
if [ "$IMI_HISTDAYS" != "" ] ; then
   echo "# Cleaning up old history files"
   cd $IMI_HISTORY
   find . -name "*.imi" -mtime +${IMI_HISTDAYS} -exec rm {} \;
fi
#
exit 0
#EOF

