#!/bin/sh
#-- nspr-compat32 for Slackware --
# Build script by Phantom X <megaphantomx at bol.com.br>
# Suggested usage: $ nspr-compat32.SlackBuild 2>&1 | tee build.log
#--
# Copyright 2008, 2009, 2010, 2011 Phantom X, Goiania, Brazil.
# Copyright 2006 Martijn Dekker, Groningen, Netherlands.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# http://www.mozilla.org/projects/nspr/

PACKAGER_ID=${PACKAGER_ID:-$USER}
PACKAGER=${PACKAGER:-$USER@$HOSTNAME}

# Set YES for native build with gcc >= 4.2
SB_NATIVE=${SB_NATIVE:-NO}

# Set to YES to replicate slackbuild and patches
SB_REP=${SB_REP:-YES}

CWD=$(pwd)
TMP=${TMP:-/tmp}
if [ ! -d ${TMP} ]; then
  mkdir -p ${TMP}
fi

PNAME=nspr
NAME=nspr-compat32
PKG=${PKG:-${TMP}/package-${NAME}}

VERSION=${VERSION:-4.8.7}
if [ "${SB_NATIVE}" = "YES" ] ;then
  ARCH32=${ARCH32:-$(linux32 uname -m)}
else
  ARCH32=${ARCH32:-i686}
fi
SLKTARGET32=${SLKTARGET32:-i486}
SLKDTARGET=${SLKDTARGET:-slackware}
BUILD=${BUILD:-1}
NJOBS=${NJOBS:-$(( $(getconf _NPROCESSORS_ONLN) + 1 ))}
DOCDIR=${PKG}/usr/doc/${NAME}-${VERSION}
SBDIR=${PKG}/usr/src/slackbuilds/${PNAME}
PKGDEST=${PKGDEST:-${CWD}}
PKGFORMAT=${PKGFORMAT:-txz}
PKGNAME=${NAME}-$(echo ${VERSION} | tr - . )-${ARCH32}-${BUILD}${PACKAGER_ID}

DATE=$(LC_ALL=C date +%d-%b-%Y)

SRCDIR=${PNAME}-${VERSION}
SRCARCHIVE=${SRCDIR}.tar.gz

DL_PROG=${DL_PROG:-wget}
DL_TO=${DL_TO:-5}
DL_OPTS=${DL_OPTS:-"--timeout=${DL_TO}"}
DL_URL="http://ftp.mozilla.org/pub/mozilla.org/${PNAME}/releases/v${VERSION}/src/${SRCARCHIVE}"
FEDORADIST=fedora-dist.sh
FEDORABRANCH=${FEDORABRANCH:-devel}

# if source is not present, download in source rootdir if possible
test -r ${CWD}/${SRCARCHIVE} || ${DL_PROG} ${DL_OPTS} ${DL_URL}
if ! test -r ${CWD}/${SRCARCHIVE} ;then
  export SRCARCHIVE=${SRCDIR}.tar.bz2
  sh ${CWD}/${FEDORADIST} ${PNAME} ${FEDORABRANCH} ${SRCARCHIVE} || exit 1
fi

if [ "${SB_NATIVE}" = "YES" ] ;then
  SLKCFLAGS="-O2 -march=native -mtune=native ${SB_ECFLAGS} -pipe"
else
  case "${ARCH32}" in
    i[3-6]86)    SLKCFLAGS="-O2 -march=${ARCH32} -mtune=i686"
                 ;;
  esac
fi
LIBDIRSUFFIX=""
export CC="gcc -m32"
export CXX="g++ -m32"
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:${PKG_CONFIG_PATH}"

if [ -d ${PKG} ]; then
  # Clean up a previous build
  rm -rf ${PKG}
fi
mkdir -p ${PKG}

cd ${TMP}
rm -rf ${SRCDIR}
tar -xvf ${CWD}/${SRCARCHIVE} || exit 1
cd ${SRCDIR} || exit 1

chmod -R u+w,go+r-w,a-s .

cp ./mozilla/nsprpub/config/nspr-config.in \
   ./mozilla/nsprpub/config/nspr-config-pc.in || exit 1

if [ -r ${CWD}/apply-patches.sh ]; then
  . ${CWD}/apply-patches.sh || exit 1
fi

sed -i -e 's/\$(MKSHLIB) \$(OBJS)/\$(MKSHLIB) \$(LDFLAGS) \$(OBJS)/g' \
           mozilla/nsprpub/config/rules.mk || exit 1

zcat ${CWD}/nspr-config-vars.in.gz \
  > ./mozilla/nsprpub/config/nspr-config-vars.in || exit 1

./mozilla/nsprpub/configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --includedir=/usr/include/nspr4 \
  --enable-optimize="${SLKCFLAGS}" \
  --disable-debug \
  --build=${SLKTARGET32}-${SLKDTARGET}-linux \
  --host=${SLKTARGET32}-${SLKDTARGET}-linux \
  --target=${SLKTARGET32}-${SLKDTARGET}-linux|| exit 1

make || exit 1

DESTDIR=${PKG} \
  make install || exit 1

NSPR_LIBS=$(./config/nspr-config --libs)
NSPR_CFLAGS=$(./config/nspr-config --cflags)
NSPR_VERSION=$(./config/nspr-config --version)
mkdir -p ${PKG}/usr/lib${LIBDIRSUFFIX}/pkgconfig

cat ./config/nspr-config-vars \
  > ${PKG}/usr/lib${LIBDIRSUFFIX}/pkgconfig/nspr-standalone.pc || exit 1


zcat ${CWD}/nspr.pc.in.gz | \
  sed -e "s,_libdir_,/usr/lib${LIBDIRSUFFIX},g" \
      -e "s,_prefix_,/usr,g" \
      -e "s,_execprefix_,/usr,g" \
      -e "s,_includedir_,/usr/include/nspr4,g" \
      -e "s,_NSPR_VERSION_,${NSPR_VERSION},g" \
      -e "s,_FULL_NSPR_LIBS_,${NSPR_LIBS},g" \
      -e "s,_FULL_NSPR_CFLAGS_,${NSPR_CFLAGS},g" \
      >> ${PKG}/usr/lib${LIBDIRSUFFIX}/pkgconfig/nspr-standalone.pc || exit 1

ln -sf nspr-standalone.pc ${PKG}/usr/lib${LIBDIRSUFFIX}/pkgconfig/nspr.pc || exit 1

mkdir -p ${PKG}/lib${LIBDIRSUFFIX}

# Get rid of the things we don't want installed (per upstream)
rm -rf \
   ${PKG}/usr/bin/compile-et.pl \
   ${PKG}/usr/bin/prerr.properties \
   ${PKG}/usr/lib${LIBDIRSUFFIX}/libnspr4.a \
   ${PKG}/usr/lib${LIBDIRSUFFIX}/libplc4.a \
   ${PKG}/usr/lib${LIBDIRSUFFIX}/libplds4.a \
   ${PKG}/usr/share/aclocal/nspr.m4 \
   ${PKG}/usr/include/nspr4/md || exit 1

for file in libnspr4.so libplc4.so libplds4.so
do
  mv -f ${PKG}/usr/lib${LIBDIRSUFFIX}/${file} ${PKG}/lib${LIBDIRSUFFIX}/${file} || exit 1
  ln -sf ../../lib${LIBDIRSUFFIX}/${file} ${PKG}/usr/lib${LIBDIRSUFFIX}/${file} || exit 1
done

rm -rf ${PKG}/usr/{bin,sbin,doc,include,libexec,man,info,share} ${PKG}/var
rmdir ${PKG}/usr/share/aclocal
rmdir ${PKG}/usr/share

find ${PKG} | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

mkdir -p ${PKG}/install
cat ${CWD}/slack-desc-compat32 > ${PKG}/install/slack-desc
cat ${CWD}/slack-required > ${PKG}/install/slack-required

cat > ${PKG}/install/doinst.sh <<EOF
if grep '^/usr/lib${LIBDIRSUFFIX}/seamonkey' etc/ld.so.conf 1> /dev/null 2> /dev/null ; then
  sed -i -e '/^\/usr\/lib${LIBDIRSUFFIX}\/seamonkey/s|^|#|g' etc/ld.so.conf
fi
if [ -x /sbin/ldconfig ]; then
  /sbin/ldconfig 2> /dev/null
fi
EOF

sed -i "s|_PACKAGER|${PACKAGER}|g; s|_BUILD_DATE|${DATE}|g" \
       ${PKG}/install/slack-desc

if [ "${SB_REP}" = "YES" ] ;then
  # Replicate slackbuild and patches
  mkdir -p ${SBDIR}
  install -m0644 ${CWD}/slack-desc-compat32 ${SBDIR}/
  install -m0755 ${CWD}/${NAME}.SlackBuild \
                 ${SBDIR}/${NAME}.SlackBuild
fi


# Build package:
set +o xtrace        # no longer print commands upon execution

ROOTCOMMANDS="set -o errexit -o xtrace ; cd ${PKG} ;
  /bin/chown --recursive root:root .  ;"

ROOTCOMMANDS="${ROOTCOMMANDS}
  /sbin/makepkg --linkadd y --chown n --prepend ${PKGDEST}/${PKGNAME}.${PKGFORMAT} "

if test ${UID} = 0; then
  eval ${ROOTCOMMANDS}
  set +o xtrace
elif test "$(type -t fakeroot)" = 'file'; then
  echo -e "\e[1mEntering fakeroot environment.\e[0m"
  echo ${ROOTCOMMANDS} | fakeroot
else
  echo -e "\e[1mPlease enter your root password.\e[0m (Consider installing fakeroot.)"
  /bin/su -c "${ROOTCOMMANDS}"
fi

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  echo "Cleaning..."
  if [ -d ${TMP}/${SRCDIR} ]; then
    rm -rf ${TMP}/${SRCDIR} && echo "${TMP}/${SRCDIR} cleanup completed"
  fi
  if [ -d ${PKG} ]; then
    rm -rf ${PKG} && echo "${PKG} cleanup completed"
  fi
  rmdir ${TMP} && echo "${TMP} cleanup completed"
fi
exit 0
