#!/bin/bash
# Copyright 2011-2016 Chad Lemmen http://www.lemmen.com
#
# Set the Informix environment variables
#. /etc/profile.d/informix.sh


username=$1
password=$2
tmp=/usr/tmp
dldir=$tmp/ss-geteft
bin=`dirname $0`


# Remove the download directory to cleanup old files
rm -rf "$dldir"
# Create download directory, which will stay until this script is run again
mkdir -p "$dldir"

cd "$dldir"
# Download EFT settlement notice.
ncftpget -DD -a -u "$username" -p "$password" ftp://ftp.fintech.net/out/*

# Need to be in program dir for printing to work
cd $bin

for x in `ls $dldir`
do
  if [ -z "$x" ]; then
    continue
  fi
 
  # Convert CSV files to a format that Informix can load
  "$bin"/convertcsv "$dldir"/"$x" > "$dldir"/"$x".csv

  # Pass the downloaded files to the 4GL program. Passing the 'lp' argument 
  # will print the EFT notice after it's imported into the database
  "$bin"/rspbpurch.4ge "$dldir"/"$x".csv lp
done

# Remove empty directories
rm -d "$dldir"

