#!/bin/bash

#MBSRC=/appliance
MBDST=/cygdrive/c/WINDOWS/system32/inetsrv

. /var/run/applogic/appliance.sh

#cp $MBSRC/MetaBase.xml.template /tmp/MetaBase.xml >/dev/null 2>&1

if [ x"$_content_on_fs" = x"" -o x"$_content_on_fs" = x"off" ]; then
   if [ `ls /cygdrive | grep -E "^d$" | wc -l` -ne 1 ]; then
      # Error accessing 'content' volume
      vme id=alert msg="content_on_fs is 'off' but the local content volume is missing" severity=fatal
      test ! -d /cygdrive/d/$_docs_dir && vme id=alert msg="docs directory 'docs_dir' does not exist on content volume" severity=warn
   fi
   sed -i.bak 's|\tPath=\".*\"|\tPath=\"D:\\'$_docs_dir'\"|g' $MBDST/MetaBase.xml
else
   ping -n 1 -w 2000 fs >/dev/null 2>&1
   if [ $? -ne 0 ]; then
      # Error accessing NAS
      vme id=alert msg="content_on_fs is 'on' but the fs terminal is not connected" severity=fatal
      echo
   fi
   sed -i.bak 's|\tPath=\".*\"|\tPath=\"\\\\fs\\share\\'$_docs_dir'\"|g' $MBDST/MetaBase.xml
fi

if [ x"$_index_files" != x"" ]; then
    rs=`echo $_index_files | sed 's/ /,/g'`
    sed -i.bak 's|\tDefaultDoc=\".*\"|\tDefaultDoc=\"'$rs'\"|g' $MBDST/MetaBase.xml
else
    sed -i.bak 's|\tDefaultDoc=\".*\"|\tDefaultDoc=\"default.htm,default.asp,default.aspx,index.html\"|g' $MBDST/MetaBase.xml
fi

if [ x"$_logs_enabled" = x"" -o x"$_logs_enabled" = x"off" ]; then
   sed -i.bak 's|LogFileDirectory=\".*\"|LogFileDirectory=\"C:\\WINDOWS\\system32\\LogFiles\\W3SVC1\"|g' $MBDST/MetaBase.xml
else
   ping -n 1 -w 2000 log >/dev/null 2>&1
   if [ $? -ne 0 ]; then
      # Error accessing NAS
      vme id=alert msg="Logs are enabled but the 'log' terminal is not connected" severity=fatal
   fi
   sed -i.bak 's|LogFileDirectory=\".*\"|LogFileDirectory=\"\\\\log\\share\\'$_logs_base_dir'\"|g' $MBDST/MetaBase.xml
fi
#sed -i.bak 's|#LOGTYPE#|1|' /tmp/MetaBase.xml


if [ x"$_idle_timeout_sec" = x"" ]; then
     _idle_timeout_sec=15
fi
sed -i.bak 's|ConnectionTimeout=\".*\"|ConnectionTimeout=\"'$_idle_timeout_sec'\"|g' $MBDST/MetaBase.xml
sed -i.bak 's|IdleTimeout=\".*\"|ConnectionTimeout=\"'$_idle_timeout_sec'\"|g' $MBDST/MetaBase.xml

if [ x"$_data_timeout_sec" = x"" ]; then
     _data_timeout_sec=300
fi
sed -i.bak 's|AspScriptTimeout=\".*\"|AspScriptTimeout=\"'$_data_timeout_sec'\"|g' $MBDST/MetaBase.xml
sed -i.bak 's|CGITimeout=\".*\"|CGITimeout=\"'$_data_timeout_sec'\"|g' $MBDST/MetaBase.xml

memtotal=`systeminfo| grep Total| awk '{print $4}'| sed 's/,//g'` 
freemem=`expr $memtotal - 128` 
fm_bytes=`expr $freemem '*' 1024` 
iisconn=`expr $fm_bytes / 2560`

test -z $_max_connections && _max_connections=9999
test $_max_connections -gt $iisconn && _max_connections=$iisconn

if [ x"`grep MaxConn $MBDST/MetaBase.xml`" = x"" ]; then
   sed -i.bak '/ServerAutoStart=\"TRUE\"/aMaxConnections=\"'$_max_connections'\"' $MBDST/MetaBase.xml
else
   sed -i.bak 's|MaxConnections=\".*\"|MaxConnections=\"'$_max_connections'\"|g' $MBDST/MetaBase.xml
fi

#unix2dos < /tmp/MetaBase.xml > $MBDST/MetaBase.xml 2>/dev/null
#rm -f /tmp/MetaBase* >/dev/null 2>&1

