#!/bin/sh

# Slackware build script for r2e

# Written by Tom Canich <tcanich@canich.net>
# Released under the BSD
# Modified by Robby Workman <rworkman@slackbuilds.org>
# Copyright (c) 2009, Tom Canich, State College, Pennsylvania, USA
# Copyright 2016, Alexander Verbovetsky, Moscow, Russia
# 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=r2e
PRGNAMLONG=rss2email
VERSION=${VERSION:-2.71}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

ARCH=noarch

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

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAMLONG-$VERSION
tar xvf $CWD/$PRGNAMLONG-$VERSION.tar.gz
chmod -R a=r,a+X,u+w $PRGNAMLONG-$VERSION
cd $PRGNAMLONG-$VERSION
chown -R root:root .

patch -p1 < $CWD/remove-newlines.patch
patch -p1 < $CWD/fix-email-header-injection.patch
sed --in-place \
  -e 's/http:\/\/rss2email.infogami.com/http:\/\/www.allthingsrss.com\/rss2email\//g' \
  -e 's/__version__ = "2.70"/__version__ = "2.71"/' \
  rss2email.py
[ "${CERTVER:-yes}" = "no" ] && patch < $CWD/rss2email.certver.patch

mkdir -p $PKG/usr/share/$PRGNAM
install -m 0644 -o root -g root feedparser.py $PKG/usr/share/$PRGNAM
install -m 0644 -o root -g root html2text.py $PKG/usr/share/$PRGNAM
install -m 0644 -o root -g root rss2email.py $PKG/usr/share/$PRGNAM

mkdir -p $PKG/usr/bin
install -m 0755 -o root -g root $CWD/wrapper.r2e $PKG/usr/bin/r2e

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a CHANGELOG config.py.example readme.html $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/README.Slackware > $PKG/usr/doc/$PRGNAM-$VERSION/README.Slackware
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
sed "s%@VERSION@%$VERSION%g" $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:-tgz}