#!/bin/sh
# -*- Bash -*-
# Xact Payment System Installation Script
# 
# Copyright (c) 1997 Jarlath O'Carroll.
# Author: Jarlath O'Carroll (jarlath@oisin.com) Sat Mar 21 15:37:22 PDT 1997

PRO_INSTALL=true
if [ ! "$rootdir" ] ; then
   set -e
   PRO_INSTALL=""
   if echo '\c' | grep -s c >/dev/null 2>&1
   then
        ECHO_N="echo -n"
        ECHO_C=""
   else
        ECHO_N="echo"
        ECHO_C='\c'
   fi

   echo ""
   echo "     Xact Payment System Installation. ${ECHO_C}"
   echo ""
   echo ""
   echo "Please read and follow the installation instructions exactly. ${ECHO_C}"

   ${ECHO_N} "Where do you wish to install the Xact System [/www/tmp] : ${ECHO_C}"
   read temp
   if [ "$temp" = "" ]; then temp="/www/tmp"; fi
   rootdir=$temp;
   if [ ! -d "$temp" ]; then mkdir $temp; fi
fi

echo ""
echo "   The Xact Payment System is being installed in the '${rootdir}' directory."

xactdir=Xact-CyberCash
smpsfile=smps-2.1.2-linux
#smpsfile=`echo ${smpsfile} | sed -e 's/.tar.gz//' -e 's/${xactdir}\///'`
smpsdir=${rootdir}/smps-2.1.2-linux

# Check to see if there is an existing tar file if so remove it.
if [ -f ${xactdir}/${smpsfile}.tar ]; then
   rm -rf ${xactdir}/${smpsfile}.tar;
fi

# Now Unzip the SMPS tar file
(cd ${xactdir}; \
    cp ${smpsfile}.tar.gz ${smpsfile}.tar.gz.orig; \
    gunzip ${smpsfile}.tar.gz; \
    mv ${smpsfile}.tar.gz.orig ${smpsfile}.tar.gz;)

# Check to see if the SMS distribution is already installed in this directory
# If so we need to remove it to make sure we have a clean install of this latest
# version of Xact and the CyberCash SMPS system.
if [ -d ${smpsdir} ]; then
   echo ""
   echo "   ${smpsdir} file already exists."
   echo "   It is recommended that you remove this previous installation of"
   echo "   SMPS system but MAKE SURE TO BACKUP any existing transactions that you"
   echo "   may have processed using the existing system."
   ${ECHO_N} "   Do you wish to remove the existing SMPS Distribution ? (y/n): ${ECHO_C}"
   read temp
   if [ "$temp" = "y" -o "$temp" = "Y" ]; then
      echo "   Removing ${smpsdir}"
      rm -rf ${smpsdir}/*;
   else
      echo ""
      echo "                    INSTALL STOPPED!"
      echo ""
      exit 1
   fi
fi

echo ""
echo "   Installing the SMPS CyberCash System in ${rootdir}.${ECHO_C}"

# Expanding the tar file and remove unwanted install log script file.
echo "   Expanding the SMPS directory tree in the background ... ${ECHO_C}"
(cd ${xactdir}; tar -xvf ${smpsfile}.tar -C ${rootdir} > ${smpsfile}.log; \
    echo "   SMPS System extraction log:  ${xactdir}/${smpsfile}.log "; \
    echo "   Removing SMPS tar file."; \
    rm -rf ${smpsfile}.tar)
if [ -f xact-install.log ]; then
   (cd ${rootdir}; rm xact-install.log)
fi

# Set the environment var 'AUTOMATIC_INSTALL' to 'true' so that we can avoid
# any interaction with the user for the CyberCash install scripts.
# Make a copy of the original install script then change the script to use
# port 10000 as opposed to 8000 (better chance of it being available).
# Run the CyberCash supplied install script and save the output into
# a local log file (xact-install.log).
echo ""
echo "   Running the CyberCash Install Script in the background ..."
export AUTOMATIC_INSTALL=true
(cd ${smpsdir}; \
cp install install.orig; \
sed -e 's/8000/10000/' ./install > install.tmp; \
mv install.tmp install; \
chmod +x install; \
install 1> xact-install.log 2> xact-install.log)
if [ -d ${smpsdir}/raima/ ]; then
   echo "   CyberCash Install Script log:  ${smpsdir}/xact-install.log"
   echo "   Done. ${ECHO_C}"
else
   echo "${ECHO_C}   IN-COMPLETE SMPS System Install. ${ECHO_C}"
   echo "   Please check the install logs for errors during the installation"
   echo "   of the SMPS System:"
   echo ""
   echo "   ${smpsdir}/xact-install.log or"
   echo "   ${smpsdir}/install.log."
   echo ""
   exit 1
fi

# Build our merchant under which we will run our test suite
# and when ready our active card transactions.
echo ""
echo "   Building the Xact Merchant Directory and Config Files ..."
echo "      What name do you wish to use for this merchant?"
echo "      This is used to create the directory name under which this"
${ECHO_N} "      merchant specific configuration files will be created [xact]: ${ECHO_C}"
read temp
if [ "$temp" = "" ]; then temp="xact"; fi
merchant=$temp;

echo ""
echo "      What secret do you wish to use to identify transactions destined for"
echo "      this merchant?  8 characters of random words rather than a string of"
${ECHO_N} "      random characters is recommended [xact_online_processing]: ${ECHO_C}"
read temp
if [ "$temp" = "" ]; then temp="xact_online_processing"; fi
secret=$temp;

(cd ${smpsdir}/bin; \
build-merchant \
    -type credit \
    -authtype authonly \
    -processor fdc \
    -merchant ${merchant} \
    -creditport 10000 \
    -creditsecret ${secret}; )

if [ -d ${smpsdir}/merchants/xact ]; then
   echo ""
   echo "   Done. ${ECHO_C}"
   echo ""
   if [ ! ${PRO_INSTALL} ] ; then
      echo "   ${xactdir} installation is now complete."
      echo ""
      echo "   Welcome to the World of Online Commerce !!"
      echo ""
   fi
else
   echo ""
   echo "   ${xactdir} installation is IN-COMPLETE. ${ECHO_C}"
   echo ""
   echo "   Please check the install logs for errors during the installation"
   echo "   of this package:"
   echo ""
   echo "   1. ${smpsdir}/${smpsfile}.log or"
   echo "   2. ${smpsdir}/xact-install.log or"
   echo "   3. ${smpsdir}/install.log."
   echo ""
   echo "   If the problem persists please contact Universal Access via E-mail"
   echo "   for support in relation to this installation at xact-support@ua.com."
   echo ""
   exit 1
fi
