#!/bin/bash

# wp81in.sh - version 2.3 (2026)
# A script to install WordPerfect 8.1 for Linux on debian-based systems.
# \(c\) Peter Stone, 2026
# peter@xwp8users.com

bold=$(tput bold)
normal=$(tput sgr0)

echo " "
echo This script is designed to install ${bold}WordPerfect 8.1 for Linux${normal} on a 
echo debian-based distro current in 2026 or later.
echo " " 
echo It has been tested most recently on Mint 22.3, Ubuntu 26.04, and MX 25.1.
echo " " 
echo Before running this script, you should have taken the following preliminary steps:
echo " "
echo 1. Create the following directory, which will serve as the working directory
echo for the script:"  "\"${bold}~/Downloads/wp-inst${normal}\"
echo " "
echo 2. Copy this script file to that directory.
echo
echo 3. Copy your ${bold}wp8-full${normal} and ${bold}fonts-\[x\]${normal} deb files,
echo or your ${bold}wpx-free${normal} deb file,
echo from your Corel Linux CD to that directory.
echo " "
echo " "
echo The script should be run as a normal user \(not as root\). It will call for
echo your sudo password on the first occasion that \"sudo\" is called.
echo " "
echo You must be a user enabled to use \"sudo\" in order to run the script.
echo " "
echo After changing to the working directory, the script can be executed 
echo with the command:"  "\"${bold}sh ./wp81in.sh${normal}\"

# *****************************************************************************
# Carrying out preliminary steps (mainly tests and downloads)

echo " "
# Ensuring that the working directory exists.

test -e ~/Downloads/wp-inst
if ! [ $? = 0 ] 
then 
    mkdir ~/Downloads/wp-inst
fi

cd ~/Downloads/wp-inst

echo " "
# Testing for the WordPerfect 8.1 deb.

test -e ./wp-full_8.1-12_i386.deb 
if [ $? = 0 ] 
then 
     test -e ./fonts-16_1.0-5.deb
     if ! [ $? = 0 ] 
     then 
        echo " "
        echo "Command failed. fonts-16 is not available. Exiting script."
        exit
     fi

     cp ./wp-full_8.1-12_i386.deb ./wp81.deb

else
     test -e ./wpx-free_8.0-78_i386.deb
     if [ $? = 0 ] 
     then 
         cp ./wpx-free_8.0-78_i386.deb ./wp81.deb
     else
         echo " "
         echo "Command failed. No version of WordPerfect 8.1 is available. Exiting script."
        exit
    fi
fi

echo " "


# *****************************************************************************
# Now ensuring that i386 architecture is available in debian-based distros.

sudo dpkg --add-architecture i386
sudo apt-get update

# *****************************************************************************

# Now installing wget deb.

sudo apt-get -y install wget

if ! [ $? = 0 ] 
then 
   echo " "
   echo "Command failed. Unable to install wget. Exiting script."
   exit
fi

# Now installing libxaw7:i386.

echo " "
sudo apt-get -y install libxaw7:i386

if ! [ $? = 0 ] 
then 
    echo "Command failed. Unable to install libxaw7. Exiting script."
    exit
fi

# Removing packages which conflict with wp-utils.
# In version 3.7 onwards, wp-utils itself deals with libc5 requirements.

sudo dpkg -r wppmwrap wppmwrap80 wppmwrap81d wppmwrap-80 wppmwrap-81d
sudo dpkg -r xlib6g type1inst type1-64add prewp64 prewp68 wputils80
sudo dpkg -r ldso libc5

echo " "
# Downloading the wp-utils deb, if necessary.

test -e ./wp-utils_5.1_i386.deb
if ! [ $? = 0 ] 
then 
    wget http://www.xwp8users.com/packages/wp-utils_5.1_i386.deb
fi
  
if ! [ $? = 0 ] 
then 
    echo " "
    echo "Command failed. wp-utils is not available. Exiting script."
    exit
fi

echo " "
# Now installing wp-utils.

sudo dpkg -i wp-utils_5.1_i386.deb

if ! [ $? = 0 ] 
then 
    echo "Command failed. Unable to install wp-utils. Exiting script."
    exit
fi

sudo chmod +x /usr/bin/installpkg15
sudo chmod +x /usr/bin/type1inst
sudo chmod +x /usr/bin/xwppmgr
sudo chmod +x /usr/bin/xwppmgr81
sudo chmod +x /usr/bin/xwppmgr80
sudo chmod +x /lib/ld-2.27.so

echo " "
# Now installing libc6:i386.

sudo apt-get -y install libc6:i386

if ! [ $? = 0 ] 
then 
    echo " "
    echo "Command failed. Unable to install libc6:i386. Exiting script."
    exit
fi

echo " "
# Now running ldconfig.

sudo ldconfig -c old

if ! [ $? = 0 ] 
then 
    echo " "
    echo "Command failed. Unable to run ldconfig. Exiting script."
    exit
fi

# *******************************************************************
# Now installing some official support packages.

echo " "
# Now installing binutils.

sudo apt-get -y install binutils

if ! [ $? = 0 ] 
then 
    echo " "
    echo "Command failed. Unable to install binutils. Exiting script."
    exit
fi

echo " "
# Now installing groff.

sudo apt-get -y install groff

if ! [ $? = 0 ] 
then 
    echo " "
    echo "Command failed. Unable to install groff. Exiting script."
    exit
fi

echo " "
# Now installing perl.

sudo apt-get -y install perl

if ! [ $? = 0 ] 
then 
    echo " "
    echo "Command failed. Unable to install perl. Exiting script."
    exit
fi

echo " "
# Now installing xbase-clients.

sudo apt-get -y install xbase-clients

if ! [ $? = 0 ] 
then 
    echo " "
    echo "Command failed. Unable to install xbase-clients. Exiting script."
    exit
fi

cd ~/Downloads/wp-inst

echo " "

# **********************************************************************
# Now proceeding with the main installation.

# Now installing fonts for wp-full.

cd ~/Downloads/wp-inst

test -e ./wp-full_8.1-12_i386.deb
if [ $? = 0 ]
then
    test -e ./fonts-16_1.0-5.deb
    if [ $? = 0 ]
    then
        ar -vx ./fonts-16_1.0-5.deb
        mv ./data.tar.gz ./fonts-16-1.0.tgz

        if ! [ $? = 0 ]
        then
            echo " "
            echo "Command failed. fonts-16 cannot be converted. Exiting script."
            exit
        fi

        sudo installpkg15 ./fonts-16-1.0.tgz

        if ! [ $? = 0 ]
        then
            echo " "
            echo "Command failed. Unable to install fonts-16. Exiting script."
            exit
        fi
    fi
fi

test -e ./wp-full_8.1-12_i386.deb
if [ $? = 0 ]
then
    echo " "
    test -e ./fonts-69_1.0-4.deb
    if [ $? = 0 ]
    then
        ar -vx ./fonts-69_1.0-4.deb
        mv ./data.tar.gz ./fonts-69-1.0.tgz

        if [ $? = 0 ]
        then
            sudo installpkg15 ./fonts-69-1.0.tgz
        fi
    fi
fi

test -e ./wp-full_8.1-12_i386.deb
if [ $? = 0 ]
then
    test -e fonts-115_1.0-4.deb
    if [ $? = 0 ]
    then
        ar -vx ./fonts-115_1.0-4.deb
        mv ./data.tar.gz ./fonts-115-1.0.tgz

        if [ $? = 0 ]
        then
            sudo installpkg15 ./fonts-115-1.0.tgz
        fi
    fi
fi

# *****************************************************************************
# Now converting and then installing the core WordPerfect package.

echo " "
cd ~/Downloads/wp-inst

test -e ./wp81.deb
if [ $? = 0 ]
then
    ar -vx ./wp81.deb
    mv ./data.tar.gz ./wp81.tgz

    if ! [ $? = 0 ]
    then
        echo " "
        echo "Command failed. Unable to convert the core WordPerfect package."
        echo "Exiting script."
        exit
    fi

    sudo installpkg15 ./wp81.tgz

    if ! [ $? = 0 ]
    then
       echo " "
       echo "Command failed. Unable to install the core WordPerfect files."
       echo "Exiting script."
       exit
    fi
fi

# Now co-ordinating with 8.0, if necessary.

sudo cp /usr/bin/xwp81 /usr/bin/xwp

cd ~/Downloads/wp-inst

echo " "

# ***********************************************************************
# Now setting up the fonts.

test -e /usr/lib/wp8/shbin10/xwpfi
if [ $? = 0 ] 
then 
    cd /usr/X11R6/lib/X11/fonts/Type1

    sudo type1inst

    if ! [ $? = 0 ] 
    then 
        echo " "
        echo "Command failed. Unable to run type1inst. Exiting script."
        exit
    fi

    sudo mkfontdir

    if ! [ $? = 0 ] 
    then 
        echo " "
        echo "Command failed. Unable to run mkfontdir. Exiting script."
        exit
    fi

    sudo /usr/lib/wp8/shbin10/wpfi

    if ! [ $? = 0 ] 
    then 
        echo " "
        echo "Command failed. Unable to run wpfi. Exiting script."
        exit
    fi

    sudo cp -RT /usr/X11R6/lib/X11/fonts/Type1 /usr/share/fonts/type1

    if ! [ $? = 0 ] 
    then 
        echo " "
        echo "Command failed. Unable to copy fonts to the whole system." 
        echo "Exiting script."
        exit
    fi  
fi

cd ~/Downloads/wp-inst

echo " "

sudo apt-get -y install printer-driver-cups-pdf

# Now creating a link to /etc/printcap, if necessary.

test -e /etc/printcap
if ! [ $? = 0 ] 
then 
    sudo ln -s /run/cups/printcap /etc/printcap
fi

# *****************************************************************************
# Now setting up the WP Print Manager executable.

echo " "
cd /usr/lib/wp8/shbin10

test -e ./xwppmgr.bin
if ! [ $? = 0 ] 
then 
    sudo cp ./xwppmgr ./xwppmgr.bin
fi

test -e ./xwppmgr.bin
if ! [ $? = 0 ] 
then 
    echo " "
    echo "Command failed. Unable to create xwppmgr.bin."
    echo "Exiting script."
    exit
fi

sudo ldconfig -c old

# **********************************************************************
# Now providing the final messages

echo " "
echo \(1\) ${bold}WordPerfect 8.1${normal} has been successfully installed, along with an updated 
echo version of the WP Print Manager.
echo " "
echo \(2\) ${bold}Next${normal}, you should run WordPerfect 8.1 ${bold}once${normal} as a superuser,
echo by giving, in a terminal window, the command:
echo "   "\"${bold}sudo xwp -adm${normal}\"
echo " "
echo Then, within WordPerfect, you should select Preferences, and then File Locking,
echo and then ensure that the option to \“Disable Unix File locking\" is switched on.
echo
echo This should ensure that error messages referring to \"too many processes\", or
echo to locked files, are not received.
echo " "
echo \(3\) ${bold}Thereafter${normal}, you should run WordPerfect 8.1 as a normal user, by giving,
echo in a terminal window, the command:
echo "   "\"${bold}xwp${normal}\"
echo " "
echo Alternatively, you may run WordPerfect 8.1 from an entry thereto in your Linux menu.
echo " "
echo \(4\) To run the WP Print Manager, you can give, in a terminal window, the command:
echo "   "\"${bold}sudo xwppmgr${normal}\"
echo " "
echo " "
echo Enjoy!
echo " "
exit
