#!/bin/bash

DESTDIR=./dist
VERSION=$(git describe --tag | sed 's/^v//')
ITERATION=1
NAME=scw-apache-lua
#BINARY=$NAME
#DEFAULTS_FILE="$BINARY"
#CONFIG_FILE="$NAME.toml"
SERVICE_FILE="$NAME.conf"
SERVICE_DIR="/etc/systemd/system/httpd.service.d"
PKG_TYPE=rpm
DESCR="ScraperWall Integration of Blacklist, Captcha and Apache"
OUTDIR=./rpms-centos7
RPM_DIR=/opt/rpm.scraperwall.com/centos7


rm -rf $DESTDIR
install -d $DESTDIR/usr/local/httpd
install -d $DESTDIR/etc/default
install -d $DESTDIR/etc/httpd/conf.d
install -d ${DESTDIR}${SERVICE_DIR}

install -v -m 644 scw-apache-lua.conf ${DESTDIR}${SERVICE_DIR}/${SERVICE_FILE}
install -v -m 644 centos7-httpd-lua.conf $DESTDIR/etc/httpd/conf.d/scw-apache-lua.conf
cp -rv lua $DESTDIR/usr/local/httpd
install -v -m 644 default $DESTDIR/etc/default/$NAME



fpm -s dir -t $PKG_TYPE -C $DESTDIR --name $NAME \
  --version $VERSION \
  --iteration $ITERATION \
  --description "$DESCR" \
  --config-files "etc/default/$NAME" \
  -p $OUTDIR \
  --rpm-sign

# --rpm-trigger-after-install "[]$BINARY: ./centos7-install.sh" \
# --rpm-trigger-before-uninstall "[]$BINARY: ./centos7-uninstall.sh" \

ok=$?

rm -rf $DESTDIR

rpm_file="$OUTDIR/$NAME-$VERSION-$ITERATION.x86_64.rpm"
if [ $ok -eq 0 -a -f "$rpm_file" ]; then
	# rm -f "$RPM_DIR/$BINARY-*.rpm"
  cp "$rpm_file" "$RPM_DIR"

	(cd "$RPM_DIR" && \
   createrepo -v . && \
   rsync -av --progress --delete . fender.spyz.org:/srv/http-vhosts/rpm.scraperwall.com/centos7/)
fi