ZIM_VERSION=9.00
ZIM_VERSION=9.00
ErrorMessage ()
{
   echo "*** The activation key is invalid possibly because one of these reasons:"
   echo "   1. The name of the company is not exactly as the label;"
   echo "   2. The activation key is invalid;"
   echo "   3. The activation key is not for this environment;"
   echo "   4. The serial number is incorrect."
   echo
}

# This code is Copyright (c) Zim Technologies Int'l Inc. 1983-2002
#     All rights reserved.
#

umask 022

CURRENTDIR=`pwd`
ZIMCOMMON=common.$ZIM_VERSION.tar
ZIMSERV=services.$ZIM_VERSION.tar
ZIMZIM=zim.$ZIM_VERSION.tar
ZIMEXAMPLE=example.$ZIM_VERSION.tar
ZIMJDBCSAM=jdbcsam.$ZIM_VERSION.tar
ZIMGCC=gcc.$ZIM_VERSION.tar
ZIMWEB=zimweb.$ZIM_VERSION.tar

echo ""
echo ""
echo ""
echo "Zim Software Installation"
echo ""

if [ "$#" = "1" ]
then
   if [ "$1" != "-r" ]
   then
      echo ""
      echo "\"$1\": Invalid option"
      echo "Use option -r to perform re-installation."
      echo ""
      echo ""
      exit
   fi
fi

# Begin the installation by accepting the LA.
echo ""
echo "Please read the Zim Software License agreement"
echo "and either accept or reject the terms prior to continuing"
echo "with the installation."
echo ""
echo "Press <enter> to read the Zim Software License agreement:"
read a
more license.unx
echo ""
echo ""
echo "Do you accept the terms of the above License Agreement?"
echo 'Enter "ACCEPT" to accept or "REJECT" to reject the above terms:'
read a
echo ""
echo ""
if [ "$a" = "accept" -o "$a" = "ACCEPT" ]
then
   echo "Accepting..."
else
   echo "Software License Agreement declined."
   echo "Installation cannot proceed."
   exit 1
fi

if [ "$#" = "1" ]
then
   if [ ! -f $HOME/.zim ]
   then
      echo ""
      echo "Re-installation request of ZIM software cannot be performed."
      echo "ZIM products have NOT been installed before."
      echo ""
      exit
    fi

    INSTALL_DIR=`cat $HOME/.zim | sed 's/^.*://' | sed 's/^ *//'`

    ZIM=$INSTALL_DIR

    if [ "$ZIM" = "" ]
    then
       echo ""
       echo "Re-installation request of ZIM software cannot be performed."
       echo "Previously installed directory specfied in .zim file is empty."
       echo ""
       exit
    fi

    if [ ! -d $ZIM ]
    then
       echo ""
       echo "Re-installation request of ZIM software cannot be performed."
       echo "Previously installed directory $ZIM specified in .zim file can NOT be located."
       echo ""
       exit
    fi

    if [ ! -f $ZIM/zimactiv.zim ]
    then
       echo ""
       echo "Re-installation request of ZIM software cannot be performed."
       echo "Product keys of ZIM software previously installed in $ZIM are NOT found."
       echo ""
       exit
    fi

    echo ""
    echo "If you want to change the directory, type the new one at the prompt."
    echo "Otherwise, press <enter> to accept the current directory $ZIM"
    read answer

    if [ "$answer" != "" ] && [ "$ZIM" != "$answer" ]
    then
        ZIM=$answer
        test ! -d $ZIM && mkdir -p $ZIM || true
        if [ ! -d $ZIM ]
        then
           echo "ERROR: Unable to create the install directory $ZIM."
           echo "ERROR: Installation cannot proceed."
           exit 1
        fi
    fi

    cp    $INSTALL_DIR/zimactiv.zim $ZIM 2> /dev/null
    cp     zimactiv $ZIM
    chmod  555 $ZIM/zimactiv

    test -f $INSTALL_DIR/zimdb.zim     && cp $INSTALL_DIR/zimdb.zim     $ZIM/zimdb_bk.zim     || true
    test -f $INSTALL_DIR/zimconfig.srv && cp $INSTALL_DIR/zimconfig.srv $ZIM/zimconfig_bk.srv || true
	test -f $INSTALL_DIR/config.xml    && cp $INSTALL_DIR/config.xml $ZIM/config_bk.xml       || true
	
    export ZIM=$ZIM

    # May be needed in the future ...
    # for ii in $ZIMCOMMON $ZIMSERV $ZIMZIM $ZIMEXAMPLE $ZIMJDBCSAM $ZIMWEB
    #
    for ii in $ZIMCOMMON $ZIMSERV $ZIMZIM $ZIMEXAMPLE $ZIMJDBCSAM
    do
        echo "Installing file \"$ii\" ..."
        cd   $CURRENTDIR
        if [ -f $ii ]
        then
           cp $ii $ZIM/$ii
           cd $ZIM && tar -xof $ii
        fi
    done

    cd $CURRENTDIR

    chmod -R 744 $ZIM
    chmod    755 $ZIM/zim $ZIM/zimserver $ZIM/zimactiv $ZIM/zimdd $ZIM/zdbagent $ZIM/timserver
    chmod    755 $ZIM/zimfiles $ZIM/zimfix $ZIM/zimtcap

    if [ -f $ZIM/zimdb_bk.zim ]
    then
        cp $ZIM/zimdb_bk.zim $ZIM/zimdb.zim
        rm $ZIM/zimdb_bk.zim
    fi

    if [ -f $ZIM/zimconfig_bk.srv ]
    then
        cp $ZIM/zimconfig_bk.srv $ZIM/zimconfig.srv
        rm $ZIM/zimconfig_bk.srv
    fi
	
	if [ -f $ZIM/config_bk.xml ]
    then
        cp $ZIM/config_bk.xml $ZIM/config.xml
        rm $ZIM/config_bk.xml
    fi

    # Adjust the permissions.
    chmod -R 744 $ZIM 2>/dev/null
    chmod 644 $ZIM/*.sql $ZIM/*.zim $ZIM/*.srv $ZIM/*.jar $ZIM/*.cmd $ZIM/*.ztcap 2>/dev/null
    chmod 755 $ZIM/zim $ZIM/zimserver $ZIM/zimactiv $ZIM/zimdd $ZIM/zdbagent $ZIM/timserver $ZIM/zimfiles $ZIM/zimfix $ZIM/zimtcap 2>/dev/null
    #chmod 755 $ZIM/zimbackup

    echo "Removing temporary files..."
    rm -rf $ZIM/*.tar $ZIM/sort.lst $ZIM/temp.lst

    echo "Previous ZIM installation directory: $ZIM" > $HOME/.zim

    echo ""
    echo ""
    echo "ZIM re-installation completed."
    echo ""
    echo ""

    exit 1
fi

# Determine the installation directory.
ZIM=/opt/zim/$ZIM_VERSION
echo ""
echo ""
echo ""
echo "All ZIM products will be installed in the following directory:"
echo ""
echo $ZIM
echo ""
echo ""
echo "If you want to change the directory, type the new one at the prompt."
echo "Otherwise, press <enter> to accept the current one."
read a
test "$a" != "" && ZIM=$a || true
test ! -d $ZIM && mkdir -p $ZIM || true
if [ ! -d $ZIM ]
then
   echo "ERROR: Unable to create the install directory $ZIM."
   echo "ERROR: Installation cannot proceed."
   exit 1
fi
export ZIM

# Install the GCC libraries if present.
if [ -f $ZIMGCC.Z ]
then
   mkdir -p /usr/local/lib
   mkdir -p /usr/local/lib/gcc-lib/powerpc-ibm-aix4.3.2.0/3.3.3
   uncompress -c $ZIMGCC > /usr/local/lib/$ZIMGCC
   cd /usr/local/lib && tar -xof $ZIMGCC
   cd $CURRENTDIR
fi

# Read the company name to install the products.
cp zimactiv $ZIM
chmod 555 $ZIM/zimactiv
while true
do
   echo ""
   echo ""
   echo "Enter the Company Name to activate all the keys for this installation"
   echo "exactly as printed in the label or enter \"exit\"."
   echo "(The \"exit\" breaks the installation)"
   read COMPANY
   test "$COMPANY" = "exit" && exit 1 || true
   test "$COMPANY" != "" && break || true
done

# Activate product by product and build a list of activated products.
PRODLIST=""
while true
do
   echo " "
   echo " "
   echo "Enter the Serial Number (5 digits), \"end\" or \"exit\":"
   echo "(The \"end\" indicates no more products to activate)"
   echo "(The \"exit\" breaks the installation)"
   read SERIAL
   test "$SERIAL" = "end" && break || true
   test "$SERIAL" = "exit" && exit 1 || true
   echo " "
   echo " "
   echo "Enter the Activation Key:"
   read ACTKEY
   echo "$ZIM/zimactiv -a $SERIAL $ACTKEY \"$COMPANY\" 2> /dev/null" > $ZIM/temp.lst
   echo "$ZIM/zimactiv -p $SERIAL $ACTKEY \"$COMPANY\" > $ZIM/sort.lst 2> /dev/null" >> $ZIM/temp.lst
   chmod 777 $ZIM/temp.lst
   $ZIM/temp.lst
   test -s $ZIM/sort.lst && PRODLIST=$PRODLIST" "`cat $ZIM/sort.lst` || ErrorMessage
done
if [ "$PRODLIST" = "" ]
then
   echo "No products activated to be installed."
   echo "Installation ended."
   break
else
   # Create a list of (uniquely) installable products.
   test -f $ZIM/temp.lst && rm -f $ZIM/temp.lst || true
   j=0
   for i in $PRODLIST
   do
      case $i in
         1) echo $ZIMCOMMON $ZIMSERV $ZIMZIM $ZIMEXAMPLE >> $ZIM/temp.lst;;
         3) echo $ZIMJDBCSAM >> $ZIM/temp.lst;;
         4) echo $ZIMWEB >> $ZIM/temp.lst
            j=1;;
         5) echo $ZIMZIM >> $ZIM/temp.lst;;
      esac
   done

   test -f $ZIM/sort.lst && rm -f $ZIM/sort.lst || true

   for i in `cat $ZIM/temp.lst`
   do
      echo $i >> $ZIM/sort.lst
   done

   sort -u -o $ZIM/temp.lst < $ZIM/sort.lst

   # Install the products and the documentation.
   echo " "
   echo " "
   for i in `cat $ZIM/temp.lst`
   do
      echo "Installing file \"$i\"..."
      cd $CURRENTDIR
      if [ -f $i ]
      then
         cp $i $ZIM/$i
         ##uncompress -c $i > $ZIM/$i
         cd $ZIM && tar -xof $i
      fi
   done
   cd $CURRENTDIR

   # Adjust the permissions.
   chmod -R 744 $ZIM 2>/dev/null
   chmod 644 $ZIM/*.sql $ZIM/*.zim $ZIM/*.srv $ZIM/*.jar $ZIM/*.cmd $ZIM/*.ztcap 2>/dev/null
   chmod 755 $ZIM/zim $ZIM/zimserver $ZIM/zimactiv $ZIM/zimdd $ZIM/zdbagent $ZIM/timserver $ZIM/zimfiles $ZIM/zimfix $ZIM/zimtcap 2>/dev/null
   #chmod 755 $ZIM/zimbackup

   # Adjust ZimWeb if installed.
   if [ j = 1 ]
   then
       z=$ZIM/ZimWeb
       sed s.\\[ZIMWEB\\].$z.g $z/tomcat/webapps/ZII/WEB-INF/web.ori > $z/tomcat/webapps/ZII/WEB-INF/web.xml
       chmod 777 $z/tomcat/webapps/ZII/WEB-INF/web.xml
       rm $z/tomcat/webapps/ZII/WEB-INF/web.ori
       echo "export ZIMWEB=$z" > $z/bin/set_env.sh
       chmod 777 $z/bin/set_env.sh
   fi

   echo "Removing temporary files..."
   rm -rf $ZIM/*.tar $ZIM/sort.lst $ZIM/temp.lst

   echo "Previous ZIM installation directory: $ZIM" > $HOME/.zim

   echo " "
   echo " "
   echo "ZIM installation completed."
   echo " "
   echo " "
fi

