#!/bin/bash

# Slackware build script for Artistic Style

# Copyright 2020 B. Watson <yalhcru@gmail.com>

# Copyright 2017,2018 Nate Bargmann <email removed>

# Ryan P.C. McQuen | Everett, WA | <email removed>

# Formerly maintained by Daniel Jordan <email removed>
#    - updated version number
#    - added default variable options
#    - auto detect ARCH
#    - build and install shared libraries [bkw: say what?]
#
# Originally by Dugan Chen <email removed>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version, with the following exception:
# the text of the GPL license may be omitted.

# 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. Compiling,
# interpreting, executing or merely reading the text of the program
# may result in lapses of consciousness and/or very being, up to and
# including the end of all existence and the Universe as we know it.
# See the GNU General Public License for more details.

# You may have received a copy of the GNU General Public License
# along with this program (most likely, a file named COPYING). If
# not, see <http://www.gnu.org/licenses/>.

# 20200113 bkw:
# - Take over maintenance.
# - Add man page and privacy patch, BUILD=2.
# - Get rid of "shared" from make command. We weren't installing the
#   shared library anyway, despite the comment above from Daniel
#   Jordan. The only thing that lists astyle as a dependency is ebe,
#   and that's a runtime dep (it executes the astyle command, doesn't
#   link the shared lib). If someone needs the shared lib, I can add
#   it to the build, but for now I'm leaving it alone because the
#   .so isn't versioned (upgrades may cause breakage).
# - Simplify and tweak the script.

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

PRGNAM=astyle
VERSION=${VERSION:-3.1}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

TARBALL="astyle_${VERSION}_linux.tar.gz"

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

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
tar xvf $CWD/$TARBALL
cd $PRGNAM
chown -R root:root .

# Don't change this back to template please. Every file in the tarball
# is +x, and *none* of them need to be.
find -L .  -type d -a -exec chmod 755 {} \+ -o \
           -type f -a -exec chmod 644 {} \+

DOCDIR=$PKG/usr/doc/$PRGNAM-$VERSION

# This patch (from Debian) prevents the user's browser from trying to
# load images from sourceforge, when viewing the *local* HTML docs in
# a browser.
patch -p1 < $CWD/privacy.patch

CFLAGS="$SLKCFLAGS -Wl,-s" \
CXXFLAGS="$SLKCFLAGS -Wl,-s" \
  make -C build/gcc release
  make -C build/gcc install prefix=$PKG/usr SYSCONF_PATH=$DOCDIR

# Man page borrowed from Debian. It's kind of a placeholder, will
# expand it someday.
mkdir -p $PKG/usr/man/man1
sed -e "s|@VERSION@|$VERSION|" \
    -e "s|@DATE@|$( date +"%B %e, %Y" )|" \
    $CWD/$PRGNAM.1 | \
    gzip -9c > $PKG/usr/man/man1/$PRGNAM.1.gz

cp -a *.md file $DOCDIR
sed -i 's,\r,,' $DOCDIR/file/*

cat $CWD/$PRGNAM.SlackBuild > $DOCDIR/$PRGNAM.SlackBuild

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

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