#!/bin/bash

# Slackware build script for Remind

# Copyright 2019 T3slider <t3slider@gmail.com>
# Copyright 2025 Judah Milgram <milgrama@cgpp.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.

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

PRGNAM=remind
VERSION=${VERSION:-06.01.06}
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 [ ! -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

LATITUDE=${LATITUDE:-45.42055555555555}
LONGITUDE=${LONGITUDE:--75.68944444444445}
LOCATION=${LOCATION:-Ottawa}
PAGE=${PAGE:-Letter}
DATESEP=${DATESEP:--}
TIMESEP=${TIMESEP:-:}
DATETIMESEP=${DATETIMESEP:-@}

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 {} \;

CUSTOM=src/custom.h

sed -i "s/^\(#define DEFAULT_LATITUDE \).*/\1$LATITUDE/" $CUSTOM
sed -i "s/^\(#define DEFAULT_LONGITUDE \).*/\1$LONGITUDE/" $CUSTOM
sed -i "s/^\(#define LOCATION \).*/\1\"$LOCATION\"/" $CUSTOM

# W and H are in pts
case ${PAGE} in
    "Letter") W=612; H=792;;     # 8.5 x 11 in.
    "Tabloid") W=792; 1224;;     # 11 x 17 in.
    "Ledger") W=1224; H=792;;    # 17 x 11 in.
    "Legal") W=612; H=1008;;     # 8.5 x 14 in.
    "Statement") W=396; H=612;;  # 5.5 x 8.5 in.
    "Executive") W=540; H=720;;  # 7.5 x 10 in.
    "A3") W=842; H=1190;;        # 29.7 x 42 cm.
    "A4") W=595; H=842;;         # 21 x 29.7 cm.
    "A5") W=420; H=595;;         # 14.8 x 21 cm.
    "B4") W=729; H=1032;;        # 25.7 x 36.4 cm.
    "B5") W=519; H=729;;         # 18.3 x 25.7 cm.
    "Folio") W=612; H=936;;      # 8.5 x 13 in.
    "Quarto") W=612, H=780;;     # 8.5 x 10.8 in.
    "10x14") W=720; H=1008;;     # 10 x 14 in.
    *) echo rem2ps page size can be set directly with -m option. See the man page.;;
esac
sed -i "s/^\(#define DEFAULT_PAGE \).*/\1{\"$PAGE\", $W, $H}/" $CUSTOM

sed -i "s/^\(#define DATESEP \).*/\1'$DATESEP'/" $CUSTOM
sed -i "s/^\(#define TIMESEP \).*/\1'$TIMESEP'/" $CUSTOM
sed -i "s/^\(#define DATETIMESEP \).*/\1'$DATETIMESEP'/" $CUSTOM

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 \
  --disable-perl-build-artifacts

export INSTALL_BASE=/usr
make
make install DESTDIR=$PKG

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
cp -a COPYRIGHT README.md docs/* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

cd $PKG/usr/bin
rm -rf rem
ln -sf remind rem
cd $PKG/usr/share/remind/holidays
rm -rf uk
ln -sf gb uk

chmod 0644 $PKG/usr/share/applications/tkremind.desktop

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

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