#!/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

# Note: ntopng requires the 'redis' pkg on FreeBSD, however the package name on OPNsense is 'redis72' (dependency of 'os-redis')
# for this reason this dependency should be installed manually for the time being
echo "deps: {" >> ${STAGEDIR}/+MANIFEST
for i in "json-c" "libmaxminddb" "libzmq4" "rrdtool" "lua53" "libsodium" "sqlite3" "hiredis" "librdkafka" "zstd" "nmap" "curl"
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;

# find ${STAGEDIR}/usr/local/share/ntopng/scripts/lua -name "*.lua" -type f -exec  ${NTOPNG_HOME}/tools/luac.sh {} ';'

# Vulscan
\mkdir -p ${STAGEDIR}/usr/local/share/nmap/scripts/vulscan
curl -o ${STAGEDIR}/usr/local/share/nmap/scripts/vulscan/vulscan.nse https://raw.githubusercontent.com/scipag/vulscan/master/vulscan.nse
curl -o ${STAGEDIR}/usr/local/share/nmap/scripts/vulscan/cve.csv     https://www.computec.ch/projekte/vulscan/download/cve.csv

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

# Add missing libraries symlinks due to invalid shlib version
RES=\`ldd /usr/local/bin/ntopng | grep "not found" | cut -d '=' -f 1 | sed 's/[[:blank:]]//g'\`

for LIB in \$RES; do
    BASELIB=\`echo \$LIB|cut -d '.' -f 1\`
    CMD=\`ln -s /usr/local/lib/\$BASELIB.so /usr/local/lib/\$LIB\`
done
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/
