#!/bin/bash

# wp81in-manjaro.sh - version 2.0
# A script to install WordPerfect 8.1 for Linux on arch-based distros such as Manjaro and EndeavourOS.
# \(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}
echo on versions of Manjaro and EndeavourOS current in 2026. It may work on
echo other arch-based distros.
echo " "
echo It has been tested most recently on Manjaro 26.0.3 Gnome and EndeavourOS Titan KDE.
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 for 
echo 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, 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-manjaro.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

# *****************************************************************************
# Now installing wget.

test -e /usr/bin/wget
if ! [ $? = 0 ] 
then 
    sudo pacman -S wget
fi

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

# Now installing binutils, mkfontscale, and if necessary

test -e /usr/bin/ar
if ! [ $? = 0 ] 
then 
    sudo pacman -S binutils
fi    
    
if ! [ $? = 0 ] 
then 
   echo " "
   echo "Command failed. Unable to install binutils. Exiting script."
   exit
fi

test -e /usr/bin/mkfontdir
if ! [ $? = 0 ]
then
    sudo pacman -S xorg-mkfontscale
fi

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

sudo pacman -S libxaw

echo " "
# Now downloading (if necessary) and installing installpkg15

test -e ./installpkg15
if ! [ $? = 0 ]
then
    wget http://www.xwp8users.com/packages/installpkg15
fi

if ! [ $? = 0 ]
then
    echo " "
    echo "Command failed. installpkg15 is not availiable. Exiting script."
    exit
fi

echo " "

# sudo cp ./installpkg15 /usr/bin/installpkg15

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

sudo chmod +x ./installpkg15

echo " "
# Now downloading wp-utils-arch, if necessary.

test -e ./wp-utils-arch-4.5.tgz
if ! [ $? = 0 ] 
then 
    wget http://www.xwp8users.com/packages/wp-utils-arch-4.5.tgz
fi

if ! [ $? = 0 ] 
then 
    echo " "
    echo "Command failed. wp-utils-arch is not availiable. Exiting script."
    exit
fi

echo " "

# Now installing wp-utils-arch.

cd ~/Downloads/wp-inst

sudo ./installpkg15 ./wp-utils-arch-4.5.tgz

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

cd ~/Downloads/wp-inst

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


# *****************************************************************************
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 groff.

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

echo " "


####
# # Now installing perl.

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


cd ~/Downloads/wp-inst

echo " "

# ****************************************************************************
# Now proceeding with the main installation.
# Now installing fonts for wp-full.

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    
    
echo " "

cd ~/Downloads/wp-inst

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

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 WordPerfect 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

# Now copying fonts to whole system.

    test -e /usr/share/fonts/type1
    if [ $? = 0 ]
    then
        sudo cp -RT /usr/X11R6/lib/X11/fonts/Type1 /usr/share/fonts/type1
    else
        sudo cp -RT /usr/X11R6/lib/X11/fonts/Type1 /usr/share/fonts/Type1
    fi
fi

cd ~/Downloads/wp-inst

echo " "

# ***************************************************************************
# Now dealing with printing.

sudo pacman -S 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

cd ~/Downloads/wp-inst

echo " "

# *********************************************************************************
# Now doing some extra setup necessary for Manjaro.

sudo chmod -R 777 /usr/lib/wp8/shlib10
    
sudo cp /usr/bin/xwppmgr81-mj /usr/bin/xwppmgr
sudo cp /usr/bin/xwppmgr81-mj /usr/bin/xwppmgr81

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

echo " "
echo \(1\) ${bold}WordPerfect 8.1${normal} has been successfully installed,
echo along with an updated 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 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 be able to run WordPerfect 8.1 from an entry thereto
echo 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
