#!/bin/bash


function BackupRenameFile() {
  fname="$1"
  let bkno=1
  if [ ! -e "$fname" ]; then
     return 0
  fi
  while [ -e "${fname}~${bkno}" ]
  do
     let bkno=`expr $bkno + 1`
  done
  mv "$fname" "${fname}~${bkno}"
}


# Make links for shared libraries
ln -sf $SETUP_INSTALLPATH/bin/libborqt-6.9.0-qt2.3.so $SETUP_INSTALLPATH/bin/libborqt-6.9-qt2.3.so


# Hack for Debian 2.2
if [ ! -e /usr/lib/libstdc++-libc6.1-1.so.2 ]; then
    libcpp=`ls /usr/lib/libstdc++-3*.so | tail -1`
    if [ -n "$libcpp" ]; then
	ln -sf "$libcpp" $SETUP_INSTALLPATH/bin/libstdc++-libc6.1-1.so.2
    fi
fi


### Make sure the font installation scripts and tools have execute permission
chmod 755 $SETUP_INSTALLPATH/fonts/install_fonts.sh
chmod 755 $SETUP_INSTALLPATH/fonts/add_aliases.sh
chmod 755 $SETUP_INSTALLPATH/fonts/add_scales.sh
chmod 755 $SETUP_INSTALLPATH/fonts/addfonts


### Install fonts into X-windows
$SETUP_INSTALLPATH/fonts/install_fonts.sh | tee $SETUP_INSTALLPATH/fonts/install_log
if [ -r $SETUP_INSTALLPATH/fonts/install_log ]; then
	cat $SETUP_INSTALLPATH/fonts/install_log >> ~/aplx_install_log
else
	echo "Error installing fonts. Log missing" >> ~/aplx_install_log
fi


### Create 'startaplx' script
cat <<EOF > $SETUP_INSTALLPATH/bin/startaplx
#!/bin/bash

#
# Startup script for APLX for Linux (GUI version)
#
# (Generated during install)

# Directory containing library (.so) files
APLX_LD_PATH=$SETUP_INSTALLPATH/bin

# X-windows font directories for APLX fonts
# Read font install path used, if known
APLX_FONT_PATH=""
if [ -z "\$APLX_FONT_PATH" -a -r $SETUP_INSTALLPATH/fonts/installed_path ]; then
	APLX_FONT_PATH=\$(cat $SETUP_INSTALLPATH/fonts/installed_path)
fi
if [ -z "\$APLX_FONT_PATH" ]; then
	# Bad luck. Use default and hope for the best
	APLX_FONT_PATH=/usr/lib/X11/fonts/Type1
fi

# Add APLX directory to LD_LIBRARY_PATH
if [ -n "\$LD_LIBRARY_PATH" ]; then
	# Already have an LD_LIBRARY_PATH. Prepend ours
	export LD_LIBRARY_PATH="\$APLX_LD_PATH:\$LD_LIBRARY_PATH"
else
	# Don't yet have an LD_LIBRARY_PATH. Create it now
	export LD_LIBRARY_PATH="\$APLX_LD_PATH"
fi

# Put the APLX printing font in the X server font path
if [ -n "\$APLX_FONT_PATH" ]; then
	xset fp+ \$APLX_FONT_PATH
	xset fp rehash
fi

# Strip out utf-8 part of 'en_US.utf-8' and ensure right character set
LANG=$(echo $LANG | sed 's/\.utf-8\|\.UTF-8//'); export LANG
LC_CTYPE=iso_8859_1;export LC_CTYPE

# Run APLX
$SETUP_INSTALLPATH/bin/aplxlinux $*

EOF

### Create 'disp_apl_lang_pdf' script to display APLX Language Manual
cat <<EOF > $SETUP_INSTALLPATH/bin/disp_apl_lang_pdf
#!/bin/bash

#
# Script to display APLX Language Manual using Acrobat, KGhostView or xpdf
#
# (Generated during install)

type -t acroread > /dev/null
if [ "\$?" -eq 0 ]; then
    acroread "$SETUP_INSTALLPATH/manuals/APLX Language Manual.pdf"
else
    type -t kghostview > /dev/null
    if [ "\$?" -eq 0 ]; then
        kghostview "$SETUP_INSTALLPATH/manuals/APLX Language Manual.pdf"
    else
        type -t xpdf > /dev/null
        if [ "\$?" -eq 0 ]; then
            xpdf "$SETUP_INSTALLPATH/manuals/APLX Language Manual.pdf"
        fi
    fi
fi

EOF

### Create 'disp_apl_gui_pdf' script to display APLX GUI Programming Manual
cat <<EOF > $SETUP_INSTALLPATH/bin/disp_apl_gui_pdf
#!/bin/bash

#
# Script to display APLX Language Manual using Acrobat, KGhostView or xpdf
#
# (Generated during install)

type -t acroread > /dev/null
if [ "\$?" -eq 0 ]; then
    acroread "$SETUP_INSTALLPATH/manuals/APLX System Classes.pdf"
else
    type -t kghostview > /dev/null
    if [ "\$?" -eq 0 ]; then
        kghostview "$SETUP_INSTALLPATH/manuals/APLX System Classes.pdf"
    else
        type -t xpdf > /dev/null
        if [ "\$?" -eq 0 ]; then
            xpdf "$SETUP_INSTALLPATH/manuals/APLX System Classes.pdf"
        fi
    fi
fi

EOF

### Create 'disp_apl_new_pdf' script to display What's New in v5
cat <<EOF > $SETUP_INSTALLPATH/bin/disp_apl_new_pdf
#!/bin/bash

#
# Script to display APLX Language Manual using Acrobat, KGhostView or xpdf
#
# (Generated during install)

type -t acroread > /dev/null
if [ "\$?" -eq 0 ]; then
    acroread "$SETUP_INSTALLPATH/manuals/APLXv5_Update_Reference.pdf"
else
    type -t kghostview > /dev/null
    if [ "\$?" -eq 0 ]; then
        kghostview "$SETUP_INSTALLPATH/manuals/APLXv5_Update_Reference.pdf"
    else
        type -t xpdf > /dev/null
        if [ "\$?" -eq 0 ]; then
            xpdf "$SETUP_INSTALLPATH/manuals/APLXv5_Update_Reference.pdf"
        fi
    fi
fi

EOF


### Verify/create KDE applnk directories
if [ -d ~/.kde2 ]; then
   if [ ! -d ~/.kde2/share/applnk-mdk ]; then
      if [ ! -d ~/.kde2/share ]; then
         mkdir ~/.kde2/share
      fi
      if [ ! -d ~/.kde2/share/applnk ]; then
         mkdir ~/.kde2/share/applnk
      fi
   fi
   if [ -e /etc/SuSE-release ]; then
      if [ -d ~/.kde2/share/applnk ]; then
         if [ ! -d ~/.kde2/share/applnk/SuSE ]; then
            mkdir ~/.kde2/share/applnk/SuSE
         fi
      fi
   fi
else
   if [ -d ~/.kde ]; then
      userid=`id -u`
      if [ -e /etc/mandrake-release -o $userid -ne 0 ]; then
         grep "release 8\." /etc/mandrake-release >/dev/null 2>/dev/null
         if [ $? -eq 0 ]; then
            if [ ! -d ~/.kde/share ]; then
               mkdir ~/.kde/share
            fi
            if [ ! -d ~/.kde/share/applnk-mdk ]; then
               mkdir ~/.kde/share/applnk-mdk
            fi
         else
            if [ ! -d ~/.kde/share ]; then
               mkdir ~/.kde/share
            fi
            if [ ! -d ~/.kde/share/applnk ]; then
               mkdir ~/.kde/share/applnk
            fi
         fi
      else
         if [ ! -d ~/.kde/share ]; then
            mkdir ~/.kde/share
         fi
         if [ ! -d ~/.kde/share/applnk ]; then
            mkdir ~/.kde/share/applnk
         fi
      fi
   fi
fi

