#!/bin/bash

# Slackware build script for local-persist

# Copyright 2019 Sergey Poznyakoff <gray@gnu.org>
# 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=local-persist
VERSION=${VERSION:-1.3.0}
BUILD=${BUILD:-2}
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 the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
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}

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
# replace glide with go modules
# https://github.com/MatchbookLab/local-persist/pull/82
cat << EOF > go.mod
module local-persist

go 1.15

require (
	github.com/Microsoft/go-winio v0.3.6-0.20160621211438-ce2922f643c8 // indirect
	github.com/Sirupsen/logrus v0.7.3 // indirect
	github.com/coreos/go-systemd v0.0.0-20161102170722-64d5cd7cb947 // indirect
	github.com/coreos/pkg v0.0.0-20161026222926-447b7ec906e5 // indirect
	github.com/docker/distribution v2.5.0-rc.1.0.20161102195617-fbe6e8d212ed+incompatible // indirect
	github.com/docker/engine-api v0.4.0
	github.com/docker/go-connections v0.2.2-0.20161031182214-f512407a188e // indirect
	github.com/docker/go-plugins-helpers v0.0.0-20161031114640-60d242cfd0fb
	github.com/docker/go-units v0.3.2-0.20161020213227-8a7beacffa30 // indirect
	github.com/fatih/color v1.1.0
	github.com/mattn/go-colorable v0.0.7-0.20161102080925-6e26b354bd2b // indirect
	github.com/mattn/go-isatty v0.0.0-20160806122752-66b8e73f3f5c // indirect
	github.com/opencontainers/runc v1.0.0-rc2.0.20161102180326-49ed0a10e4ed // indirect
	github.com/stevvooe/resumable v0.0.0-20180830230917-22b14a53ba50 // indirect
	github.com/stretchr/testify v1.7.0 // indirect
	golang.org/x/net v0.0.0-20160322021652-4876518f9e71
	golang.org/x/sys v0.0.0-20161023150541-c200b10b5d5e // indirect
)
EOF
cat << EOF > go.sum
github.com/Microsoft/go-winio v0.3.6-0.20160621211438-ce2922f643c8 h1:Qp3i6ZUVcAOeTQj0KBsivXCfsnvsBASQYfXXS/N34NY=
github.com/Microsoft/go-winio v0.3.6-0.20160621211438-ce2922f643c8/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
github.com/Sirupsen/logrus v0.7.3 h1:G04jzNEnKQKTemyvbCV32zFbOu+D++ECKnCFazUjPdc=
github.com/Sirupsen/logrus v0.7.3/go.mod h1:rmk17hk6i8ZSAJkSDa7nOxamrG+SP4P0mm+DAvExv4U=
github.com/coreos/go-systemd v0.0.0-20161102170722-64d5cd7cb947 h1:9zLgi/vHKaTOHO7YtcSvAzSiytU13fhg/w5t8YOM/Ho=
github.com/coreos/go-systemd v0.0.0-20161102170722-64d5cd7cb947/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20161026222926-447b7ec906e5 h1:aqoxNZ9nyZ9kabG+daqglH9RtWIbpxDjVQMoUsoVHuo=
github.com/coreos/pkg v0.0.0-20161026222926-447b7ec906e5/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/distribution v2.5.0-rc.1.0.20161102195617-fbe6e8d212ed+incompatible h1:6W0HnJhig6393/Qm1Qv/4mhZl/TdsVuRP24WRVkCi64=
github.com/docker/distribution v2.5.0-rc.1.0.20161102195617-fbe6e8d212ed+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/engine-api v0.4.0 h1:D0Osr6+45yAlQqLyoczv5qJtAu+P0HB0rLCddck03wY=
github.com/docker/engine-api v0.4.0/go.mod h1:xtQCpzf4YysNZCVFfIGIm7qfLvYbxtLkEVVfKhTVOvw=
github.com/docker/go-connections v0.2.2-0.20161031182214-f512407a188e h1:jcaJgljPmPKABWQTL7kZ4bn75pxBwDrzW9PcLkqkH7o=
github.com/docker/go-connections v0.2.2-0.20161031182214-f512407a188e/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
github.com/docker/go-plugins-helpers v0.0.0-20161031114640-60d242cfd0fb h1:3PbRNm4EK4EszBwI3UR2Wv6IR9tT+PozgOdgJjSq8hs=
github.com/docker/go-plugins-helpers v0.0.0-20161031114640-60d242cfd0fb/go.mod h1:LFyLie6XcDbyKGeVK6bHe+9aJTYCxWLBg5IrJZOaXKA=
github.com/docker/go-units v0.3.2-0.20161020213227-8a7beacffa30 h1:bANAGhKtjG23j8rJr6QEKZJlUaRhiTuq7kCaYV9Ggbg=
github.com/docker/go-units v0.3.2-0.20161020213227-8a7beacffa30/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/fatih/color v1.1.0 h1:4RQHlUrrLRssqNPpcM+ZLy+alwucmC4mkIGTbiVdCeY=
github.com/fatih/color v1.1.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/mattn/go-colorable v0.0.7-0.20161102080925-6e26b354bd2b h1:QDkLN5n0HZJHlRE6QtBlrhuHgiQVYBuJ+R7gfyJBQ84=
github.com/mattn/go-colorable v0.0.7-0.20161102080925-6e26b354bd2b/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.0-20160806122752-66b8e73f3f5c h1:3nKFouDdpgGUV/uerJcYWH45ZbJzX0SiVWfTgmUeTzc=
github.com/mattn/go-isatty v0.0.0-20160806122752-66b8e73f3f5c/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/opencontainers/runc v1.0.0-rc2.0.20161102180326-49ed0a10e4ed h1:CO6K4Ex3bZFJrue+c19dYXE0ZCa9qoux6JSPMZKEeHw=
github.com/opencontainers/runc v1.0.0-rc2.0.20161102180326-49ed0a10e4ed/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stevvooe/resumable v0.0.0-20180830230917-22b14a53ba50 h1:4bT0pPowCpQImewr+BjzfUKcuFW+KVyB8d1OF3b6oTI=
github.com/stevvooe/resumable v0.0.0-20180830230917-22b14a53ba50/go.mod h1:1pdIZTAHUz+HDKDVZ++5xg/duPlhKAIzw9qy42CWYp4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/net v0.0.0-20160322021652-4876518f9e71 h1:uzpLVINUETeZRto6ZphAoh11snmPXlRiUF+rNKPHlO8=
golang.org/x/net v0.0.0-20160322021652-4876518f9e71/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20161023150541-c200b10b5d5e h1:uau++4dmjGy3TuvPvRbhZHY+TQVsImIxtWY5HRh9eho=
golang.org/x/sys v0.0.0-20161023150541-c200b10b5d5e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
EOF
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 {} \;

# Make sure go binary is in PATH
if [ -x /etc/profile.d/go.sh ]; then
  . /etc/profile.d/go.sh
fi

go mod download
make binary

mkdir -p $PKG/usr/bin $PKG/etc/rc.d $PKG/install
cp bin/local-persist $PKG/usr/bin
cp $CWD/rc.local-persist $PKG/etc/rc.d/rc.local-persist.new
chmod 775 $PKG/etc/rc.d/rc.local-persist.new
cat $CWD/doinst.sh > $PKG/install/doinst.sh

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

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  README.md \
  $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
