#!/bin/sh

HERE=`pwd`
VERSION=`freebsd-version -u | cut -d '.' -f 1`

STAGEDIR=${HERE}/stage
NTOPNG_HOME=${HERE}/../..
rm -rf ${STAGEDIR}
mkdir -p ${STAGEDIR}

# Build +MANIFEST

cat >> ${STAGEDIR}/+MANIFEST <<EOF
name:        ntopng
version:     "@PACKAGE_VERSION@"
origin:      net/ntopng
comment:     "High speed network traffic monitor"
desc:        "Web-based network traffic monitoring tool"
maintainer:  packager@ntop.org
www:         https://www.ntop.org
prefix:      /
EOF

echo "deps: {" >> ${STAGEDIR}/+MANIFEST
for i in "json-c" "libmaxminddb" "libzmq4" "rrdtool" "lua53" "libsodium" "sqlite3" "redis" "librdkafka" "zstd"
do
    pkg query "  %n: { version: \"%v\", origin: %o }" "$i" >> ${STAGEDIR}/+MANIFEST
done
echo "}" >> ${STAGEDIR}/+MANIFEST

# Add files

cp -r usr ${STAGEDIR}
mkdir -p ${STAGEDIR}/usr/local/bin ${STAGEDIR}/usr/local/share/ntopng
cp ../../ntopng ${STAGEDIR}/usr/local/bin/
strip ${STAGEDIR}/usr/local/bin/ntopng
cp ../wizard/ntopng-config ${STAGEDIR}/usr/local/bin/
mkdir -p ${STAGEDIR}/usr/local/etc/ntopng

# Sample configuration file
CONFIGFILE=${STAGEDIR}/usr/local/etc/ntopng/ntopng.conf.sample
cp ../etc/ntopng/ntopng.conf ${CONFIGFILE}
# Add FreeBSD-specific defaults
echo "-t=/usr/local/share/ntopng/" >> ${CONFIGFILE}
echo "-d=/var/db/ntopng/" >> ${CONFIGFILE}

mkdir -p ${STAGEDIR}/usr/local/share/man/man8
cp ${NTOPNG_HOME}/ntopng.8 ${STAGEDIR}/usr/local/share/man/man8
cp -r ${NTOPNG_HOME}/httpdocs ${STAGEDIR}/usr/local/share/ntopng
rm -f ${STAGEDIR}/usr/local/share/ntopng/httpdocs/misc/ntopng-utils-manage*
cp -Lr ${NTOPNG_HOME}/scripts ${STAGEDIR}/usr/local/share/ntopng  # L to dereference symlinks
mv ${STAGEDIR}/usr/local/share/ntopng/httpdocs/ssl/ntopng-cert.pem.dummy ${STAGEDIR}/usr/local/share/ntopng/httpdocs/ssl/ntopng-cert.pem
mkdir ${STAGEDIR}/usr/local/share/ntopng/pro
cp -r ${NTOPNG_HOME}/pro/httpdocs ${STAGEDIR}/usr/local/share/ntopng/pro
cp -r ${NTOPNG_HOME}/pro/scripts ${STAGEDIR}/usr/local/share/ntopng/pro
rm -rf ${STAGEDIR}/usr/local/share/ntopng/pro/scripts/lua/nedge
cd ${STAGEDIR}/usr/local/share/ntopng/scripts/lua; rm -rf pro; cp -r ../../pro/scripts/lua pro
find ${STAGEDIR}/usr/local/share/ntopng/pro -name "*.lua" -type f -exec ${NTOPNG_HOME}/pro/utils/snzip -c -i {} -o {}r \;
find ${STAGEDIR}/usr/local/share/ntopng/pro -name "*.lua" -type f -exec /bin/rm  {} ';';
find ${STAGEDIR}/usr/local/share/ntopng/pro/scripts -name "*.luar" | xargs renamex -s/.luar/.lua/s;

rm -f ${STAGEDIR}/usr/local/share/ntopng/httpdocs/geoip/*
find ${STAGEDIR}/usr/local/share/ntopng -name "*~"   | xargs /bin/rm -f
find ${STAGEDIR}/usr/local/share/ntopng -name ".svn" | xargs /bin/rm -rf
find ${STAGEDIR}/usr/local/share/ntopng -name ".git*" | xargs /bin/rm -rf
find . -name "*#" -exec /bin/rm {} ';'
find . -name "*~" -exec /bin/rm {} ';'

# Add GeoIP files
mkdir -p ${STAGEDIR}/usr/local/share/ntopng/httpdocs/geoip
cp ~/dat_files/dbip-asn-lite-*.mmdb ${STAGEDIR}/usr/local/share/ntopng/httpdocs/geoip/dbip-asn-lite.mmdb
cp ~/dat_files/dbip-city-lite-*.mmdb ${STAGEDIR}/usr/local/share/ntopng/httpdocs/geoip/dbip-city-lite.mmdb
cp ~/dat_files/dbip-country-lite-*.mmdb ${STAGEDIR}/usr/local/share/ntopng/httpdocs/geoip/dbip-country-lite.mmdb

# Build plist (list of files to package)

cd $STAGEDIR
find . -type f | sed '$d' > ${STAGEDIR}/plist
cd $HERE

# Build +POST_INSTALL

cat >> ${STAGEDIR}/+POST_INSTALL <<EOF
if ! id "ntopng" >/dev/null 2>&1; then
  pw useradd ntopng -s /sbin/nologin -d /nonexistent -c "ntopng user" -w no
fi
chown ntopng:ntopng /usr/local/bin/ntopng-config
chmod ugo+s /usr/local/bin/ntopng-config
chown -R ntopng:ntopng /usr/local/etc/ntopng
EOF

# Build +POST_DEINSTALL

cat >> ${STAGEDIR}/+POST_DEINSTALL <<EOF
rm -rf /usr/local/share/ntopng
EOF


# Build package

rm -f *.txz
pkg create -m ${STAGEDIR}/ -r ${STAGEDIR}/ -p ${STAGEDIR}/plist -o $HERE

if test $VERSION -eq "12" ; then
    PKG_NAME=ntopng-@PACKAGE_VERSION@.txz
else
    PKG_NAME=ntopng-@PACKAGE_VERSION@.pkg
fi

/bin/rm -f ~/binary_packages/ntopng-*

tar -tvf ${PKG_NAME}
cp ${PKG_NAME} ~/binary_packages/
