#! /bin/bash
# Password Safe build script 
#--
# Last edited: 03.05.2019
#--

if [ $UID = 0 ]; then
	echo "[1mThere is no need to run this build script as root.[0m You'll be"
	echo "asked for your root password at package creation time (if you"
	echo "haven't installed fakeroot)."
	echo -n "Type \"take risk\" to continue: "
	read RISK
	test "$RISK" = "take risk" || exit 1
fi

PRGNAM=passwordsafe

# exit after errors
set -e
trap 'echo "$0 FAILED at line ${LINENO}" | tee "${LOGS}/error-${PRGNAM}.log"' ERR
# Catch uninitialized variables
set -u
# catch non-zero status in pipes
set -o pipefail
P1=${1:-1}

GIT_ROOT=`git rev-parse --show-toplevel 2>/dev/null || readlink -e ../..`
sed 's/\s*=\s*/=/g' < "${GIT_ROOT}/version.wx" > "${GIT_ROOT}/version.wx.tmp"
. "${GIT_ROOT}/version.wx.tmp"
rm "${GIT_ROOT}/version.wx.tmp"

VERSION=${VERSION:-${VER_MAJOR}.${VER_MINOR}.${VER_REV}${VER_SPECIAL:-}}
ARCH=${ARCH:-x86_64} # i[345]6 or x86_64
BUILD_ID=${BUILD_ID:-1}
BUILDER_ID=${BUILDER_ID:-kan}
OUTPUT=${OUTPUT:-${PWD}/packages}
LOGS=${LOGS:-${PWD}/logs}
MAKE_OPTIONS="-j5"

##
DOCSRCDIR=${GIT_ROOT}/docs
SRCDIR=${GIT_ROOT}/src
MAKEDIR=${GIT_ROOT}/install/slackware/build
##
PKGNAME=${PRGNAM}-${VERSION/-/.}-${ARCH}-${BUILD_ID}${BUILDER_ID}
##
PREFIX="/usr"
DOCDIR=${PREFIX}/doc/${PRGNAM}-${VERSION}
MANDIR=${PREFIX}/man
HELPDIR=${PREFIX}/share/${PRGNAM}/help
XMLDIR=${PREFIX}/share/${PRGNAM}/xml
DEFDOCDIR=${PREFIX}/share/doc/passwordsafe
DEFMANDIR=${PREFIX}/share/man
##
DATE=$(LC_ALL=C /bin/date +%d-%b-%Y)
PKG=${PWD}/package-${PRGNAM}
# $PKG_TMP must be subdir of $PKG, we use it later
PKG_TMP=${PKG}/tmp
MYDIR=${PWD}

FLAGS="-DCMAKE_INSTALL_PREFIX=${PREFIX} \
-DCMAKE_BUILD_TYPE=Release"

NO_YUBI=0
NO_QR=0
NO_GTEST=1

# process parameters
while [[ $# -gt 0 ]]; do
    case "$1" in
        --help|-h)
            echo -e "Usage: $0 [--no-yubi] [--no-qr] [--gtest]\n\tno-yubi -- disable Yuibikey support\n\tno-qr -- disable QR-code support\n\tgtest -- enable gtest" >&2
            exit 1
            ;;
        --no-yubi)
            NO_YUBI=1
            ;;
        --no-qr)
            NO_QR=1
            ;;
        --gtest)
            NO_GTEST=0
            ;;
        *)
            echo "Unknown flag $1" >&2
            exit 1
    esac
    shift
done

if [ "${NO_YUBI}" == "1" ]; then
    VERSION+="_noyubi"
    FLAGS+="${FLAGS} -DNO_YUBI=ON"
fi
if [ "${NO_QR}" == "1" ]; then
    VERSION+="_noqr"
    FLAGS+="${FLAGS} -DNO_QR=ON"
fi
if [ "${NO_GTEST}" == "1" ]; then
    FLAGS+="${FLAGS} -DNO_GTEST=ON"
fi


PKGNAME=${PRGNAM}-${VERSION/-/.}-${ARCH}-${BUILD_ID}${BUILDER_ID}

umask 022

# Create working directories:
mkdir -p "${LOGS}"
mkdir -p "${OUTPUT}"
mkdir -p "${PKG_TMP}"
mkdir -p "${MAKEDIR}"
# always erase old package's contents
rm -rf "${PKG}"/* "${MAKEDIR}"/*
# update version
[ -f ${GIT_ROOT}/src/ui/wxWidgets/version.h ] && rm ${GIT_ROOT}/src/ui/wxWidgets/version.h
${GIT_ROOT}/Misc/mkversion.pl MAJOR=${VER_MAJOR} MINOR=${VER_MINOR} REV=${VER_REV} SPECIAL=${VER_SPECIAL:-""} "${GIT_ROOT}/src/ui/wxWidgets/version.in" "${GIT_ROOT}/src/ui/wxWidgets/version.h"

## Package building
echo Building ...
cd ${MAKEDIR}
cmake ${FLAGS} ${GIT_ROOT} 2>&1 | tee "${LOGS}/cmake-${PRGNAM}.log" || exit 1

echo "- make"
make ${MAKE_OPTIONS} 2>&1 | tee "${LOGS}/make-${PRGNAM}.log" || exit 1

echo "- prepare package"
DESTDIR="${PKG_TMP}" make install 2>&1 | tee "${LOGS}/install-${PRGNAM}.log" || exit 1

# collect files before packaging
# copy docs
mkdir -p "${PKG_TMP}${DOCDIR}"
cp "${GIT_ROOT}"/README* "${GIT_ROOT}"/LICENSE "${GIT_ROOT}"/install/copyright "${PKG_TMP}${DOCDIR}"
if [ -d "${PKG_TMP}${DEFDOCDIR}" ]; then
    mv "${PKG_TMP}${DEFDOCDIR}"/* "${PKG_TMP}${DOCDIR}" || true
    rmdir "${PKG_TMP}${DEFDOCDIR}"
fi

# move man to slackware real dir (${PREFIX}/share/man is symlink to ${PREFIX}/man)
mkdir -p "${PKG_TMP}${MANDIR}"
if [ -d "${PKG_TMP}${DEFMANDIR}" ]; then
    mv "${PKG_TMP}${DEFMANDIR}"/* "${PKG_TMP}${MANDIR}" || true
    rmdir "${PKG_TMP}${DEFMANDIR}"
fi

#copy buildscript
mkdir -p "${PKG_TMP}/usr/src/slackbuild"
cp -a "${MYDIR}/${PRGNAM}.SlackBuild" "${PKG_TMP}/usr/src/slackbuild/"

cd "${PKG_TMP}"
# remove debugging symbols
find . -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF | cut -f 1 -d : | xargs -0 strip --strip-unneeded 2> /dev/null || true

# fix permissions
find . -type f -print0 | xargs -0 --no-run-if-empty chmod 644
chmod 755 "${PKG_TMP}${PREFIX}/bin/"*

mkdir "${PKG_TMP}/install"

# make slack-desc package description file
cp "${MYDIR}/slack-desc" "${PKG_TMP}/install/slack-desc"
echo "${PRGNAM}: build at ${DATE}" >> "${PKG_TMP}/install/slack-desc"

set +o xtrace

ROOTCOMMANDS="set -o errexit -o xtrace -o pipefail ; cd \"${PKG_TMP}\";
    chown -R root:root . ;"
ROOTCOMMANDS="${ROOTCOMMANDS}
    if [ -x /usr/bin/requiredbuilder ]; then /usr/bin/requiredbuilder -v -b -p -y .; fi ;
    /sbin/makepkg --linkadd y --chown n ../${PKGNAME}.txz 2>&1 | tee \"${LOGS}/makepkg-${PRGNAM}.log\" && \
    mv ../${PKGNAME}.txz \"${OUTPUT}\" && cd \"${OUTPUT}\" && \
    md5sum ${PKGNAME}.txz > ${PKGNAME}.txz.md5 && \
    rm -rf \"${PKG}\""

if [ ${UID} = 0 ]; then
    eval ${ROOTCOMMANDS}
    set +o xtrace
elif [ -x /usr/bin/fakeroot ]; then
    echo "[1mEntering fakeroot environment.[0m"
    echo -n "${ROOTCOMMANDS}" | /usr/bin/fakeroot
else
    echo "[1mPlease enter your root password.[0m"
    /bin/su -c "${ROOTCOMMANDS}"
fi

echo "Package is ready in ${OUTPUT}/${PKGNAME}.txz"
#####
