#!/bin/bash
#################################
#                               #
#   FTP SERVER                  #
#                               #
#   DO NOT EDIT THIS FILE!!     #
#                               #
#################################
ver="1.0"


if [ "$1" != "start" ]; then
  echo -e "\e[31;1m\n Can only be started from within 'inxi-report'!\e[0m\n"
  exit
fi

scrpath=$(readlink -f $0)
workdir=$(dirname $scrpath)
 cd $workdir

source "$workdir/inxi-report.conf"

datum=$(date +'%d %b %Y - %H:%M' )


function ftpconnect () {
ftp -v -n "$ftphost" <<END_SCRIPT
quote USER "$ftpusername"
quote PASS "$ftppassword"
ascii
passive on
put "$docroot/index.html" "$ftppath/index.html"
quit
END_SCRIPT
}  > /tmp/ftp-inxi.tmp
ftpconnect


function notify_send () {
  notify-send $urgency -i $workdir/zlogo "$msg1" "$msg2"
}


if [ "$ftpnotify" = "yes" ]; then
  cat /tmp/ftp-inxi.tmp | awk 'BEGIN{FS=" *"} $1~/^[0-9]+$/ {print $1}' > /tmp/inxi-report.ftp
  while read -r line
  do
    if [ "$line" = "530" ]; then
      msg1=" INXI-REPORT FTP SERVER "
      msg2="Server:   $line Connection failure!"
      urgency="-u critical"
      notify_send
    elif [ "$line" = "550" ]; then
      msg1=" INXI-REPORT FTP SERVER "
      msg2="Server:   $line Remote path failure!"
      urgency="-u critical"
      notify_send
    elif [ "$line" = "226" ]; then
      echo "*** Inxi-report uploaded to FTP server" | tee -a "$HOME/.xsession-errors"
      exit
    fi
  done< <(cat /tmp/inxi-report.ftp)
  rm -f /tmp/ftp-inxi.tmp
  rm -f /tmp/inxi-report.ftp

else
  rm -f /tmp/ftp-inxi.tmp
  rm -f /tmp/inxi-report.ftp
fi
