#!/usr/local/bin/bash

LOCAL=/usr/local
VE=univnautes

BASE_DIR=/home/pfsense
BUILDER_TOOLS=$BASE_DIR/tools
BUILDER_SCRIPTS=$BUILDER_TOOLS/builder_scripts
BUILDER_PROFILES=$BUILDER_SCRIPTS/builder_profiles
UNIVNAUTES=$BUILDER_PROFILES/univnautes

echo "** building virtualenv in $LOCAL/$VE"

if [ -e $LOCAL/$VE ]
then
	echo "$0: $LOCAL/$VE already exists... using it..." 1>&2
	exit 0
fi

cd $LOCAL
virtualenv $VE

echo "** activate virtualenv"
cd $VE
. bin/activate

pip install pip==1.4.1

echo "** install packages"
for p in flup http://isos.univnautes.entrouvert.com/authentic2/authentic2-2.1.1.12.ge5caca7.tar.gz
do
	pip install "$p"
done

for f in pffedportal pfidp bin etc rc.d pkg www
do
	echo "** create `pwd`/`basename $f`"
	cp -a $UNIVNAUTES/virtualenv/$f .
done

echo "** populate www directory"
touch www/index.html
ln -s ../pffedportal/media www/
ln -s /var/db/cpelements www/media/local

echo "** cleaning..."
find . -name "*.pyc" -exec rm {} \;
find . -name "*.pyo" -exec rm {} \;
cd $LOCAL
virtualenv --relocatable $VE

echo "*** manually add lasso and sqlite3 modules"
cd $VE
cp -av /usr/local/lib/python2.7/site-packages/*lasso* ./lib/python2.7/site-packages/
cp -av /usr/local/lib/python2.7/sqlite3 ./lib/python2.7/

echo "** /usr/local/univnautes is ready"
echo "   bye bye"
