#!/bin/sh

# This script is executed by the rpmbuild process to fix the perl_lib path and
# create the apache conf.d file

ROOT=$1

CFG_APACHE_CONF=<<EOF
#
# apache.conf include file for EPrints
#
# If this file exists then it will not be over written by
#
# Put your own extra directives for this site here
#
# Comment out the "Include" lines if you don't want to use the
# autogenerated config for this repository.
#

# Include list of 'Include's for each repository
Include /opt/eprints3/var/auto-apache-includes.conf

# Include autogenerate apache.conf elements
Include /opt/eprints3/var/auto-apache.conf
EOF

VAR_AUTO_APACHE_CONF=<<EOF
#
# auto-apache.conf include file for EPrints
#
# DO NOT EDIT, this file is created by bin/generate_apacheconf
# and may be overwritten. To modify, change the repository configuration
# and re-run: bin/generate_apacheconf
#

# Load the perl modules & repository configurations
PerlSetEnv EPRINTS_APACHE 2
PerlRequire /opt/eprints3/bin/startup.pl

NameVirtualHost *:80

# Makes the request object accessable for other objects
# (apache 2.0 only)
PerlOptions +GlobalRequest
EOF
VAR_AUTO_APACHE_INCLUDES_CONF=<<EOF
#
# auto-apache-includes.conf include file for EPrints
#
# DO NOT EDIT, this file is created by bin/generate_apacheconf
# and may be overwritten. To modify, change the repository configuration
# and re-run: bin/generate_apacheconf
#
# List of files to include for repository specific config...

EOF

pushd $ROOT

# Include eprints apache conf using a conf.d file
mkdir -p ${ROOT}/etc/httpd/conf.d
echo "Include /opt/eprints3/cfg/apache.conf" > ${ROOT}/etc/httpd/conf.d/eprints3.conf

# Fix the perl path in the script files
pushd ${ROOT}/opt/eprints3/bin
for i in `ls`; do
	echo "Fixing Perl include in: $i"
	sed "s|${ROOT}||" < $i > "${i}.new"
	mv "${i}.new" $i
	chmod 755 $i
done
popd

# Fix the base_path in SystemSettings
pushd ${ROOT}/opt/eprints3/perl_lib/EPrints
sed "s|${ROOT}||" < 'SystemSettings.pm' > "SystemSettings.pm.new"
mv "SystemSettings.pm.new" SystemSettings.pm
popd

pushd ${ROOT}/opt/eprints3
mkdir cfg
pushd cfg
echo $CFG_APACHE_CONF > apache.conf
popd
pushd var
echo $VAR_AUTO_APACHE_CONF > auto-apache.conf
echo $VAR_AUTO_APACHE_INCLUDES_CONF > auto-apache-includes.conf
touch indexer.log
perl -e "for(1..5) { system('touch','indexer.log.'.\$_) }";
popd
popd

popd
