#! /bin/sh
# Edit the following lines according to your installation of Bossa
# If LINUX_VERSION is debian, then the module information will be installed
# using update-modules.  If it is anything else, the module information will
# be written to /etc/modules.conf directly
#BOSSA=2.4.18-bossa
#BOSSA_HOME=/root/bossa2312
BOSSA=2.4.24-bossa
BOSSA_HOME=/root/bossa_linux2424
HR_BOSSA=2.4.18-fullbossa_hrt
HR_BOSSA_HOME=/home/jll/fullbossa2418_hrtime
COMPILER_HOME=/root/compiler
LINUX_VERSION=debian

BOSSA_VERSION=`uname -r`

if ( test $BOSSA_VERSION = $HR_BOSSA ) ;
then BOSSA_OPTS=-hrt;
     BOSSA_HOME=$HR_BOSSA_HOME;
     FILENAME=$1;
else BOSSA_OPTS=;
     FILENAME=$1;
fi

echo Compiling the bossa file $BOSSA_OPTS

for FILENAME ;
do
if ! (test -z $2) ;
then
  echo
  echo $FILENAME ;
fi

if ! ( test -e c-code/${FILENAME%.bossa}.c &&
       test c-code/${FILENAME%.bossa}.c -nt ${FILENAME%.bossa}.bossa &&
       ( ( ! ( test -e $COMPILER_HOME/cb.opt ) ) ||
         test c-code/${FILENAME%.bossa}.c -nt $COMPILER_HOME/cb.opt ) &&
       ( ( ! ( test -e $COMPILER_HOME/cb ) ) ||
         test c-code/${FILENAME%.bossa}.c -nt $COMPILER_HOME/cb ) &&
       ( ! ( test "$BOSSA_OPTS" = "-hrt" ) ||
	 grep --quiet hrtime.h c-code/${FILENAME%.bossa}.c ) &&
       ( test "$BOSSA_OPTS" = "-hrt" ||
	 ( ! (grep --quiet hrtime.h c-code/${FILENAME%.bossa}.c ) ) )
 ) ;
then if test -e $COMPILER_HOME/cb.opt && test -e $COMPILER_HOME/cb;
     then if test $COMPILER_HOME/cb.opt -nt $COMPILER_HOME/cb;
          then $COMPILER_HOME/cb.opt $BOSSA_OPTS ${FILENAME%.bossa}.bossa || exit 0;
          else $COMPILER_HOME/cb $BOSSA_OPTS ${FILENAME%.bossa}.bossa || exit 0;
	  fi;
     else if test -e $COMPILER_HOME/cb.opt;
          then $COMPILER_HOME/cb.opt $BOSSA_OPTS ${FILENAME%.bossa}.bossa || exit 0;
	  else $COMPILER_HOME/cb $BOSSA_OPTS ${FILENAME%.bossa}.bossa || exit 0;
	  fi;
     fi
fi

cd c-code

echo Checking for compiled file
test ${FILENAME%.bossa}.c -nt ../${FILENAME%.bossa}.bossa || \
  (echo Policy must have the same name as the .bossa file && exit 1) || exit 0

if ( test $LINUX_VERSION = "debian" ) ;
then
grep --quiet bossa_${FILENAME%.bossa} /etc/modutils/bossa || \
(echo Initializing /etc/modutils/bossa && \
echo alias /dev/bossa/bossa_${FILENAME%.bossa} ${FILENAME%.bossa} >> /etc/modutils/bossa && \
update-modules) || exit 0 ;
else
grep --quiet bossa_${FILENAME%.bossa} /etc/modules.conf || \
(echo Initializing /etc/modules.conf && \
echo alias /dev/bossa/bossa_${FILENAME%.bossa} ${FILENAME%.bossa} >> /etc/modules.conf) || exit 0 ;
fi

echo Compiling the module
gcc -D__KERNEL__ -I$BOSSA_HOME/include -I$BOSSA_HOME/kernel -Wall \
-Wstrict-prototypes \
-Wno-trigraphs -O2 -fomit-frame-pointer -fno-strict-aliasing -fno-common \
-pipe -mpreferred-stack-boundary=2 -march=i686 -DMODULE -DMODVERSIONS \
-include $BOSSA_HOME/include/linux/modversions.h \
-DKBUILD_BASENAME=${FILENAME%.bossa} -DEXPORT_SYMTAB -c ${FILENAME%.bossa}.c || exit 0

echo Installing the module
echo /lib/modules/$BOSSA_VERSION/kernel/kernel
if ! ( test -e /lib/modules/$BOSSA_VERSION )
then mkdir /lib/modules/$BOSSA_VERSION
fi
if ! ( test -e /lib/modules/$BOSSA_VERSION/kernel )
then mkdir /lib/modules/$BOSSA_VERSION/kernel
fi
if ! ( test -e /lib/modules/$BOSSA_VERSION/kernel/kernel )
then mkdir /lib/modules/$BOSSA_VERSION/kernel/kernel
fi
cp ${FILENAME%.bossa}.o /lib/modules/$BOSSA_VERSION/kernel/kernel/ || exit 0

(if lsmod | grep --quiet "^${FILENAME%.bossa} "; then rmmod ${FILENAME%.bossa}; fi; \
(insmod ${FILENAME%.bossa} || \
echo A policy must be removed from the hierarchy before installing a new version))

cd ..

done
