#!/bin/bash

#*****************************************************************************
# 
#  pyunorc-update64 - Utility to update pythonloader.unorc on x86_64
#
#  It extends PYTHONPATH and PYTHONHOME with various paths, so pyuno is able
#  to find 32-bit .so files even on 64-bit system and on the contrary
#  the 32-bit libraries are able to find .py and .pic files from the 64bit
#  package.
# 
#  Initial version by: Petr Mladek <pmladek@suse.cz>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2, as
#  published by the Free Software Foundation.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# 
#*****************************************************************************

# do the job just on x86_64
test `uname -i` = "x86_64" || exit 0;

PYUNORCFILE=@OOINSTBASE@/program/pythonloader.unorc

# try to detect the right path from the compat link
if test -L /usr/lib64/python ; then
    PYTHONHOME64=`readlink /usr/lib64/python`
    if test "${PYTHONHOME64#/*}" = "$PYTHONHOME64" ; then
	# relative path
	PYTHONHOME64="/usr/lib64/$PYTHONHOME64"
    fi
else
    # fall back to the current known python version
    PYTHONHOME64=/usr/lib64/python2.4
fi

# derive the path to 32-bit .so files from the 64-bit path
PYTHONHOME32=`echo $PYTHONHOME64 | sed -e "s|/usr/lib64|/usr/lib|"`

# the following paths must be mentioned in PYTHONPATH
# it includes two 32-bit paths, main python dir, and lots of paths printed by
# PYTHONPATH=/usr/lib64/python/ python -c 'import sys; print sys.path'
# , all are converted to URL
PYTHONPATH_COMPAT64="file://$PYTHONHOME32/lib-dynload \
file://$PYTHONHOME32/site-packages \
file:///usr/lib64/python \
file://$PYTHONHOME64 \
file://$PYTHONHOME64/plat-linux2 \
file://$PYTHONHOME64/lib-tk \
file://$PYTHONHOME64/lib-dynload \
file://$PYTHONHOME64/site-packages \
file://$PYTHONHOME64/site-packages/Numeric \
file://$PYTHONHOME64/site-packages/PIL \
file://$PYTHONHOME64/site-packages/gtk-2.0"

# check PYTHONPATH
sed_script=`mktemp /tmp/OpenOffice_org-pyunorc.sed.XXXXXXXX`
for path in $PYTHONPATH_COMPAT64 ; do
    if ! grep -q "PYTHONPATH=.*$path" $PYUNORCFILE ; then
        echo "s|^\([[:blank:]]*PYTHONPATH=.*\)$|\1 $path|" >>$sed_script
    fi
done	

# check PYTHONHOME; it must be set to PYTHONHOME32 on x86_64
if grep -q "^[[:blank:]]*PYTHONHOME=" $PYUNORCFILE ; then
    if ! grep -q "^[[:blank:]]*PYTHONHOME=file://$PYTHONHOME32" $PYUNORCFILE ; then
	# just fix the path
	echo "s|^\([[:blank:]]*PYTHONHOME=\).*$|\1file://$PYTHONHOME32|" >>$sed_script
    fi
else
    # the variable is missing at all
    echo "s|^\([[:blank:]]*PYTHONPATH=.*\)$|\1\nPYTHONHOME=file://$PYTHONHOME32|" >>$sed_script
fi	

# update the config file if any problem was found
if test -s $sed_script ; then
    mv -f $PYUNORCFILE $PYUNORCFILE.savedby.pyunorc-update64
    sed -f $sed_script $PYUNORCFILE.savedby.pyunorc-update64 >$PYUNORCFILE
fi

# remove the temporaty file
rm -rf $sed_script
