#!/bin/sh 
#
#=======================================================================
#
#                      The Ch Language Envirnment 
#               Proprietary and Confidential Information
#
# This material contains, and is part of a computer software program
# which is, proprietary and confidential information owned by 
# SoftIntegration, Inc.  The program, including this material,
# may not be duplicated, disclosed or reproduced in whole or in part for
# any purpose without the express written authorization of 
# SoftIntegration, Inc. All authorized reproductions must be marked with 
# this legend.
#
#                       Copyright (c) 2001-2003
#                        SoftIntegration, Inc.
#                         All rights reserved
#========================================================================
#
#       install.sh -- Install the Ch language environment.
#
#       Usage:    ./install.sh or sh ./install.sh 
#       Description:    This script installs Ch software on a Unix machine
#



PACKAGE="ch.bin"
PRODUCT="Ch"
VERSION="5.1.0"

# See if the lame SYS-V echo command flags have to be used.
#
if test "`/bin/echo 'helloch\c'`" = "helloch\c"
then
        EFLAG="-n"
        ENDER=""
else
        EFLAG=""
        ENDER="\c"
fi
ECHO="/bin/echo ${EFLAG}"


if test ! -r ${PACKAGE}
then
   echo ""
   echo "Error: ${PACKAGE} cannot be found. "
   echo ""
   echo "       It might have been removed after the first successful installation. "
   echo "       You may need to unpack the downloaded file again for reinstallation"
   echo ""
   exit 2
fi

echo ""
echo "=================================================================="
echo ""
echo "             Ch Language Environment Installation"
echo "                  For Ch version ${VERSION}"
echo "        Copyright (c) 2001-2003 by SoftIntegration, Inc."
echo ""
echo "=================================================================="
echo ""
echo ""

cat <<END

BY INSTALLING THIS SOFTWARE YOU ARE CONSENTING TO BE BOUND BY
AND ARE BECOMING A PARTY TO THE AGREEMENT FOUND IN THE FOLLOING
LICENSE AGREEMENT. IF YOU DO NOT AGREE TO ALL OF THE TERMS
OF THIS AGREEMENT, PLEASE DO NOT INSTALL OR USE THIS SOFTWARE.

END

echo ""
echo ""
more ./license.txt

${ECHO} "Do you agree to the license terms? (y/n)[n] ${ENDER}"
read check
if test "${check}" = "y" -o "${check}" = "Y" 
then
  echo ""
else
  exit 0
fi

#Create license
if test -r license.gen
then
  echo ""
  ./license.gen
  if test $? -ne 0
  then
     exit -1
  fi
fi


#default installation directory for ch
INSTALL_DIR="/usr/local/ch${VERSION}"


# CHECK FOR root
user=`id | cut -d ")" -f 1 | cut -d "(" -f 2`

if test "${user}" != "root"
then
  cat << END

Note: 
      You are installing Ch without the 'root' privilege.
      You may not be able to use ch and chs as a login shell.

END
${ECHO} "Do you want to continue? (y/n)[y] ${ENDER}"
      read check
      if test "${check}" = "n" -o "${check}" = "N"
      then 
        exit 0
      fi
fi

if test -r README
then
# To be 100% sure we can find gzip to extract the file.
#
GZIP="gzip"
flag=1
# do not test GZIP for TSLinux
if test "`uname`" = "Linux" 
  then
     flag=0
fi
while test ${flag} -eq 1
do
        ${GZIP} -h >/dev/null 2>&1
        if test $? -ne 0
        then
                echo ""
                echo "Unable to locate 'gzip' in your path."
                echo "Please provide the fully-qualified directory in which it can be found."
                echo ""
                ${ECHO} "Directory containing the 'gzip' binary: ${ENDER}"
                read GZIP_DIR
                GZIP="${GZIP_DIR}/gzip"
        else
                flag=0
        fi
done
fi

if test "${user}" = "root"
then

   # To be 100% sure we can find the executable awk to extract the file.
   # We use the way different from gzip here because similar way does't work here.
   if test -f /usr/bin/awk
   then
     AWK=/usr/bin/awk
   else
     AWK=/bin/awk
   fi
   flag=1
   while test ${flag} -eq 1
   do
        if test ! -x ${AWK}
        then
                echo ""
                echo "Unable to locate '${AWK}' in your path."
                echo "Please provide the fully-qualified directory in which it can be found."
                echo ""
                ${ECHO} "Directory containing the 'awk' binary: ${ENDER}"
                read AWK_DIR
                AWK="${AWK_DIR}/awk"
        else
                flag=0
        fi
   done

   # /usr/local/ch, /usr/ch, /opt/ch, /bin/ch and /bin/ch is expected to be empty or a symbolic link
   #if ((test -r /usr/local/ch) && (test ! -h /usr/local/ch))  || ((test -r /usr/ch) && (test ! -h /usr/ch)) || ((test -r /opt/ch) && (test ! -h /opt/ch)) || ((test -r /bin/ch) && (test ! -h /bin/ch)) || ((test -r /bin/chs) && (test ! -h /bin/chs)) 
   if test -r /usr/local/ch && test ! -h /usr/local/ch  || test -r /usr/ch && test ! -h /usr/ch || test -r /opt/ch && test ! -h /opt/ch || test -r /bin/ch && test ! -h /bin/ch || test -r /bin/chs && test ! -h /bin/chs 
   then
      cat << END

  ** Installation Error: **

  /usr/local/ch, /usr/ch, /opt/ch, /bin/ch and /bin/chs is expected to be empty, or 
  a symbolic link, if existing.  Please check and remove them away before 
  installation. 
  Thank you.

END
      exit 1
   fi
fi


echo ""
echo " You may install Ch to anywhere you want to."
echo " The default directory is ${INSTALL_DIR}, you can hit Return to continue"
echo " or specify the directory path under which the software will be installed."
flag=1
while test ${flag} -eq 1
do
        echo ""
        ${ECHO} "Location for installing ${PRODUCT} language environment [${INSTALL_DIR}]: ${ENDER}"
        read TARGET
        if test -z "${TARGET}"
        then
                TARGET="${INSTALL_DIR}"
        fi

# Change ~ to HOME
        if test ! -z "`echo ${TARGET} | grep '~'`"
        then
                TARGET=`echo ${TARGET} | sed "s:~:${HOME}:"`
        fi

	flag=0
        if test "${TARGET}" = "/usr/local/ch" -o "${TARGET}" = "/usr/ch" -o "${TARGET}" = "/opt/ch"
        then
              echo ""
              echo "   We do not recommend use ${TARGET}, it may conflic with future upgrade."
              echo "   Please select another directory. " 
              echo ""
              flag=1
              continue
        else 
           if test ! -d ${TARGET}
           then
                echo ""
                echo " Directory ${TARGET} does not exist."
                echo ""
                ${ECHO} "Do you want the directory to be created? (y/n)[y] ${ENDER}"
                read check
                if test "${check}" = "n" -o "${check}" = "N"
                then
                        flag=1
                        continue
                fi
           else
           # move the orginal target directory to ${TAREGET}.old${num}
              if test ! -w ${TARGET}
              then
                   echo ""
                   echo "   ERROR: You do no have write permission on ${TARGET}"
                   flag=1
                   continue
              fi

              if test ! -d ${TARGET}.old 
              then 
                        num=''
              else
                 dirFlag=1
                 num=0
                 while test ${dirFlag} -eq 1
                 do
			num=`expr ${num} + 1`
			if test ! -d ${TARGET}.old${num}
                        then
                           dirFlag=0
                        fi
                 done 
              fi

              echo ""
              echo "   Existing '${TARGET}' directory found"
              echo ""
              ${ECHO} "Do you want to move '${TARGET}' to '${TARGET}.old${num}' (y/n)[y] ${ENDER}"
              read check
              if test "${check}" = "n" -o "${check}" = "N"
              then
                 cat << END

      Please make a backup copy of your configuration file if you want to.
      The installation will remove files in directory '${TARGET}', then install Ch. 

END
                  ${ECHO} "Do you want to continue (y/n)[y] ${ENDER}"
                  read check
                  if test "${check}" = "n" -o "${check}" = "N"
                  then
                      flag=1
                      continue
                  else
                      /bin/rm -rf ${TARGET}/*
                      if test $? -ne 0
                      then
                          echo "ERROR: Failed to delete files in ${TARGET}"
                          flag=1
                          continue
                      fi
                  fi
              else
                  # The -p flag may cause an error on some HP-UX systems .

                  echo "   Move ${TARGET} to '${TARGET}.old${num}' now ..."
                  echo ""

                  /bin/mv ${TARGET}  ${TARGET}.old${num}
                  if test $? -ne 0
                  then
                        echo "ERROR: Failed to move ${TARGET} to '${TARGET}.old${num}'"
                        flag=1
                        continue
                  fi
   	          echo ""
                  echo "'${TARGET}' has been moved to '${TARGET}.old${num}'"
                  echo ""
              fi
           fi

           #
           # The -p flag may cause an error on some HP-UX systems.
           # Removing the -p will work for 'basename TARGET' as long
           # as 'dirname TARGET' exists.
           #
           mkdir -p ${TARGET}
           if test $? -ne 0
           then
                   echo "ERROR: Problem in creating directory ${TARGET}"
                   flag=1
                   continue
           fi
           chmod 755 ${TARGET}

       fi # end of the case where target directory is not /usr/local/ch and /bin/ch
done

#
# Begin the installation.
#
echo ""
echo "Installing ${PRODUCT} files, please wait ..."

# Actually install the package in TARGET.
#

if test -r README
then
#    ${GZIP} -dc ${PACKAGE} | (cd ${TARGET}; tar -xmf -)
    ${GZIP} -dc ${PACKAGE} | (cd ${TARGET}; tar -xf -)
else
#    cat ${PACKAGE} | (cd ${TARGET}; tar -xmf -)
    cat ${PACKAGE} | (cd ${TARGET}; tar -xf -)
fi
   

#To make the Linux DLL works under different std++ version
if test "`uname`" = "Linux" && test -f /usr/lib/libstdc++-libc6.2-2.so.3     
then
    if test -f  $TARGET/dl/libcgi6_2.dl
    then
      mv $TARGET/dl/libcgi6_2.dl $TARGET/dl/libcgi.dl
    fi
    if test -f  $TARGET/dl/libwcgi6_2.dl
    then
      mv $TARGET/dl/libwcgi6_2.dl $TARGET/dl/libwcgi.dl
    fi
    if test -f  $TARGET/dl/libchplot6_2.dl
    then
      mv $TARGET/dl/libchplot6_2.dl $TARGET/dl/libchplot.dl
    fi
else
    rm -f $TARGET/dl/libcgi6_2.dl
    rm -f $TARGET/dl/libwcgi6_2.dl
    rm -f $TARGET/dl/libchplot6_2.dl
fi

#changed uid and gid
userid=`id -u`
groupid=`id -g`
chown -R $userid:$groupid $TARGET

echo ""
echo "Files has been successfully installed."
echo ""

# adding the shells for ch 
# considering the situation if /etc/shells no existing or /etc/shells doesn't contain
# /bin/ch 
if test "${user}" = "root"
then
# delete the old symbolic first, if existing 
    if  test -h /usr/local/ch
    then
       /bin/rm -f /usr/local/ch
    fi

   if  test -h /usr/ch
   then
      /bin/rm -f /usr/ch
   fi

   if  test -h /opt/ch
   then
      /bin/rm -f /opt/ch
   fi

   if  test -h /bin/ch
   then
       /bin/rm -f /bin/ch
   fi

   if  test -h /bin/chs
   then
      /bin/rm -f /bin/chs
   fi

   # create the symbolic for Ch
   # if Ch is installed to the /usr/local directory, the symbolic will go there
   # otherwise, it goes to default /usr/ch and /usr/ch/bin/ch
   if test ! -z "`echo ${TARGET} | grep '/usr/local/'`" 
   then
       /bin/ln -s ${TARGET} /usr/local/ch
   elif test ! -z "`echo ${TARGET} | grep '/opt/'`" 
   then
       /bin/ln -s ${TARGET} /opt/ch
   else
      /bin/ln -s ${TARGET} /usr/ch
   fi  

   /bin/ln -s ${TARGET}/bin/ch /bin/ch
   /bin/ln -s ${TARGET}/bin/chs /bin/chs

if test "`uname`" != "Darwin" 
then
   /bin/cp  ${TARGET}/config/.chrc       /etc/skel/.chrc 
   /bin/cp  ${TARGET}/config/.chlogin    /etc/skel/.chlogin 
   /bin/cp  ${TARGET}/config/.chlogout   /etc/skel/.chlogout
fi

# if /etc/shells exists and /etc/shells doesn't contain /bin/ch or /bin/chs

    ch_add_shell=false;
    if test -f /etc/shells
    then
        if test "`${AWK} '/\/bin\/ch$/ { print}' /etc/shells`" != "/bin/ch" 
        then
            ch_add_shell=true;   
        fi
    fi

   # Add /bin/ch, /bin/chs to the /etc/shells
   if test ${ch_add_shell} = "true"
   then
        if test "`${AWK} '/\/bin\/ch$/ { print}' /etc/shells`" != "/bin/ch"
        then 
            echo ""
            ${ECHO} "Do you want ch to be used as one of login shells in the system? (y/n)[y] ${ENDER}"
            read check
            if test "${check}" = "n" -o "${check}" = "N"
            then
                echo ""
                echo " If you ingored this setting, the user will not be able to login" 
                echo " with ftp or telnet if ch is used as the login shell."
                echo ""

                ${ECHO} "Are you sure that you don't want to add ch as a login shell? (y/n)[n] ${ENDER}"
                read check
                if test "${check}" = "y" -o "${check}" = "Y"
                then
                        echo ""
                        echo "You have chosen not to set ch as one of login shells,"
                        echo "you may add /bin/ch to /etc/shells later if you want to. "
                        echo ""
                else
                        if test -f /etc/shells
                        then
                        echo "/bin/ch" >> /etc/shells
                        else
                        echo "/bin/ch" > /etc/shells
                        fi
                fi
           else
                if test -f /etc/shells
                then
                echo "/bin/ch" >> /etc/shells
                else
                echo "/bin/ch" > /etc/shells
                fi
          fi
       fi
  fi  # end of adding shells for ch 
fi    # end of handling root

# create license.key
if test -r license.gen
then
  /bin/mv /tmp/Ch3.0_license.key ${TARGET}/license/license.key
fi

echo ""
${ECHO} "The ${PRODUCT} language environment version ${VERSION} installation is complete. ${ENDER}"
echo ""
echo ""

#
#
# Get rid of the trash.

if test -r README
then
    ${ECHO} "Garbage cleaning begins ... ${ENDER}"
    echo ""
    /bin/rm -f ${PACKAGE}
    /bin/rm -f install.sh 
    if test -r license.gen
    then
       /bin/rm -f license.gen
    fi
    ${ECHO} "Garbage cleaning ends.  ... ${ENDER}"
fi

echo ""

if test "${user}" != "root"
then
  cat << END

Note: 
      You are installing Ch without the 'root' privilege.

      If you use 'csh' or 'tcsh' shell,  
      you need to add the following commands to the startup file
      .cshrc or .tcshrc in your home directory, respectively, 
      and restart csh or tcsh by typing either 'csh' or 'tcsh'.
           setenv CHHOME ${TARGET}
           set path=(${TARGET}/bin \$path)
           set MANPATH=(\$MANPATH ${TARGET}/docs/man)
 
      If you use 'sh', 'ksh', or 'bash' shell,  
      you need to add the following commands to the startup file
      .profile, .profile, or .bash_profile in your home directory, respectively, 
      and restart sh, ksh or bash by typing either 'sh', 'ksh' or 'bash'.
           export CHHOME=${TARGET}
           export PATH=${TARGET}/bin:\$PATH
           export MANPATH=\$MANPATH:${TARGET}/docs/man

      Getting started in Ch, type
           ch
           help

END
else
  cat << END

      If you use 'csh' or 'tcsh' shell,  
      you need to add the following command to the startup file
      .cshrc or .tcshrc in your home directory, respectively.
           set MANPATH=(\$MANPATH ${TARGET}/docs/man)

      If you use 'sh', 'ksh', or 'bash' shell,  
      you need to add the following command to the startup file
      .profile, .profile, or .bash_profile in your home directory, respectively.
           export MANPATH=\$MANPATH:${TARGET}/docs/man

      Getting started in Ch, type
           ch
           help
END

fi

exit 0

