#!/bin/sh

echo ">>> univnautes virtualenv postinst : starting..."

# run this script just after installing the virtualenv directory on the
# future ISO image
# /!\ python must be installed

if [ ! -r /usr/local/univnautes/bin/postinst ]
then
	echo "error: $0 must be run on a clean (virgin) pfsense+univnautes install" 1>&2
	exit 1
fi

# install rc.d scripts
echo ">>> Install rc.d scripts"
mkdir -p /usr/local/etc/rc.d
cd /usr/local/etc/rc.d
ln -sfv ../../univnautes/rc.d/* .

# install some scripts/helpers
echo ">>> Install tools in /usr/local/bin"
mkdir -p /usr/local/bin
cd /usr/local/bin
for b in cp_* univnautes-* pf_*
do
	ln -svf ../univnautes/bin/$b .
done

# install pfSense package
echo ">>> Install pfSense univnautes package"
mkdir -p /usr/local/pkg
cd /usr/local/pkg
ln -sfv ../univnautes/pkg/univnautes* .
echo ">>> Install pfSense univnautes package privileges"
mkdir -p /usr/local/pkg/priv
cd /usr/local/pkg/priv
ln -sfv ../../univnautes/pkg/priv/* .

# copy python binaries in the virtualenv
echo ">>> Copy python binaries in /usr/local/univnautes/bin (virtualenv)"
cd /usr/local/univnautes/bin
rm -f python python2.7
cp -av /usr/local/bin/python2.7 .
ln python2.7 python

# patch captiveportal & others
echo ">>> apply UnivNautes patches"
cd /usr/src/univnautes-patches
pwd
for f in `grep -v "^#" list`
do
	echo patching $f
	patch $f `basename $f`.diff
	echo delete $f.orig
	rm -f $f.orig
	echo ""
done

# cleaning (if there is too much files on ISO, the installer crash)
echo ">>> more cleaning ..."
for d in /usr/local/man /usr/local/share/examples /usr/local/share/doc /usr/local/lib/perl5/*/perl/man
do
       echo -n "    remove $d ..."
       rm -rf $d
       echo " ok"
done
echo "    remove .pyc files"
find /usr/local/ -name "*.pyc" -exec rm {} \;
echo "    remove .pyo files"
find /usr/local/ -name "*.pyo" -exec rm {} \;



# rename myself... (never execute again...)
cd /usr/local/univnautes/bin
mv postinst postinst.done

echo ">>> univnautes virtualenv postinst : The End !"
