#!/bin/sh
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
#
# This script takes a file with placeholders in it, replaces the placeholders
# with values and writes the new content over the original file
# If a directory is given instead of a file then it will recursively replace
# the placeholders on the files in the directory.
# It also takes a file mode to set the permissions on the modified file

# fill_placeholders <file> <placeholders> <mode>
# file - The file or directory to replace the placeholders in
# placeholders - A spec for the placeholders, a newline separated list of
#                placeholder|value
#                e.g.
#                apacheport|80
#                ttalocation|/opt/tarantella
#                ttauser|ttaserv
# mode - The mode for the file permission to set any changed file

fill_placeholders(){
    templateFile="$1"
    placeholders="$2"
    mode="$3"
    tempFile="${templateFile}.processed"
    [ ! -f "${templateFile}" ] && { 
        echo "$0: ${templateFile} does not exist" 
                                    exit 
    }
    substitutions=`for p in $placeholders; do echo "s|%%${p}|g"; done`
    sed -e "$substitutions" "${templateFile}" > "${tempFile}"
    if [ $? -eq 0 ]
    then 
        cp "${tempFile}" "${templateFile}"
    else
        echo "Substitution failed: ${templateFile}"
    fi
    rm "${tempFile}" 

    if [ -n "${mode}" ]
    then
        chmod $mode "${templateFile}"
    fi
}

#
# Usage
#
usage(){
  echo ""
  echo "Usage: $0 SourceDir|SourceFile Placeholders [mode]"
}

#
# Exit codes
#
OK=0
FAIL=1

#
# Check we have the necessary arguments.
# 

if [ $# -lt 2 -o $# -gt 3 ]; then
  usage
  exit $FAIL
fi

# Supplied Parameters
SRC_FILE="$1"
PLACEHOLDERS="$2"
MODE="$3"

#
# Check the source tree exists
#

if [ ! -e "$SRC_FILE" ]
then
  echo "$SRC_FILE does not exist!"
  exit $FAIL
fi

DoIfEndIf() {
    awk '$1=="%%IfSGD" && $2=="Full" { state=5; next }
	 $1=="%%IfSGD" && $2=="Secure" { state=6; next }
	 $1=="%%IfSGD" { if (state==0) {state=4} else {++state}; next }
	 $1=="%%EndIfSGD" { if (state<=4) {state=0} else {state=4}; next }
	 substr($1,1,4)=="%%If" { ++state; next }
	 substr($1,1,7)=="%%EndIf" { if (state<=1) {state=0} else {--state}; next }
	 '"$2"' { print }' $1
}

DisableIndexes() {
  awk '$1=="<Directory" {
      state=1
    }
    $1=="</Directory>" {
      if (state==1) print "    Options Indexes"
      state=0
    }
    $1=="Options" && state>0 {
      if (state==1) state=2
      if ($2!="None") {
	printf "%s Indexes\n", $0; next
      }
    }
    {
      print
    }' $1 |
  sed '/^[ 	]*Options[ 	].*Indexes/s/[ 	]Indexes//'
}


PLACEHOLDER_NAMES=`echo "${PLACEHOLDERS}" | sed 's/|.*//g'`

if [ -d "${SRC_FILE}" ]
then 
    find "$SRC_FILE" -type f -print 
else
    echo "${SRC_FILE}"
fi |
xargs egrep -l "${PLACEHOLDER_NAMES}" |
while read f
do
    [ -n "`egrep '^%%If' ${f}`" ] && {
        # Process %%If/%%EndIf sections
        cp -p "${f}" "${f}.$$"
        # Include
        # Sections not in any %%If/%%EndIf (state 0)
        # Sections in %%IfSGD/%%EndIfSGD (state 4)
        # Sections in %%IfSGD Full/%%EndIfSGD (state 5)
        # Sections in %%IfSGD Secure/%%EndIfSGD (state 6)
        DoIfEndIf "${f}.$$" 'state==0 || state==4 || state==5 || state==6' | DisableIndexes >"${f}"
        rm -f "${f}.$$" 2>/dev/null
    }
    fill_placeholders "${f}" "${PLACEHOLDERS}" $MODE
done

exit $OK
