#!/bin/sh

# Copyright (C) 2000-2019 The Xastir Group
# <http://www.lesstif.org/>

# Compile lesstif-0.95.0 under Linux Standard Base 3.0.  Install
# into /opt/lsb-tmp/ directory structure.

# NOTE:
# "Free Standards Group, FSG, Linux Standard Base, LSB, Free
# Standards Certified, LSB Certified and the Free Standards
# Certified logo are trademarks, service marks and certification
# marks, as appropriate, of Free Standards Group in the United
# States and in other countries."
#
# We are in no way representing that Xastir has been certified by
# the FSG.  To do so costs real money.  We do intend some Xastir
# binaries to install and run properly on LSB-3.0 compliant x86
# Linux systems though...
#

# In order to build Lesstif on LSB we need a static version of the
# LSB libXt.so library, called libXt.a.  To get this, we must
# compile the "lsb/appbat" portion of LSB-CVS and snag the library
# out of there.  We can then copy it or symlink it into /opt/lsb/lib
# and change permissions on /opt/lsb/lib/libXt.so so that it's
# unreadable.  We'll get the static version of libXt compiled in to
# our app that way.  Put the .so back and disable the .a when we're
# done compiling Lesstif and Xastir so that the LSB runtime is
# intact.
#
# Actual method used:
#
#   cd /opt/lsb
#   cp -R lib lib.lesstif
#   mv lib lib.orig
#   ln -s lib.lesstif lib
#   cd lib
#   rm libXt.so          (or just "chmod 000 libXt.so")
#   ln -s PATH/libXt.a   (from LSB "appbat")
#
# This makes it easy to switch back to the default library setup by
# changing the /opt/lsb/lib symlink.
#


export PATH=${PATH}:/opt/lsb/bin
export LSBCC_WARN=1

# A colon-separated list of "extra" shared libraries to link with
# the application.  Each shared lib must be LSB-compliant and must
# be distributed along with the application.
#export LSBCC_SHAREDLIBS=


# Patches to the Lesstif source code:
#
# Edit lib/Xm/ImageCache.c: Change "_XInitImageFuncPtrs" to
# "XInitImage" in two places, then comment out the first instance:
sed -i -e 's/_XInitImageFuncPtrs/XInitImage/g' lib/Xm-2.1/ImageCache.c
sed -i -e 's/^extern void XInitImage/\/\/extern void XInitImage/g' lib/Xm-2.1/ImageCache.c
#
# Comment out the sighandler_t line in lib/Xm/DebugUtil.c:
sed -i -e 's/^typedef void ..sighandler_t/\/\/typedef void (*sighandler_t/' lib/Xm-2.1/DebugUtil.c


CC=lsbcc CXX=lsbc++ ./configure \
    --prefix=/opt/lsb-tmp \
    --enable-static \
    --x-libraries=/opt/lsb/lib \
    --x-includes=/opt/lsb/include \
    --with-motif-includes=/opt/lsb/include \
    --with-motif-libraries=/opt/lsb/lib \
    --disable-shared

make clean

find . -type f -name Makefile -print | while read i
  do
    sed -i 's@/usr/include@opt/lsb/include@g' $i
    sed -i 's@/usr/local/include@opt/lsb/include@g' $i
    sed -i 's@/usr/X11R6/include@opt/lsb/include@g' $i
    sed -i 's@/usr/lib@opt/lsb/lib@g' $i
    sed -i 's@/usr/local/lib@opt/lsb/lib@g' $i
    sed -i 's@/usr/X11R6/lib@opt/lsb/lib@g' $i
    sed -i 's@/usr/X11/lib@opt/lsb/lib@g' $i
  done

(make 2>&1) | tee make.log

#make -n install
sudo make install


