#!/bin/sh

# Copyright (C) 2000-2019 The Xastir Group

# TODO:
# Check for a link at /opt/Xastir/share/xastir.  If found, remove it
# until we're done creating the .tar.bz2 file.  Perhaps do an rm -rf
# there and symlink it to /usr/local/share/xastir when done.

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

# The trick:  First compile Lesstif as an LSB package, installed at
# /opt/lsb-tmp, using LSB-BUILD-LESSTIF script.  The commands
# below assume you've set up the directory structure at /opt/lsb
# specified at the top of that script.
#
# We should have /opt/lsb/lib.orig & /opt/lsb/lib.lesstif
# directories at this point along with a symlink from /opt/lsb/lib
# -> lib.lesstif:
#
#   cd /opt/lsb
#   cp -R lib.orig lib.xastir
#   rm lib                 (remove the symlink we created before)
#   ln -s lib.xastir lib   (link to the new directory)
#   cd lib
#   rm libXt.so            (or just "chmod 000 libXt.so")
#   rm libz.so             (or just "chmod 000 libz.so")
#   cp /home/src/lsb/libXt.a .      (lib from LSB CVS: "lsb/appbat" module)
#   ln -s /opt/lsb-tmp/lib/libXm.a (from our Lesstif compile)
#   ln -s /opt/lsb-tmp/lib/libz.a
#   ln -s /opt/lsb-tmp/lib/libjpeg.a
#   ln -s /opt/lsb-tmp/lib/libpcre.a
#   ln -s /opt/lsb-tmp/lib/libcurl.a
#   ln -s /usr/local/lib/libtiff.a
#   ln -s /usr/local/lib/libproj.a
#   ln -s /usr/local/lib/libgeotiff.a
#   ln -s /opt/lsb-tmp/lib/libpng.a
#   ln -s /opt/lsb-tmp/lib/libjasper.a
#   ln -s /opt/lsb-tmp/lib/libGraphicsMagick.a libMagick.a
#   ln -s /opt/lsb-tmp/lib/libdb.a
#   ln -s /opt/lsb-tmp/lib/libXpm.a
#
#   cd /opt/lsb
#   cp -R include include.xastir
#   mv include include.orig
#   ln -s include.xastir include  (link to the new directory)
#   cd include
#   ln -s /opt/lsb-tmp/include/Xm (from our Lesstif compile)
#   ln -s /opt/lsb-tmp/include/pcre.h
#   ln -s /opt/lsb-tmp/include/curl
#   ln -s /opt/lsb-tmp/include/png.h
#   ln -s /opt/lsb-tmp/include/jasper
#   rm zlib.h
#   ln -s /opt/lsb-tmp/include/zlib.h
#   ln -s /opt/lsb-tmp/include/zconf.h
#   ln -s /opt/lsb-tmp/include/GraphicsMagick/magick
#   ln -s /opt/lsb-tmp/include/db.h
#   mkdir X11
#   cd X11
#   ln -s /opt/lsb-tmp/include/Xm/xpm.h

# This will allow us to statically link libraries without mucking
# around with the Makefiles.  When done compiling the LSB-Xastir
# package, put the /opt/lsb/lib directory back to normal by issuing
# these commands:
#
#  cd /opt/lsb
#  rm lib include             (remove the symlinks)
#  ln -s lib.orig lib         (link back to the original directory)
#  ln -s include.orig include (link back to the original directory)
#


REV="2.0.8"
FILENAME=`date +xastir-lsb-$REV-%Y-%b-%d.tar.bz2`


# Remove old versions of the package
rm xastir-lsb-*.bz2


./bootstrap.sh

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=libXm.so

CC=lsbcc CXX=lsbc++ ./configure \
    --prefix=/opt/Xastir \
    --without-ax25 \
    --with-festival \
    --without-gpsman \
    --with-graphicsmagick \
    --with-proj \
    --with-geotiff \
    --without-gdal \
    --with-internal-shapelib \
    --with-pcre \
    --with-dbfawk \
    --with-map_cache \
    --with-rtree \
    --with-lsb

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/X11R6/lib@/opt/lsb/lib@g' $i
    sed -i 's@/usr/X11/lib@/opt/lsb/lib@g' $i
    sed -i 's@/usr/X11/include@/opt/lsb/include@g' $i
    sed -i 's@/usr/local/lib@/opt/lsb/lib@g' $i
  done

(make 2>&1) | tee make.log
strip src/xastir
strip src/xastir_udp_client
strip src/testdbfawk
strip callpass/callpass

# Get rid of the LSB-Xastir install files so that we get a clean
# install each time to make the .tar.bz2 file from.
echo Removing /opt/Xastir/
sudo /bin/rm -rf /opt/Xastir

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

# Copy GraphicsMagick "gm" binary to the Xastir area
sudo /bin/cp /opt/lsb-tmp/bin/gm /opt/Xastir/bin/.

#(lsbappchk -nA /opt/Xastir/bin/xastir 2>&1) | grep -v SHT_NOTE
#(lsbappchk -nA /opt/Xastir/bin/callpass 2>&1) | grep -v SHT_NOTE
#(lsbappchk -nA /opt/Xastir/bin/xastir_udp_client 2>&1) | grep -v SHT_NOTE
#(lsbappchk -nA /opt/Xastir/bin/gm 2>&1) | grep -v SHT_NOTE
(lsbappchk -n /opt/Xastir/bin/xastir 2>&1) | grep -v SHT_NOTE
(lsbappchk -n /opt/Xastir/bin/callpass 2>&1) | grep -v SHT_NOTE
(lsbappchk -n /opt/Xastir/bin/xastir_udp_client 2>&1) | grep -v SHT_NOTE
(lsbappchk -n /opt/Xastir/bin/gm 2>&1) | grep -v SHT_NOTE


tar cjf $FILENAME /opt/Xastir

# Remove old versions of the package from the ftp site
#ssh eskimo rm ftp/aprs/xastir/LSB/xastir-lsb*

# Upload the latest version
scp xastir-lsb-$REV-* eskimo:ftp/aprs/xastir/LSB/.

# Fix the permissions so that people can download the file
ssh eskimo chmod 644 ftp/aprs/xastir/LSB/*


