#!/bin/sh
#
# Create the sed file for configuring everything
#

BAREOS_SOURCE=`cd .. && pwd`

cwd=`pwd`
. ${cwd}/scripts/functions
mkdir -p ${bin}
mkdir -p ${tmp}
out="${tmp}/sed_tmp"

# set ports and other default parameters
BASEPORT=${BASEPORT:-8101}
AUTOCHANGER_SCRIPT=${AUTOCHANGER_SCRIPT:-mtx-changer}
LIBDBI=${LIBDBI:-}
libdbi=${LIBDBI}
srcdir=${BAREOS_SOURCE}
dirport=${BASEPORT}
fdport=`expr ${BASEPORT} '+' 1`
sdport=`expr ${BASEPORT} '+' 2`
sdport2=`expr ${BASEPORT} '+' 3`
db_type=${DBTYPE:-"postgresql"}
db_name=${db_name:-"regress"}
db_user=${db_user:-"regress"}
db_password=${db_password:-""}
os=`uname -s`
# Drop Linux or FreeBSD from OS to shorten name
if test ${os} = "Linux" -o ${os} = "FreeBSD"; then
  os=`./scripts/get-os`
else
  os=${os}-`./scripts/get-os`
fi
if [ -r ${srcdir}/core/src/include/version.h ]; then
  bversion=`sed -n -e 's/^.*VERSION.*"\(.*\)"$/\1/p' ${srcdir}/core/src/include/version.h`
  bdate=`sed -n -e 's/^.*LSMDATE.*"\(.*\)"$/\1/p' ${srcdir}/core/src/include/version.h`
fi

if git --version >/dev/null; then
   cd  ${srcdir}
   gitbranch=`git rev-parse --abbrev-ref HEAD`
   gitrev=`git rev-parse --short HEAD`
   cd -
   regress_gitbranch=`git rev-parse --abbrev-ref HEAD`
   regress_gitrev=`git rev-parse --short HEAD`
fi

builddir="${src}"
hostname="${hostname:-$HOST}"

# get git info
regress_gitrev=""
bareos_gitrev=""

cd $cwd
regress_gitrev=`git show -s --format=%h HEAD`
cd -

cd ${BAREOS_SOURCE}
bareos_gitrev=`git show -s --format=%h HEAD`
cd -

GCOV=`which gcov`
PERL=`which perl`

. ./config

# Create sed command script
>${out}
echo "s%@bareos_gitrev@%${bareos_gitrev}%g" >>${out}
echo "s%@regress_gitrev@%${regress_gitrev}%g" >>${out}
echo "s%@archivedir@%${tmp}%g" >>${out}
echo "s%@bindir@%${bin}%g" >>${out}
echo "s%@sbindir@%${bin}%g" >>${out}
echo "s%@scriptdir@%${scripts}%g" >>${out}
echo "s%@scripts@%${scripts}%g" >>${out}
echo "s%@rscripts@%${rscripts}%g" >>${out}
echo "s%@plugindir@%${plugindir}%g" >>${out}
echo "s%@plugindirtmp@%${plugindirtmp}%g" >>${out}
echo "s%@working_dir@%${working}%g" >>${out}
echo "s%@piddir@%${working}%g" >>${out}
echo "s%@job_email@%${EMAIL}%g" >>${out}
echo "s%@tape_drive@%${TAPE_DRIVE}%g" >>${out}
echo "s%@autochanger@%${AUTOCHANGER}%g" >>${out}
echo "s%@changer_script@%${AUTOCHANGER_SCRIPT}%g" >>${out}
echo "s%@tmpdir@%${tmp}%g" >>${out}
echo "s%@logdir@%${tmp}%g" >>${out}
echo "s%@changer_path@%${AUTOCHANGER_PATH}%g" >>${out}
echo "s%@tape_drive1@%${TAPE_DRIVE1}%g" >>${out}
echo "s%@smtp_host@%${SMTP_HOST}%g" >>${out}
echo "s%@disk_drive@%${tmp}/disk-changer%g" >>${out}
echo "s%@basename@%${HOST}%g" >>${out}
echo "s%@hostname@%${hostname}%g" >>${out}
echo "s%@hostname1@%${hostname1}%g" >>${out}
echo "s%@hostname2@%${hostname2}%g" >>${out}
echo "s%@hostname3@%${hostname3}%g" >>${out}
echo "s%@hostname_files@%${hostname_files}%g" >>${out}
echo "s%@hostname1_files@%${hostname1_files}%g" >>${out}
echo "s%@hostname2_files@%${hostname2_files}%g" >>${out}
echo "s%@hostname3_files@%${hostname3_files}%g" >>${out}
echo "s%@hostname1_password@%${hostname1_password}%g" >>${out}
echo "s%@hostname2_password@%${hostname2_password}%g" >>${out}
echo "s%@hostname3_password@%${hostname3_password}%g" >>${out}
echo "s%@dirport@%${dirport}%g" >>${out}
echo "s%@dir_port@%${dirport}%g" >>${out}
echo "s%@sdport@%${sdport}%g" >>${out}
echo "s%@sdport2@%${sdport2}%g" >>${out}
echo "s%@fdport@%${fdport}%g" >>${out}
echo "s%@regressdir@%${cwd}%g" >>${out}
echo "s%@webuidir@%${BAREOS_SOURCE}/webui%g" >>${out}
echo "s%@srcdir@%${srcdir}%g" >>${out}
echo "s%@sitename@%${SITE_NAME}%g" >>${out}
echo "s%@os@%${os}%g" >>${out}
echo "s%@bversion@%${bversion}%g" >>${out}
echo "s%@bdate@%${bdate}%g" >>${out}
echo "s%@libdbi@%${libdbi}%g" >>${out}
echo "s%@win32_client@%${WIN32_CLIENT}%g" >>${out}
echo "s%@win32_addr@%${WIN32_ADDR}%g" >>${out}
echo "s%@win32_store_addr@%${WIN32_STORE_ADDR}%g" >>${out}
echo "s%@win32_file@%${WIN32_FILE}%g" >>${out}
echo "s%@win32_port@%${WIN32_PORT}%g" >>${out}
echo "s%@win32_password@%${WIN32_PASSWORD}%g" >>${out}
echo "s%@db_type@%${DBTYPE}%g" >>${out}
echo "s%@db_name@%${db_name}%g" >>${out}
echo "s%@db_user@%${db_user}%g" >>${out}
echo "s%@db_password@%${db_password}%g" >>${out}
echo "s%@builddir@%${builddir}%g" >>${out}
echo "s%@BUILD_DIR@%${builddir}%g" >>${out}
echo "s%@confdir@%${confdir}%g" >>${out}
echo "s%@uncomment_dbi@%#%g" >>${out}
echo "s%@gitbranch@%${gitbranch}%g" >>${out}
echo "s%@gitrev@%${gitrev}%g" >>${out}
echo "s%@regress_gitbranch@%${regress_gitbranch}%g" >>${out}
echo "s%@regress_gitrev@%${regress_gitrev}%g" >>${out}
echo "s%@GCOV@%${GCOV}%g" >>${out}
echo "s%@PERL@%${PERL}%g" >>${out}

if [ -n "${TRAYMON}" ] ; then
  echo "s%#ADD_TEST(disk:tray-monitor-connection-test-cleartext%ADD_TEST(disk:tray-monitor-connection-test-cleartext%g" >> ${out}
  echo "s%#ADD_TEST(disk:tray-monitor-connection-test-tls-psk%ADD_TEST(disk:tray-monitor-connection-test-tls-psk%g" >> ${out}
fi
