#!/bin/bash

# Slackware build script for ngspice and libngspice
# Written by B. Jogai <jogaib {at} comcast [dot] net>
# Now maintained by R. S. Ananda Murthy (rsamurti@gmail.com)

# Set initial variables:

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=ngspice
VERSION=${VERSION:-36}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# Fix the lib path to the code modules
if [ "$ARCH" = "x86_64" ]; then
  sed -i "s%/lib/%/lib${LIBDIRSUFFIX}/%g" src/spinit.in
fi

mkdir release
cd release

  ../configure \
  --prefix=/usr \
  --mandir=/usr/man \
  --infodir=/usr/info \
  --enable-xspice \
  --enable-cider \
  --enable-openmp \
  --with-x \
  --disable-debug \
  --with-ngshared

make

CFLAGS="$SLKCFLAGS" \
  ../configure --prefix=/usr \
  --bindir=/usr/bin \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --mandir=/usr/man \
  --infodir=/usr/info \
  --disable-debug \
  --enable-xspice \
  --enable-cider \
  --enable-shared \
  --disable-static \
  --enable-ndev \
  --enable-openmp \
  --with-x \
  --build=$ARCH-slackware-linux

make
make DESTDIR=$PKG install

# This has installed the configure output file as  /usr/include/config.h.

cd ..

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

find $PKG/usr/man -type f -exec gzip -9 {} \;
for i in $( find $PKG/usr/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION/examples

cp -a \
  ANALYSES AUTHORS BUGS COPYING ChangeLog DEVICES FAQ \
  INSTALL NEWS README* examples/ \
  $PKG/usr/doc/$PRGNAM-$VERSION/

cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $CWD/$PRGNAM-$VERSION-manual.pdf \
  > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM-$VERSION-manual.pdf

# The shared library enabled by "--with-ngshared"
install -Dm755 ./release/src/.libs/libngspice.so.0.0.2 \
  "$PKG/usr/lib${LIBDIRSUFFIX}/libngspice.so.0.0.2"

ln -s "libngspice.so.0.0.2" "$PKG/usr/lib${LIBDIRSUFFIX}/libngspice.so.0"
ln -s "libngspice.so.0.0.2" "$PKG/usr/lib${LIBDIRSUFFIX}/libngspice.so"

# The include file needed to use the shared library enabled by "--with-ngshared"
install -Dm644 ./src/include/ngspice/sharedspice.h  $PKG/usr/include/ngspice/sharedspice.h
# Move the configure output file.
mv $TMP/$PRGNAM-$VERSION/release/src/include/ngspice/config.h $PKG/usr/include/ngspice/

install -Dm644 $CWD/$PRGNAM.png $PKG/usr/share/icons/hicolor/48x48/apps/$PRGNAM.png

mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
