#!/bin/sh

# Installer for WordPerfect Office 2000 for Linux 
# Copyright 1999-2001 Corel Corporation

# Modified By: Andrew S - linux_tech66@corel.com ( Constructive feedback ONLY please )   
# Version: v1.5.3   
# Date: 6/21/01 

# This script has had several changes made to it since its original release.  The 
# following list will outline precisely what has been done: 
#
# * Created a subroutine which will attempt to autodetect the Office2000 CD-ROM.    
# * Added a statement which will assume /mnt/cdrom as the default package source if none is entered
# * Added a script which checks for available disk space on the users system.  The install will report exactly how much 
#   disk space will be used in both the install directory and the doc directory. ( /usr/lib/wpohelpfiles )    
# * Added a statement which checks for the presence of libncurses.so.4 and will sym link this file to 
#   libncurses.so.5 if neccesary.  If libncurses is not found on the system the installation of the WINE rpm 
#   is forced.  
# * Added --force to the fontastic RPM.  
# * Created a script which will check what version of Xfree86 is being run and modify the wpolauncher file if Xfree86 v4.0
#   is detected.   
# * Added a line to copy application icons to /opt/kde/share/applnk/ as some distros use this directory  
# * Added a line to copy /usr/sbin/fonttastic over to /etc/init.d 
# * Added a line to delete any Office2000 configuration files from the /root dir incase they are corrupt.  
# * Checks for the presence of the runparts executable. If it is not found the setupWPO2000 script is modified accordingly 
# * Created a script which checks to see if Glibc's LC_ALL variable is set to en.  If not it exports the variable and adds the 
#   it to /etc/profile 
# * Added support for LINUX BSD init based systems such as Slackware.  
# * Went through the scripts and setup proper indentation on all statements.  After all my modifications it was a wee 
#   bit hard to read.  

# All changes from v1.0 - v1.3.4 are listed above - I did not keep track of individual version changes up until now..  

# * v1.3.5 - Added a if statement to check if /etc/init.d is a sym link.  If it is its renamed to something else and fonttastic 
#   is installed.
# - Modified the sed script which fixes the FontPath problem in Xfree v4.0 to append "xset q | grep 'tcp/.*:7102' > /dev/null" 
#   (Thanks to Gavriel State for pointing this out to us)   
#
# * v1.4.0 - Appended the uninstall script to this install script. 
#
# * v1.4.1 - The installer would automatically delete any old menu icons that resided in 
#  /usr/X11R6/share/applnk/Applications/WPO2000.  This would cause problems for users who wanted 
#  to run the install on top of thier existing installation.  
#
# * v1.5.1 - Some systems have OLE installed which will cause wine to crash if it attempts to dynamically
#   link to one of those libraries. Included a subroutine to modify the master wine.conf and change ole so that its builtin.     
# - Made some changes to the scripts logging capabilities.  All statements which our normally echoed to stdout
#   are redirected to $logfile - This produces a cleaner look
# - Changed the setup so that it would only export LC_ALL if the user is running Glibc v2.1.3
# - Added a line to copy kde icons to /opt/kde2/ 
# - Added a subroutine to check for libsafe and give the user the option to disable it.  
# 
# * v1.5.2 - Added version numbering to the opening statement.
# - Added a line to copy kde icons to /usr/share/applnk-mdk.    
# - Added a warning to tell Mandrake v8.x users that a libc compatibility module is required.
#
# * v1.5.3 - If libncurses is a sym link the install script will try and install libwine by doing a force as the RPM database does not 
#   keep track of this particular sym link.  




echo "Welcome to the WordPerfect Office 2000 install/uninstall script v1.5.3"


i=0

while [ "$i" = 0 ] ; do  

 echo ""
 echo "1 - Install"
 echo "2 - Uninstall"
 echo "3 - Exit" 
 echo -n "Enter choice ->"
 read INSTALLUNINSTALL 

 if [ "$INSTALLUNINSTALL" = 1 -o "$INSTALLUNINSTALL" = 2 -o "$INSTALLUNINSTALL" = 3 ] 
  then 
   i=1 
  else 
   echo "Invalid menu option: $INSTALLUNINSTALL"
 fi
done

if [ "$INSTALLUNINSTALL" = 3 ] 
 then 
  echo "Exiting..." 
  exit
fi 

if [ "$INSTALLUNINSTALL" = 1  ] 
 then  

  # Unset LC_ALL so that perl does not complain or give warnings 
  unset LC_ALL 
  
  # Some Variable Declerations

  docdir=/usr/doc
  current=`pwd`
  RPMPATH=/dists/redhat/i386
  logfile=log.txt
  DIR=/usr/lib

  # The /usr/doc directory does not usually exist 

  mkdir -p /usr/doc 2> /dev/null

  # Setup the logfile - if enabled 

  echo "" > $logfile 
  echo "Office2000 Logfile `date`" >> $logfile
  echo "NOTE: Most of the error messages in this log file can be ignored." >> $logfile 

  # Clear the screen 
  clear


  # Simple case statement to determine parameters passed to the setup script. More options may be added later 

  case $* in
   ("--nodetect")
     CDAUTODETECT=NO
     FLAG=0
     ;;
  esac


  # CD-ROM Autodetection subroutine  

  if [ "$CDAUTODETECT" = NO ]
   then 
  echo "Skipping CD Autodetection!!" 
   else 

    echo "The installer will attempt to autodetect your CD-ROM.  If this causes problems re-run the install"
    echo "with the --nodetect flag.  (e.g. ./install --nodetect)"

    # Stat all mounted filesystems first
  
    FLAG=0
    MOUNTPOINTS=`df | grep "/" | awk '{print $6}'`

    echo "Checking for Office2000 CD"
    echo ""
  
    for i in $MOUNTPOINTS ; do
     if [ -f $i/WPO2000_User_Guide.pdf ]
      then
       CDROM="$i$RPMPATH"
       FLAG=1
       echo "Checking $i...  Found"
     fi
    done

    # If no mounted file systems contain the CD - try and mount any cd-rom devices found in /etc/fstab
 
    if [ "$FLAG" = 0 ]
     then
      MOUNTDEVICES=`cat /etc/fstab | grep iso9660 | awk '{ print $1 }'`
      if [ "$MOUNTDEVICES" != '' ]
       then
        mkdir -p /tmp/mnt
        for SINGLEDEVICE in $MOUNTDEVICES ; do
         echo -n "Checking $SINGLEDEVICE...  "
         mount $SINGLEDEVICE /tmp/mnt -t iso9660 2> /dev/null

         if [ -f /tmp/mnt/WPO2000_User_Guide.pdf ]
          then
           echo "Found"
           CDROM="/tmp/mnt$RPMPATH"
           FLAG=1
          else
           umount -f /tmp/mnt 2> /dev/null
           echo "CD-ROM Not Found"
           FLAG=0
         fi
        done
       else
        # Check the obvious /dev/cdrom
        mkdir -p /tmp/mnt
        mount /dev/cdrom /tmp/mnt 2> /dev/null
         if [ -f /tmp/mnt/WPO2000_User_Guide.pdf ]
          then
           echo "Checking /dev/cdrom...  Found"
           CDROM="/tmp/mnt$RPMPATH"
           FLAG=1
         else
          echo "CD-ROM Not Found"
          FLAG=0
        fi 
      fi
    fi
  fi


  # Prompt the user to enter in a mount directory if the CDROM could not be found. 

  if [ "$FLAG" = 0 ] 
   then
    echo "" 
    echo "Please enter directory where the Office2000 CD is mounted" 
    echo -n "(e.g. /mnt/cdrom) ->" 
    read CDROM
    CDROM=$CDROM$RPMPATH
    if [ "$CDROM" = '' ]
     then
      echo "Ok, using default directory /mnt/cdrom."
      CDROM=/mnt/cdrom$RPMPATH
    fi
  fi 



  # Make sure CD appears to be there

  if [ ! -d "$CDROM" ]
   then
    echo "Unable to find RPM directory on CD. Check that the CD is mounted and"
    echo "the correct path was specified and run the script again."
    exit 1
  fi

  RPMPATH=$CDROM

  # Determine whether Standard or Deluxe edition

  if [ -f $CDROM/wpo2000-full-dlx-*.rpm ]
   then
    EDITION=deluxe
   else
    EDITION=standard
  fi

  # Determine whether system uses glibc 2.0 or 2.1

  if [ -f /lib/libc.so.6 ]
   then
     GLIBC=`strings /lib/libc.so.6 | grep "GNU C Library" | awk '{print $7}' | cut -c 1-3`
     if [ ! -f /usr/bin/strings ]
      then 
       echo "Strings does not appear to exist on your machine - asuming that you have Glibc v2.1" 
       GLIBC=2.1 
     fi
     if [ $GLIBC != "2.0" -a $GLIBC != "2.1" ]
      then
       GLIBC="2.1"
     fi
   else
     echo "Library file /lib/libc.so.6 not found. Unable to determine C library version."
     exit 1
  fi

  # Ask whether to install minimal or full

  i=0
  while [ "$i" = 0  ] ; do
   echo "" 
   echo "Select install option:"
   echo "1 - Full install"
   echo "2 - Minimal install"
   echo -n "Enter choice ->" 
   read ANS
   if [ "$ANS" = "1" ]
   then
     INSTALL=full
     CORELDIR_DLX_FULL=317
     CORELDOCDIR_DLX_FULL=132
     CORELDIR_STD_FULL=268
     CORELDOCDIR_STD_FULL=88
     i=1
   elif [ "$ANS" = "2" ]
    then
     INSTALL=minimal
     CORELDIR_DLX_MIN=175
     CORELDOCDIR_DLX_MIN=66
     i=1
   else
    echo "Invalid selection, Try again."
   fi
  done 


  if [ "$EDITION" = deluxe ]
   then
    if [ "$INSTALL" = full ]
     then
      CORELDIR=$CORELDIR_DLX_FULL
      CORELDOCDIR=$CORELDOCDIR_DLX_FULL
     else
      CORELDIR=$CORELDIR_DLX_MIN
      CORELDOCDIR=$CORELDOCDIR_DLX_MIN
    fi
   else
    if [ "$INSTALL" = full ]
     then
      CORELDIR=$CORELDIR_STD_FULL
      CORELDOCDIR=$CORELDOCDIR_STD_FULL
     else
      CORELDIR=$CORELDIR_DLX_MIN
      CORELDOCDIR=$CORELDOCDIR_DLX_MIN
    fi
  fi


  # Subroutine which checks for available disk space in a directory 

  echo ""
  echo "Checking available disk space"
  echo "" 

 
   echo -n "Checking: $DIR  "
   diskspace=`df $DIR | grep / | awk '{print $4}'`
 
   TOTAL_DISKSPACE=`expr $diskspace / 1000`
 
   if [ "$TOTAL_DISKSPACE" -lt $CORELDIR ]
    then
     echo "INSUFFICIENT DISKSPACE !!! You have $TOTAL_DISKSPACE megabytes in $DIR while the installer needs $CORELDIR megabytes"
     echo "Would you like to continue the installation ?"
     echo "[y/n]"
     read yesno
      if [ "$yesno" = n ] || [ "$yesno" = N ]
       then
        echo "Exiting installation"
        exit 1
       else
        echo "Continuing,  you have been warned !!!"
      fi
    else
     echo "Ok"
   fi

   echo -n "Checking: $docdir  "
   diskspace=`df $docdir | grep / | awk '{print $4}'`
 
   TOTAL_DISKSPACE=`expr $diskspace / 1000`
 
   if [ "$TOTAL_DISKSPACE" -lt $CORELDOCDIR ]
    then
     echo "INSUFFICIENT DISKSPACE !!! You have $TOTAL_DISKSPACE megabytes in $docdir while the installer needs $CORELDOCDIR megabytes"
     echo "Would you like to continue the installation ?"
     echo "[y/n]"
     read yesno
      if [ "$yesno" = n ] || [ "$yesno" = N ]
       then
        echo "Exiting installation"
        exit 1
       else
        echo "Continuing,  you have been warned !!!"
      fi
    else
     echo "Ok"
   fi



  # Ask user to confirm choices
  echo "" 
  echo "Doing $INSTALL install of WordPerfect Office 2000 $EDITION edition for glibc $GLIBC"
  echo "Installing $CORELDIR megabytes into $DIR/corel and $CORELDOCDIR megabytes into /usr/doc/wpohelpfiles"
  echo "Total hard drive space required for installation is `expr $CORELDIR + $CORELDOCDIR` megabytes"
  echo "Press <Enter> to proceed or <Ctrl>-C to cancel install."
  read decide 

  #Checking to see if the system is BSD init based or SYSV 

  if [ -f /etc/rc.d/rc.K -a -f /etc/rc.d/rc.M ]
   then
    echo ""
    echo "BSD init script system found"
    bsdinit=yes
    bsdinit_flags="--nodeps"
    bsdinit_fontastic="--noscripts"
    killall fontfs 2> /dev/null
   else
    echo "SYSV init script system found"
    bsdinit=no
  fi


  # Full install
  if [ "$INSTALL" = "full" ]
   then
   if [ -f /usr/lib/libncurses.so.4 ] || [ -f /lib/libncurses.so.4 ]
      then
       if test -L /usr/lib/libncurses.so.4 || test -L /lib/libncurses.so.4 
        then
         rpm -Uvh $RPMPATH/wine-wpo2000-glibc-${GLIBC}-*.rpm --force --nodeps $bsdinit_flags
        else 
         rpm -Uvh $RPMPATH/wine-wpo2000-glibc-${GLIBC}-*.rpm $bsdinit_flags
        fi
      else

      # If not, check to see if libncurses.so.5 exists and create a symbolic link to libncurses.so.4

      if [ -f /usr/lib/libncurses.so.5 ]
      then
       echo "Creating Symbolic link between /usr/lib/libncurses.so.5 and /usr/lib/libncurses.so.4"
       ln -s /usr/lib/libncurses.so.5 /usr/lib/libncurses.so.4
       rpm -Uvh $RPMPATH/wine-wpo2000-glibc-${GLIBC}-*.rpm --force --nodeps 
        elif [ -f /lib/libncurses.so.5 ]
         then
          echo "Creating Symbolic link between /lib/libncurses.so.5 and /lib/libncurses.so.4"
          ln -s /lib/libncurses.so.5 /lib/libncurses.so.4
          rpm -Uvh $RPMPATH/wine-wpo2000-glibc-${GLIBC}-*.rpm --force --nodeps 
         else
          echo "libncurses was not found. Installing anyway.  You will have problems!!!"
          rpm -Uvh $RPMPATH/wine-wpo2000-glibc-${GLIBC}-*.rpm --force --nodeps              
       fi
   fi

  
  rpm -Uvh $RPMPATH/wpo2000-fonts-core-*.rpm $bsdinit_flags

  if  test -L /etc/init.d 
   then  
    mv /etc/init.d /tmp
    rpm -Uvh $RPMPATH/fonttastic-glibc-${GLIBC}-*.rpm --force $bsdinit_flags $bsdinit_fontastic 2> /dev/null
    mv /tmp/init.d /etc  
   else 
    rpm -Uvh $RPMPATH/fonttastic-glibc-${GLIBC}-*.rpm --force $bsdinit_flags $bsdinit_fontastic 2> /dev/null
  fi

  rpm -Uvh $RPMPATH/libaps-1*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-icons-minimal-*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-help-wordperfect-*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-help-macros-wordperfect-*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-help-quattropro-*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-help-macros-quattropro-*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-help-minimal-*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-show-on-the-go-*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-icons-full-std-*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-help-corelcentral-*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-help-presentations-*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-help-macros-presentations-*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-help-utilities-*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-help-macros-utilities-*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-help-full-std-*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-minimal-2000*.rpm $bsdinit_flags
  rpm -Uvh $RPMPATH/wpo2000-full-2000*.rpm $bsdinit_flags
  if [ "$EDITION" = "deluxe" ]
   then
     rpm -Uvh $RPMPATH/wpo2000-icons-full-dlx-*.rpm $bsdinit_flags
     rpm -Uvh $RPMPATH/wpo2000-help-paradox-*.rpm $bsdinit_flags
     rpm -Uvh $RPMPATH/wpo2000-help-macros-paradox-*.rpm $bsdinit_flags
     rpm -Uvh $RPMPATH/wpo2000-help-full-dlx-*.rpm $bsdinit_flags
     rpm -Uvh $RPMPATH/wpo2000-full-dlx-*.rpm $bsdinit_flags
    else
     rpm -Uvh $RPMPATH/wpo2000-full-std-*.rpm $bsdinit_flags
    fi
  fi

  # Minimal install
  if [ "$INSTALL" = "minimal" ]
   then
  
    if [ -f /usr/lib/libncurses.so.4 ] || [ -f /lib/libncurses.so.4 ]
     then
       if test -L /usr/lib/libncurses.so.4 || test -L /lib/libncurses.so.4 
        then
         rpm -Uvh $RPMPATH/wine-wpo2000-glibc-${GLIBC}-*.rpm --force --nodeps $bsdinit_flags
        else 
         rpm -Uvh $RPMPATH/wine-wpo2000-glibc-${GLIBC}-*.rpm $bsdinit_flags
        fi
       else

        # If not, check to see if libncurses.so.5 exists and create a symbolic link to libncurses.so.4

        if [ -f /usr/lib/libncurses.so.5 ]
         then
          echo "Creating Symbolic link between /usr/lib/libncurses.so.5 and /usr/lib/libncurses.so.4"
          ln -s /usr/lib/libncurses.so.5 /usr/lib/libncurses.so.4
          rpm -Uvh $RPMPATH/wine-wpo2000-glibc-${GLIBC}-*.rpm --force --nodeps 
        elif [ -f /lib/libncurses.so.5 ]
         then
          echo "Creating Symbolic link between /lib/libncurses.so.5 and /lib/libncurses.so.4"
          ln -s /lib/libncurses.so.5 /lib/libncurses.so.4
          rpm -Uvh $RPMPATH/wine-wpo2000-glibc-${GLIBC}-*.rpm --force --nodeps 
         else
          echo "libncurses was not found. Installing anyway.  You will have problems!!!"
          rpm -Uvh $RPMPATH/wine-wpo2000-glibc-${GLIBC}-*.rpm --force --nodeps              
        fi
    fi

   rpm -Uvh $RPMPATH/wpo2000-fonts-core-*.rpm $bsdinit_flags
  
   if test -L /etc/init.d 
    then 
     mv /etc/init.d /tmp
     rpm -Uvh $RPMPATH/fonttastic-glibc-${GLIBC}-*.rpm --force $bsdinit_flags $bsdinit_fontastic 2> /dev/null
     mv /tmp/init.d /etc
    else 
     rpm -Uvh $RPMPATH/fonttastic-glibc-${GLIBC}-*.rpm --force $bsdinit_flags $bsdinit_fontastic 2> /dev/null
   fi

   rpm -Uvh $RPMPATH/libaps-1*.rpm $bsdinit_flags
   rpm -Uvh $RPMPATH/wpo2000-icons-minimal-*.rpm $bsdinit_flags
   rpm -Uvh $RPMPATH/wpo2000-help-wordperfect-*.rpm $bsdinit_flags
   rpm -Uvh $RPMPATH/wpo2000-help-macros-wordperfect-*.rpm $bsdinit_flags
   rpm -Uvh $RPMPATH/wpo2000-help-quattropro-*.rpm $bsdinit_flags
   rpm -Uvh $RPMPATH/wpo2000-help-macros-quattropro-*.rpm $bsdinit_flags
   rpm -Uvh $RPMPATH/wpo2000-help-minimal-*.rpm $bsdinit_flags
   rpm -Uvh $RPMPATH/wpo2000-help-utilities-*.rpm $bsdinit_flags
   rpm -Uvh $RPMPATH/wpo2000-minimal-2000*.rpm $bsdinit_flags
   if [ "$EDITION" = "deluxe" ]
    then
     rpm -Uvh $RPMPATH/wpo2000-minimal-dlx-*.rpm $bsdinit_flags
    else
     rpm -Uvh $RPMPATH/wpo2000-minimal-std-*.rpm $bsdinit_flags
   fi
  fi


  # Check Xfree86 Version - Modify the wpolauncher file if Xfree v4.0 is detected 

  X -version 2> /tmp/.xversion
  xver=`cat /tmp/.xversion | grep XFree86 | grep Version | cut -c 17-17 `
  rm /tmp/.xversion

  if [ $xver -eq "3" ]
   then
    echo "3.x version of XFree86 detected..." >> $logfile
    echo "Skipping wpolauncher.  Setup Complete" >> $logfile
   else
    echo "4.x version of XFree86 detected..." >> $logfile
    echo "Installing updated wpolauncher file.. Backing up the old one to wpolauncher.bak" >> $logfile
   sed -e '141,175s^xset fp+ tcp/$host:7102/all 2>/dev/null^xset fp+ tcp/$host:7102 2>/dev/null ; xset q | grep 'tcp/.*:7102' > /dev/null^g' $DIR/corel/bin/wpolauncher > $DIR/corel/bin/wpolauncher.bak 
    mv $DIR/corel/bin/wpolauncher.bak $DIR/corel/bin/wpolauncher
    chmod 755 $DIR/corel/bin/wpolauncher
  fi 

  echo "Copying application links to the proper directory" >> $logfile
  echo "NOTE: In order for the application icons to show up the window manager must be restarted" >> $logfile
  
  if test -d /opt/kde/share/applnk/
   then 
     cp -rf /usr/X11R6/share/applnk/Applications/WPO2000 /opt/kde/share/applnk/ 2> /dev/null
  fi 

  if test -d /opt/kde2/share/applnk/Applications
   then 
    cp -rf /usr/X11R6/share/applnk/Applications/WPO2000 /opt/kde2/share/applnk/Applications 2> /dev/null
  fi 
  
  if test -d /usr/share/applnk-mdk 
   then 
    cp -rf /usr/X11R6/share/applnk/Applications /usr/share/applnk-mdk 2> /dev/null
  fi 



  echo "Removing any existing setup files" >> $logfile
  rm -rf ~/.wpo* 


  # If the system is BSD init based modify /etc/rc.d/rc.local instead 

  if [ "$bsdinit" = yes ]
   then
    i=`cat /etc/rc.d/rc.local | grep -c Fonttastic` 
     if [ "$i" = 0 ] 
      then
       echo "Adding Fonttastic to /etc/rc.d/rc.local" >> $logfile
       echo "#Wordperfect Office2000 - Fonttastic Fontserver" >> /etc/rc.d/rc.local 
       echo "/usr/X11R6/bin/fontfs &" >> /etc/rc.d/rc.local 
       chmod 755 /etc/rc.d/rc.local 
      else 
       echo "Fonttastic appears to be added to /etc/rc.d/rc.local already..  No need to add this again" >> $logfile
    fi
  fi    

  # If the run-parts executable does not exist modfiy the setupWPO2000 script

  if [ -f /bin/run-parts ] || [ -f /usr/bin/run-parts ]
   then
    echo "run-parts found.." >> $logfile
   else
    echo "run-parts not found.." >> $logfile
    echo "modifying $DIR/corel/bin/setupWPO2000.." >> $logfile
    cat $DIR/corel/bin/setupWPO2000 | head --lines 47 > $DIR/corel/bin/setupWPO2000.bak
    echo 'if [ -d /usr/lib/corel/bin/per-user-setup ]
   then
    for candidate in /usr/lib/corel/bin/per-user-setup/*
   do
        if [ -x $candidate ]
        then
                $candidate
        fi
    done
    fi' >> $DIR/corel/bin/setupWPO2000.bak
    rm $DIR/corel/bin/setupWPO2000
    mv $DIR/corel/bin/setupWPO2000.bak $DIR/corel/bin/setupWPO2000
    chmod 755 $DIR/corel/bin/setupWPO2000
  fi

  # Ensure that LC_ALL is set to EN 

  lc_all=`strings /lib/libc.so.6 | grep "GNU C Library" | awk '{print $7}' | cut -c 1-5`
  
  
  if [ "$lc_all" = 2.1.3 ]
   then 
    echo 'Your running Glibc v2.1.3 - Setting LC_ALL=en' >> $logfile 
    echo 'Adding LC_ALL=en to /etc/profile' >> $logfile
    echo 'export LC_ALL=en' >> /etc/profile 
    echo 'YOU MUST REBOOT YOUR COMPUTER BEFORE STARTING WORDPERFECT'
   fi 

  # Libsafe causes wine to seg fault... 

   libsafe=`set | grep -c libsafe`

   if [ "$libsafe" = 1 ]
    then
     echo "WARNING: It appears that you have libsafe installed on your system."
     echo "Please ensure that it is disabled while running wordperfect."
     echo "Press any key to continue ...."
     read anykey
   fi

  # Fix a few issues with Ole32 

  echo "Fixing ole32 issues in wine.conf" >> $logfile
  sed -e '99,99s/^$/ole32, oleaut32  = builtin/g' /usr/lib/corel/etc/user.wpo2000/wine.conf > /usr/lib/corel/etc/user.wpo2000/wine.conf.bak 
  mv /usr/lib/corel/etc/user.wpo2000/wine.conf.bak /usr/lib/corel/etc/user.wpo2000/wine.conf
  

  # And finally - to unmount the CDROM from /tmp/mnt ( only if it was not detected as a mounted FS )  
 

  # Warn users that have Mandrake v8.x to install a libc compatibility module 

  if test -d /usr/share/applnk-mdk 
   then 
    echo "It appears that your using Linux Mandrake v8.x or greater" 
    echo "In order for Office2000 to work properly a libc compatibilty module needs to be installed" 
    echo "This module can be downloaded from:" 
    echo "http://www.rpmfind.net/linux/Mandrake/8.0/i586/Mandrake/RPMS/compat-glibc-7.2-2.1.3.3mdk.i586.rpm" 
    echo " "
    echo "Alternatively, the module can be found on the 2nd mandrake cd in the following location:" 
    echo "/mnt/cdrom/Mandrake/RPMS2/compat-glibc-7.2-2.1.3.3mdk.i586.rpm"  
    echo " " 
    echo "Press any key to continue...   " 
    read anykey 
  fi 


  
  # Fontastic has to be killed first otherwise it wont eject 

  killall fontfs 2> /dev/null
  umount -f /tmp/mnt 2> /dev/null
  rmdir /tmp/mnt 2> /dev/null

  echo "NOTE: You must log out and back into your system in order to see" 
  echo "menu items.  Hit Ctrl-Alt-Backspace once the installer is finished." 
  echo "Starting Fontfs - It may not startup the first time around" 

  fontfs & 

  echo "Done!" 
  echo "Hit <ENTER> once.."  

else


# uninstaller for WordPerfect Office 2000 for Linucx
# Copyright 1999-2000 Corel Corporation

# Author: M Dickson.

# Thanks to:
#	J Aston for the disk space detection
#	A denHaan for feedback and help with dpkg and apt-get
#	D Meisenheimer for showing me how cool unattended ftp can be
#	J Tranter for an easier way of determining the c library
#	C Gorman for a more stable way of determining the c library 
#	C James for giving me a kick in the pants
#	K Wong and his team for finding all the "undesired features"
#-----------------------------------------------------------------------
# Caveats
#	Eterm seems not to like the way I clear lines
#	the grep on the filename for rpm is a bit globbish
#------------------------------------------------------------------------

#------------------------------------------------------------------------
#Changelog

# 0.1
# initial release, based on remove-graphics9-0.6

#------------------------------------------------------------------------
currentVersion="0.1"
#------------------------------------------------------------------------
#------G L O B A L   V A R I A B L E S-----------------------------------
	# A listing of all the packages common to STD and DLX.
commonPackageList=" libwine-wpo2000 \
	wine-doc-wpo2000 wine-tools wine-wpo2000 \
	wpo2000-full wpo2000-help-corelcentral wpo2000-help-full-std \
	wpo2000-help-macros-presentations wpo2000-help-macros-quattropro \
	wpo2000-help-macros-utilities	wpo2000-help-macros-wordperfect \
	wpo2000-help-minimal wpo2000-help-presentations wpo2000-help-quattropro \
	wpo2000-help-sdk wpo2000-help-utilities wpo2000-help-wordperfect \
	wpo2000-icons-full-std	wpo2000-icons-minimal wpo2000-minimal \
	wpo2000-sdk wpo2000-show-on-the-go "

	# A listing of all the packages in STD only
stdPackageList="wpo2000-full-std wpo2000-minimal-std "

	# A listing of all the packages in DLX only
dlxPackageRelease="wpo2000-full-dlx wpo2000-help-full-dlx \
	wpo2000-help-macros-paradox wpo2000-help-paradox \
	wpo2000-icons-full-dlx wpo2000-minimal-dlx "

	# the amalgamated list of all packages
fullPkgRemoveList="$commonPackageList $stdPackageList $dlxPackageRelease"

	# How many packages are listed above?
	# 22common + 2std + 6dlx
numPkgs=30

	# these are the packages that have a glibc version attached
libcDepList="fonttastic libwine-wpo2000 wine-tools wine-wpo2000"

	# rpm or dpkg?
type=none;

	# this gets set to dontRemoveIfGraphics9List if draw9 is not installed.
alsoRemove=" ";

	# these files should not be removed if WPO2000 is also on the system.
dontRemoveIfGraphics9List=" libaps libaps-dev fonttastic wpo2000-fonts-core "

	# How many files are listed above?
dontRemoveNum=4;

	# This flag indicates whether a whack of directories should be removed.
	# Set to 0 (no) by defualt, only reset to 1 if graphics9 is not found
cleanSystem=0;

	# These two turn bold text on and off
bon="\33[1m"
boff="\33[0m"

	# Error Strings
	# Each error condition is given a string here.  Then, when
	#  error() is used, the correct string is printed.
error2="You must be root to install this software."
error3="No package manager found."
error4="libc6 is not installed."
error8="This is not a Linux system!"
error13="Only libc6 versions 2.0 and 2.1 are supported.  Neither are installed."
error99="Unknown error has occurred.  Please contact technical support."

	# realias echo to handle special characters, which it doesn't
	#  do by default
ECHO="echo -e"

currentUID=""
#---------------------------------------------------------------------------
#-----F U N C T I O N S-----------------------------------------------------
# clearLine
clearLine ()
	{
	clearCount=80;
	if [ ! -z "$1" ] ; then
		clearCount=$1;
	fi
	clearStatus=`expr $clearCount \> 0`
	while [ $clearCount -gt 0 ] ; do
		echo -en "\b \b"
		clearCount=`expr $clearCount - 1`
	done
	}


#---------------------------------------------------------------------------
#generic error handler. Takes 1 required and 1 optional arguement
# First arguement is a number, corresponding to an error message as listed
#  above.
# Second arguement is one of:
#	blank : nothing happens
#	usage : usage message gets printed out
#	text  : the supplied text gets printed out  
error ()
	{
	t=error$1
	eval $ECHO "\${bon}*****\\\nERROR\${boff}\\\n\$$t"
	if [ ! -z  "$2" ] ; then
		if [ $2 = "usage" ] ; then 
			$ECHO $usage ; 
		fi
	else
		eval $ECHO "$2";
	fi
	exit 1
	} 

#-----------------------------------------------------------------------------
# Performs all the required system checks:
#	user is root
#	system is Linux
#	libc6 is installed
#	a package manager is installed
# If any of these fail, the whole script fails.
systemChecks ()
	{
	$ECHO "${bon}Beginning System Checks:${boff}"
	#-----Check for Linux system---------
	$ECHO -n "  Checking for a Linux system..."
	if [ ! `uname -s | egrep -e "inux"` ] ; then
		error 8 '`uname -s` was found';
	fi
	$ECHO " OK Found one.  Whew!! ;)"

	#-----Check for Graphics9 ---------------
	$ECHO -n "  Checking for Corel Graphics9...";
	if [ -f /usr/lib/corel/Graphics9/bin/gfxlauncher ] || [ -d /usr/lib/corel/Graphics9 ] ; then
		$ECHO "found. $dontRemoveIfGraphics9List will not be removed."
	else
		alsoRemove=$dontRemoveIfGraphics9List;
		numPkgs=`expr $numPkgs + $dontRemoveNum` ;
		cleanSystem=1;
		$ECHO "not found."
	fi
	
	#-----Check for Package Manager-------
	$ECHO -n "  Checking for package manager...";
	
	dpkg -h > /dev/null 2>/dev/null;
	if [ $? -eq 0 ] ; then
		type=dpkg;
	else
		rpm -v > /dev/null 2>/dev/null;
		if [ $? -eq 0 ] ; then
			type=rpm;
		fi
	fi
	if [ $type = "none" ] ; then
		error 3
	fi

	$ECHO " OK (found $type at `which $type`)";

	#-----Check for correct libc ------
	$ECHO -n "  Checking for proper C library..."
	if [ -f /lib/libc.so.6 ] ; then
		versionNumber=`strings /lib/libc.so.6 | grep "GNU C Library" | awk '{print $7}' | cut -c 1-3`;
		if [ $versionNumber != "2.0" -a $versionNumber != "2.1" ] ; then 
			versionNumber="2.1";
			libcVer="glibc-$versionNumber";
		else 
			libcVer="glibc-$versionNumber";
			$ECHO " OK (found $libcVer)";
		fi
	else
		error 4
	fi


	$ECHO "  System checks passed."
	}

#-----------------------------------------------------------------------------
# Remove the packages from your system.  Very brute force and unoptomized.
# Also removes the directories /usr/lib/corel, /var/lib/corel and 
# /usr/doc/wpohelpfiles
removePackages ()
	{
	$ECHO "${bon}Removing WPO2000, $numPkgs packages to check.${boff}"
	$ECHO  "  Removal Progress:"
	num=$numPkgs;
	if [ $type = "rpm" ] ; then
		for pkg in $fullPkgRemoveList $alsoRemove; do
#			$ECHO -n "."
			for test in $libcDepList ; do
				if [ $test = $pkg ] ; then
#					echo -n "$pkg ->";
					pkg="$pkg-$libcVer";
#					echo "$pkg"
				fi
			done;
#			clearLine
			$ECHO -n "\n    Checking for #${num}: ${bon}$pkg${boff}..."
 			$type -qa | grep "^$pkg"  > /dev/null 2>&1
			if [ "$?" -eq 0 ] ; then
				$ECHO -n "Found. Removing it."
				$type $REMOVE $pkg > /dev/null 2>&1
			fi
			num=`expr $num - 1`;
#		$ECHO "";
		done
	else
		for pkg in  $fullPkgRemoveList $alsoRemove; do
			for test in $libcDepList ; do
				if [ $test = $pkg ] ; then
#					echo -n "$pkg ->";
					pkg="$pkg-$libcVer";
#					echo "$pkg"
				fi
			done;
			$ECHO -n "."
#			$ECHO "$pkg"
#			clearLine
			$ECHO -n "\n    Removing #${num}: ${bon}$pkg${boff} if installed."
			$type $REMOVE $pkg $pkg > /dev/null 2>&1
			num=`expr $num - 1`;
		done
	fi
#	clearLine
	$ECHO "\n  Package removal complete.";
	$ECHO -n "  Cleaning up leftover directories..."	
	rm -rf /usr/lib/corel/wine
	if [ $cleanSystem -eq 1 ] ; then
		rm -rf /usr/lib/corel
		rm -rf /var/lib/corel
		rm -rf /usr/doc/graphics9*
		rm -rf /etc/fonttastic
		$ECHO "done."
		sleep 1;
	fi
#	clearLine
	$ECHO "\nWordPerfect Office 2000 for Linux  Application removal complete."
	}


#
#-----------------------------------------------------------------------------
# get the current username based on the UID
checkUID () 
	{
	grep "^[^:]*:[^:]*:$1" /etc/passwd | cut -f1 -d:
	}

#==============================================================================
#=====S C R I P T   S T A R T==================================================
clear
$ECHO "remove-wpo2000 v$currentVersion\n"
$ECHO "This script will remove all WordPerfect Office 2000 for Linux packages from this system."
	#-----Check for root---------
currentUID=$(checkUID $(id -u))

if [ $currentUID != "root" ] ; then
	$ECHO "\n  ****************************************************"
	$ECHO "  * You will require the root password to complete the *"
	$ECHO "  * removal of the WordPerfect Office 2000 for Linux.  *"
	$ECHO "  * If you don't know the root password, hit ctrl-c    *"
	$ECHO "  * now to cancel this script.                         *"
	$ECHO "  ****************************************************\n"
	$ECHO -n "\n  Please enter the root "
	while ! su root -c "$0" ; do
		clearLine
		$ECHO -n "Incorrect password.  Try again? (y/n) >"
		read response;
		if [ $response != "y" ] && [ $response != "Y" ]  ; then
			clearLine
			$ECHO "Please try again when you know the root password."
			exit;
		fi
		clearLine
		$ECHO -n "\n  Please enter the root "
		done
	exit;
fi

$ECHO "hit <ctrl-c> now to abort."
for i in 5 4 3 2 1 ; do
	$ECHO -n "$i "
	sleep 1
done;
clearLine 10
$ECHO "Onward..."

	#perform all system checks
systemChecks;

if [ $type = "rpm" ] ; then
	REMOVE="-e --allmatches --nodeps";
elif [ $type = "dpkg" ] ; then
	REMOVE="--purge --ignore-depends"
fi

removePackages;

$ECHO "\n\n${bon}All Done!${boff}\n"

exit;

fi 


