#!/bin/sh

# Slackware build script for Remind

# Written by T3slider <t3slider@gmail.com>
# All rights reserved.
#
# 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.

PRGNAM=remind
VERSION=${VERSION:-03.01.15}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

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

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -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

# Valid options for LANG are one of the following:
# ENGLISH, GERMAN, DUTCH, FINNISH, FRENCH, NORWEGIAN, DANISH, POLISH, BRAZPORT,
# ITALIAN, ROMANIAN, SPANISH, ICELANDIC
LANGUAGE=${LANGUAGE:-ENGLISH}

# You can also pass your location via the following variables (this ensures that
# sunrise, sunset, etc. work as expected for your location):
# LAT_DEG, LAT_MIN, LAT_SEC, LON_DEG, LON_MIN, LON_SEC, LOCATION
# By default these point to Ottawa, Ontario. From custom.h:

# LAT_DEG, LAT_MIN and LAT_SEC: Latitude of your location
# LON_DEG, LON_MIN and LON_SEC: Longitude of your location
# LOCATION: A string identifying your location.
# All latitude and longitude numbers should be positive for the
# northern and western hemisphere.  If you live in the southern
# hemisphere, ALL latitude values should be negative.  If you live
# in the eastern hemisphere, ALL longitude values should be negative.
# The default values are initially set to Ottawa, Ontario, Canada.
LAT_DEG=${LAT_DEG:-45}
LAT_MIN=${LAT_MIN:-24}
LAT_SEC=${LAT_SEC:-0}
LON_DEG=${LON_DEG:-75}
LON_MIN=${LON_MIN:-39}
LON_SEC=${LON_SEC:-0}
LOCATION=${LOCATION:-Ottawa}

# Default page size to use for Rem2PS. Options are Letter or A4.
DEFAULT_PAGE=${DEFAULT_PAGE:-Letter}

# Default date separator (this works for input only). Options are
# - or /
DATESEP=${DATESEP:--}

# Default time separator. Options are : or .
TIMESEP=${TIMESEP:-:}

# Character set to use. Options are ASCII, ISOLATIN1, ISOLATIN2,
# IBMEXTENDED, or IBM852.
CHARSET=${CHARSET:-ISOLATIN1}

# If you do not want the -u option (which runs remind with the
# uid and gid of the specified name), pass "no" (without quotes)
# to this option.
WANTU=${WANTU:-yes}

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 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

# You can also use Tcl/Tk's `wish` to configure and build remind graphically,
# but this is untested. If you wish to try, uncomment the following line:
#wish ./build.tk
# Note that this would also require commenting out everything up to (but NOT
# including) the "make install" line below, since that should (in theory) be
# done by the build.tk script. I don't know how this would affect the package
# directories. I would advise passing the options as variables without using
# wish unless you want to have fun.

# Set the proper options from the options passed to this script. Sorry for
# the mess...
sed -i "s/^\(#define LANG \).*/\1$LANGUAGE/" src/lang.h
sed -i "s/^\(#define LAT_DEG \).*/\1$LAT_DEG/" src/custom.h
sed -i "s/^\(#define LAT_MIN \).*/\1$LAT_MIN/" src/custom.h
sed -i "s/^\(#define LAT_SEC \).*/\1$LAT_SEC/" src/custom.h
sed -i "s/^\(#define LON_DEG \).*/\1$LON_DEG/" src/custom.h
sed -i "s/^\(#define LON_MIN \).*/\1$LON_MIN/" src/custom.h
sed -i "s/^\(#define LON_SEC \).*/\1$LON_SEC/" src/custom.h
sed -i "s/^\(#define LOCATION \).*/\1\"$LOCATION\"/" src/custom.h
if [ "$DEFAULT_PAGE" = "A4" ]; then
	sed -i 's|^#define DEFAULT_PAGE {"Letter", 612, 792}|/* & */|' src/custom.h
	sed -i 's|^\(/\* \)\(#define DEFAULT_PAGE {"A4", 595, 842}\)\( \*/\)|\2|' src/custom.h
fi
if [ "$DATESEP" = "/" ]; then
	sed -i "s|^#define DATESEP '-'|/* & */|" src/custom.h
	sed -i "s|^\(/\* \)\(#define DATESEP '/'\)\( \*/\)|\2|" src/custom.h
fi
if [ "$TIMESEP" = "." ]; then
	sed -i "s|^#define TIMESEP ':'|/* & */|" src/custom.h
	sed -i "s|^\(/\* \)\(#define TIMESEP '\.'\)\( \*/\)|\2|" src/custom.h
fi
sed -i "s/^\(#define ISOLATIN1 \).*/\1 0/" src/custom.h
if [ "$CHARSET" != "ASCII" ]; then
	sed -i "s/^\(#define $CHARSET \).*/\1 1/" src/custom.h
fi
if [ "$WANTU" = "no" ]; then
	sed -i 's|^#define WANT_U_OPTION 1|/* & */|' src/custom.h
fi

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --build=$ARCH-slackware-linux

make
make install DESTDIR=$PKG

find $PKG | xargs 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
cp -a COPYRIGHT MICROSOFT-AND-APPLE README $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$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:-tgz}