#!/bin/sh

# Slackware build script for tcollector

# Copyright 2017,2019,2022 T.J. Lipscomb <tj@tjl.co>
# 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=tcollector
VERSION=${VERSION:-20220225_127f234}
GITHASH=${GITHASH:-127f23439ee1049ba746c2fbc8c337ca63573d55}
BUILD=${BUILD:-2}
TAG=${TAG:-tjl}

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}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

cd $TMP
rm -rf $PRGNAM-$GITHASH
tar xvf $CWD/$PRGNAM-$GITHASH.tar.gz
cd $PRGNAM-$GITHASH
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 {} \;
 
mkdir -p $PKG/usr/libexec/tcollector/collectors/etc/
mkdir -p $PKG/usr/libexec/tcollector/collectors/lib/
mkdir -p $PKG/usr/libexec/tcollector/collectors/lib/docker_engine
mkdir -p $PKG/usr/libexec/tcollector/collectors/0/
mkdir -p $PKG/usr/libexec/tcollector/collectors/300/
mkdir -p $PKG/usr/libexec/tcollector/collectors/900/
install -m 0644 collectors/__init__.py $PKG/usr/libexec/tcollector/collectors/
install -m 0644 collectors/etc/* $PKG/usr/libexec/tcollector/collectors/etc/
install -m 0644 collectors/lib/*.py $PKG/usr/libexec/tcollector/collectors/lib/
install -m 0644 collectors/lib/docker_engine/* $PKG/usr/libexec/tcollector/collectors/lib/docker_engine
install -m 0644 collectors/0/* $PKG/usr/libexec/tcollector/collectors/0/
install -m 0644 collectors/300/* $PKG/usr/libexec/tcollector/collectors/300/
install -m 0644 collectors/900/* $PKG/usr/libexec/tcollector/collectors/900/

install -m 0644 -D tcollector.py $PKG/usr/libexec/tcollector/
install -m 0755 -D tcollector $PKG/usr/libexec/tcollector/

find $PKG/usr/libexec/tcollector/collectors/etc/*.py -name '*.py' -exec sh -c 'mv "${0}" "${0}.new"' {} \;
# Don't mark this one as a config file.
mv $PKG/usr/libexec/tcollector/collectors/etc/__init__.py.new $PKG/usr/libexec/tcollector/collectors/etc/__init__.py

mkdir -p $PKG/usr/sbin/
ln -s /usr/libexec/tcollector/tcollector $PKG/usr/sbin/

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS CHANGELOG.md COPYING COPYING.LESSER README.md THANKS $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

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:-tgz}
