#!/bin/bash
ABSEC_AUTHEN_DEP="-lpam -lpam_misc "
ABSEC_AUTHEN_FILE="absec_pam.o"
#could be absec_pam or absec_etc
ABSEC_AUTO_DEP="`mysql_config --cflags --libs` "
ABSEC_AUTO_FILE="absec_mysql.o"
#could be absec_fs or absec_mysql

echo "----------------"
echo "Cleaning projets"
rm -fv *.o *.lo *.la *.slo

echo "================"
echo "Compiling pam module (absec_pam)"
gcc absec_pam.c -lpam -lpam_misc -fPIC -c

echo "----------------"
echo "Compiling authen test with pam (test_authen -labsec_pam)"
gcc test_authen.c absec_pam.o -lpam -lpam_misc -o test_authen
echo "Running test"
./test_authen

echo "================"
echo "Compiling etc module (absec_etc)"
gcc -I /usr/local/apache2/include -I /usr/include/apr-1.0 -fPIC absec_etc.c -c


echo "----------------"
echo "Compiling authen test with etc (test_authen -labsec_etc)"
gcc test_authen.c absec_etc.o -L/usr/lib/x86_64-linux-gnu -lapr-1 -lcrypt -o test_authen
echo "Running test"
./test_authen


echo "================"
echo "Compiling mysql module"
gcc absec_mysql.c `mysql_config --cflags --libs` -I/usr/include/apache2 -I/usr/include/apr-1.0 -fPIC -c

echo "----------------"
echo "Compiling auto test with mysql (test_auto -labsec_mysql)"
gcc test_auto.c absec_mysql.o `mysql_config --cflags --libs` -I/usr/include/apache2 -I/usr/include/apr-1.0 -o test_auto
echo "Running test"
./test_auto

echo "================"
echo "Compiling filesystem module"
gcc absec_fs.c -I/usr/include/apache2 -I/usr/include/apr-1.0 -fPIC -c

echo "----------------"
echo "Compiling auto test with filesystem (test_auto -labsec_fs)"
gcc test_auto.c absec_fs.o -I/usr/include/apache2 -I/usr/include/apr-1.0 -o test_auto
echo "Running test"
./test_auto

echo "================"
echo "Compiling absec tools"
echo "- absec_ls"
gcc absec_ls.c `mysql_config --cflags --libs` -o abls
echo "Running test"
./abls
./abls /

echo "================"
echo "Compiling absec tools"
echo "- absec_chmod"
gcc absec_chmod.c `mysql_config --cflags --libs` -o abchmod
echo "Running test"
./abchmod 0 /
./abls /
./abchmod 666 /
./abls /

echo "================"
echo "Compiling absec tools"
echo "- absec_chown"
gcc absec_chown.c `mysql_config --cflags --libs` -o abchown
echo "Running test"
./abchown 100 /
./abls /
./abchown 200 /
./abls /

echo "================"
echo "Compiling absec tools"
echo "- absec_chgrp"
gcc absec_chgrp.c `mysql_config --cflags --libs` -o abchgrp
echo "Running test"
./abchgrp 100 /
./abls /
./abchgrp 200 /
./abls /

echo "================"
echo "Compiling apache absec module"
echo "selected authen :" $ABSEC_AUTHEN_DEP $ABSEC_AUTHEN_FILE
echo "selected auto :" $ABSEC_AUTO_DEP $ABSEC_AUTO_FILE
sudo apxs $ABSEC_AUTHEN_DEP $ABSEC_AUTO_DEP -c -i $ABSEC_AUTHEN_FILE $ABSEC_AUTO_FILE mod_absec.c
#/home/jlcyr/apache2/bin/apachectl restart
echo "Restarting apache"
sudo /etc/init.d/apache2 restart
