#!/bin/bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
source "$(dirname "$0")/../load.sh" || exit 1

function parse_runphp() {
    local runphp="$1" preamble="$2" conf="$3" postamble="$4"
    local conf0
    ac_set_tmpfile conf0
    # extraire la configuration depuis le fichier
    <"$runphp" awk -v preamble="$preamble" -v conf="$conf0" -v postamble="$postamble" '
BEGIN { out = preamble }
{
  if ($0 ~ /SOF:runphp.userconf:/) {
    if (out != "") print >out
    out = conf
  } else if ($0 ~ /EOF:runphp.userconf:/) {
    out = postamble
    if (out != "") print >out
  } else {
    if (out != "") print >out
  }
}
'
    # mettre en forme le fichier conf: pas de lignes vides avant et après
    <"$conf0" >"$conf" awk '
BEGIN { p = 0; have_pending = 0; pending = "" }
$0 != "" { p = 1 }
p == 1 {
  if ($0 != "") {
    if (have_pending) print pending
    print
    have_pending = 0
    pending = ""
  } else {
    if (!have_pending) have_pending = 1
    else pending = pending "\n"
  }
}
'
    ac_clean "$conf0"
}

projdir=
args=(
    "Mettre à jour le script runphp"
    "[path/to/runphp]"
    -d:,--projdir:PROJDIR . "Copier les fichiers pour un projet de l'université de la Réunion"
)
parse_args "$@"; set -- "${args[@]}"

if [ -n "$projdir" ]; then
    setx projdir=abspath "$projdir"
    set -- "$projdir/sbin/runphp"
fi

runphp="${1:-.}"
if [ -d "$runphp" ]; then
    runphp="$runphp/runphp"
elif [ ! -e "$runphp" ]; then
    [ "${runphp%/runphp}" != "$runphp" ] || runphp="$runphp/runphp"
fi

setx runphp=abspath "$runphp"
setx rundir=dirname -- "$runphp"
[ "$rundir" == "$MYDIR" ] && exit 0
[ -d "$rundir" ] || mkdir -p "$rundir"

# Tout d'abord, isoler chaque partie du fichier local
ac_set_tmpfile preamble
ac_set_tmpfile conf
ac_set_tmpfile postamble
parse_runphp "$MYDIR/runphp" "$preamble" "$conf" "$postamble"

# Puis analyser le fichier destination
if [ -f "$runphp" ]; then
    ac_set_tmpfile userconf
    parse_runphp "$runphp" "" "$userconf" ""
    initial_config=

elif [ -n "$projdir" ]; then
    # forcer BUILDENV0=..env.dist et BUILDENV=.env pour les projets de
    # l'université de la Réunion
    ac_set_tmpfile userconf
    sed <"$conf" >"$userconf" '
/^BUILDENV0=/s/=.*/=..env.dist/
/^BUILDENV=/s/=.*/=.env/
'
    initial_config=1

else
    userconf="$conf"
    initial_config=1
fi

# (Re)construire le fichier destination
(
    cat "$preamble"
    echo
    cat "$userconf"
    echo
    cat "$postamble"
) >"$runphp"
[ -x "$runphp" ] || chmod +x "$runphp"

rsync -lpt "$MYDIR/Dockerfile.runphp" "$rundir/"

if [ -n "$projdir" ]; then
    if testdiff "$rundir/build" "$MYDIR/build"; then
        cp "$MYDIR/build" "$rundir/build"
        chmod +x "$rundir/build"
    fi
    if [ ! -f "$projdir/..env.dist" ]; then
        sed <"$MYDIR/dot-build.env.dist" >"$projdir/..env.dist" '
/^IMAGENAME=/s/=.*\//='"$(basename -- "$projdir")"'\//
'
        initial_config=1
    fi
    if [ ! -f "$projdir/.runphp.conf" ]; then
        sed <"$MYDIR/dot-runphp.conf" >"$projdir/.runphp.conf" '
/^RUNPHP=/s/=.*/=sbin\/runphp/
'
    fi
fi

[ -n "$initial_config" ]