#!/bin/bash

if [ "`whoami`" != 'root' ]
then
    echo You need to run this script as user "root"
    exit 1
fi

PATH=/usr/sbin:$PATH

export ENSTORE_DIR=`rpm -ql enstore | head -1`
export PYTHON_DIR=$ENSTORE_DIR/Python
export PYTHONINC=`ls -d ${PYTHON_DIR}/include/python*`
export PYTHONLIB=`ls -d ${PYTHON_DIR}/lib/python*`
export PATH=${PYTHON_DIR}/bin:${PATH}
set PYTHONPATH="./"
for p in ${ENSTORE_DIR}/src \
	 ${ENSTORE_DIR}/modules \
	 ${ENSTORE_DIR}/HTMLgen 	
do	
	PYTHONPATH=${PYTHONPATH}:${p}
done

export PYTHONPATH

chmod +x web_server.py 
chmod +x make_enstore_system.py 

./web_server.py --install

cp -f /etc/init.d/httpd /etc/init.d/httpd.orig 

sed -i "/\/etc\/rc.d\/init.d\/functions/a if \[ -f \/usr\/local\/etc\/setups.sh \]; then \n # this check is required by current rpm build process, do not remove!!!! \n . /usr/local/etc/setups.sh \n setup enstore\nfi" /etc/init.d/httpd

if [ $? != 0 ];
then
	exit 1 
fi



./make_enstore_system.py 

rc=$?

if [ ${rc} == 2 ]; then
    /sbin/chkconfig httpd off
    exit 0
elif  [ ${rc} == 0 ]; then
    /sbin/chkconfig httpd on
    /sbin/service httpd start 
    exit 0
else 
    exit 1
fi


exit 0 
