#!/bin/bash

# En cas d'absence de postfix, il faut lancer :
# docker network create postfix_mailNet

# démare/arrête un compose
# sauvegarde la base de données d'un compose
# met à jours les paramètres de configuration du mandataire (proxy)

KAZ_ROOT=$(cd "$(dirname $0)/.."; pwd)
. "${KAZ_ROOT}/bin/.commonFunctions.sh"
setKazVars

cd "${KAZ_BIN_DIR}"
PATH_SAUVE="/home/sauve/"
export SIMU=""

declare -a availableComposesNoNeedMail availableMailComposes availableComposesNeedMail availableProxyComposes availableOrga
availableComposesNoNeedMail=($(getList "${KAZ_CONF_DIR}/container-withoutMail.list"))
availableMailComposes=($(getList "${KAZ_CONF_DIR}/container-mail.list"))
availableComposesNeedMail=($(getList "${KAZ_CONF_DIR}/container-withMail.list"))
availableProxyComposes=($(getList "${KAZ_CONF_DIR}/container-proxy.list"))
availableOrga=($(getList "${KAZ_CONF_DIR}/container-orga.list"))
availableComposesNeedMail+=( "${availableOrga[@]}" )

knownedComposes+=( ${availableMailComposes[@]} )
knownedComposes+=( ${availableProxyComposes[@]} )
knownedComposes+=( ${availableComposesNoNeedMail[@]} )
knownedComposes+=( ${availableComposesNeedMail[@]} )

usage () {
    echo "Usage: $0 [-n] {status|start|stop|save} [compose]..."
    echo "    -n         : simulation"
    echo "    status     : docker-compose status (default all compose available)"
    echo "    start      : start composes (default all compose validate)"
    echo "    stop       : stop composes (default all compose enable)"
    echo "    save       : save all known database"
    echo "    [compose]  : in ${knownedComposes[@]}"
    exit 1
}

doCompose () {
    # $1 dans ("up -d" "down")
    # $2 nom du répertoire du compose
    echo "compose: $1 $2"
    ${SIMU} cd "${KAZ_COMP_DIR}/$2"
    if [ ! -h .env ] ; then
	echo "create .env in $2"
	${SIMU} ln -fs ../../config/dockers.env .env
    fi
    ${SIMU} docker-compose $1

    if [ "$2" = "cachet" ] && [ "$1" != "down" ]; then
	NEW_KEY=$(cd "${KAZ_COMP_DIR}/$2" ; docker-compose logs | grep APP_KEY=base64: | sed "s/^.*'APP_KEY=\(base64:[^']*\)'.*$/\1/" | tail -1)
	if [ -n "${NEW_KEY}" ]; then
	    printKazMsg "cachet key change"
	    # change key
	    ${SIMU} sed -i \
		    -e 's%^\(\s*cachet_APP_KEY=\).*$%\1"'"${NEW_KEY}"'"%' \
		    "${KAZ_KEY_DIR}/SetAllPass.sh"
	    ${SIMU} "${KAZ_BIN_DIR}/secretGen.sh"
	    # restart
	    ${SIMU} docker-compose $1
	fi
    fi
}

doComposes () {
    # $1 dans ("up -d" "down")
    # $2+ nom des répertoires des composes
    cmd=$1
    shift
    for compose in $@ ; do
	doCompose "${cmd}" ${compose}
    done
}

updateProxy () {
    # $1 dans ("on" "off")
    # $2 nom des répertoires des composes
    cmd=$1
    shift
    echo "update proxy ${cmd}: $@"
    date=$(date "+%x %X")
    for compose in $@ ; do
	composeFlag=${compose//-/_}
	entry="proxy_${composeFlag}="
	newline="${entry}${cmd}	# update by $(basename $0) at ${date}"
	if ! grep -q "proxy_${composeFlag}=" "${DOCKERS_ENV}" 2> /dev/null ; then
	    if [[ -n "${SIMU}" ]] ; then
		echo "${newline} >> ${DOCKERS_ENV}"
	    else
		echo "${newline}"  >> "${DOCKERS_ENV}"
	    fi
	else
	    ${SIMU} sed -i \
		    -e "s|${entry}.*|${newline}|g" \
		    "${DOCKERS_ENV}"
	fi
    done
	for item in "${availableProxyComposes[@]}"; do
    	${SIMU} ${KAZ_COMP_DIR}/${item}/proxy-gen.sh
    done
}

saveDB () {
    #attention, soucis avec l'option "-ti" qui ne semble pas rendre la main avec docker exec

    containerName=$1
    userName=$2
    userPass=$3
    dbName=$4
    backName=$5
    #on utilise mysqldump (v=10.5) et mariadb-dump (v>=11.4) pour être certain d'avoir un dump. L'une des 2 lignes fera une erreur
    if [[ -n "${SIMU}" ]] ; then
	${SIMU} "docker exec ${containerName} mysqldump --user=${userName} --password=${userPass} ${dbName} | gzip > $PATH_SAUVE${backName}.sql.gz"
	${SIMU} "docker exec ${containerName} mariadb-dump --user=${userName} --password=${userPass} ${dbName} | gzip > $PATH_SAUVE${backName}.sql.gz"
    else
	docker exec ${containerName} mysqldump --user=${userName} --password=${userPass} ${dbName} | gzip > $PATH_SAUVE${backName}.sql.gz
	docker exec ${containerName} mariadb-dump --user=${userName} --password=${userPass} ${dbName} | gzip > $PATH_SAUVE${backName}.sql.gz
    fi
}

declare -a enableComposesNoNeedMail enableMailComposes enableComposesNeedMail enableProxyComposes

enableComposesNoNeedMail=()
enableMailComposes=()
enableComposesNeedMail=()
enableProxyComposes=()

startComposes () {
    ./kazDockerNet.sh add ${enableComposesNoNeedMail[@]} ${enableProxyComposes[@]} ${enableMailComposes[@]} ${enableComposesNeedMail[@]} 
	[ ${#enableComposesNeedMail[@]} -ne 0 ] && [[ ! "${enableMailComposes[@]}" =~ "postfix" ]] && ./kazDockerNet.sh add postfix
	[[ "${enableComposesNeedMail[@]}" =~ "paheko" ]] && ${SIMU} ${KAZ_COMP_DIR}/paheko/paheko-gen.sh
    doComposes "up -d" ${enableComposesNoNeedMail[@]}
    doComposes "up -d" ${enableMailComposes[@]}
    doComposes "up -d" ${enableComposesNeedMail[@]}
    updateProxy "on" ${enableComposesNoNeedMail[@]} ${enableComposesNeedMail[@]}
    doComposes "up -d" ${enableProxyComposes[@]}
	for item in "${enableProxyComposes[@]}"; do
    	[[ -x "${KAZ_COMP_DIR}/${item}/reload.sh" ]] && ${SIMU} "${KAZ_COMP_DIR}/${item}/reload.sh"
	done
    if grep -q  "^.s*proxy_web.s*=.s*on" "${DOCKERS_ENV}" 2> /dev/null ; then
	${SIMU} ${KAZ_COMP_DIR}/web/web-gen.sh
    fi
}

stopComposes () {
    updateProxy "off" ${enableComposesNoNeedMail[@]} ${enableComposesNeedMail[@]}
    doComposes "down" ${enableProxyComposes[@]}
    doComposes "down" ${enableComposesNeedMail[@]}
    doComposes "down" ${enableMailComposes[@]}
    doComposes "down" ${enableComposesNoNeedMail[@]}
    if grep -q  "^.s*proxy_web.s*=.s*on" "${DOCKERS_ENV}" 2> /dev/null ; then
	${SIMU} ${KAZ_COMP_DIR}/web/web-gen.sh
    fi
}

statusComposes () {
    ${KAZ_ROOT}/bin/kazList.sh compose status ${enableMailComposes[@]} ${enableProxyComposes[@]} ${enableComposesNoNeedMail[@]} ${enableComposesNeedMail[@]}
}

saveComposes () {
    . "${DOCKERS_ENV}"
    . "${KAZ_ROOT}/secret/SetAllPass.sh"

    savedComposes+=( ${enableMailComposes[@]} )
    savedComposes+=( ${enableProxyComposes[@]} )
    savedComposes+=( ${enableComposesNoNeedMail[@]} )
    savedComposes+=( ${enableComposesNeedMail[@]} )
    
    for compose in ${savedComposes[@]}
    do
	case "${compose}" in
	    jirafeau)
	    # rien à faire (fichiers)
	    ;;
	    ethercalc)
	    #inutile car le backup de /var/lib/docker/volumes/ethercalc_calcDB/_data/dump.rdb est suffisant
	    ;;
	    #grav)
	    # ???
	    #;;
	    #postfix)
	    sympa)
       		echo "save sympa"
		saveDB ${sympaDBName} "${sympa_MYSQL_USER}" "${sympa_MYSQL_PASSWORD}" "${sympa_MYSQL_DATABASE}" sympa
		;;
	    web)
		# rien à faire (fichiers)
		;;
	    etherpad)
		echo "save pad"
		saveDB ${etherpadDBName} "${etherpad_MYSQL_USER}" "${etherpad_MYSQL_PASSWORD}" "${etherpad_MYSQL_DATABASE}" etherpad
		;;
	    framadate)
		echo "save date"
		saveDB ${framadateDBName} "${framadate_MYSQL_USER}" "${framadate_MYSQL_PASSWORD}" "${framadate_MYSQL_DATABASE}" framadate
		;;
	    cloud)
		echo "save cloud"
		saveDB ${nextcloudDBName} "${nextcloud_MYSQL_USER}" "${nextcloud_MYSQL_PASSWORD}" "${nextcloud_MYSQL_DATABASE}" nextcloud
		;;
	    paheko)
		# rien à faire (fichiers)
		;;		
	    mattermost)
		echo "save mattermost"
		saveDB ${mattermostDBName} "${mattermost_MYSQL_USER}" "${mattermost_MYSQL_PASSWORD}" "${mattermost_MYSQL_DATABASE}" mattermost
		;;
	    dokuwiki)
		# rien à faire (fichiers)
		;;
	    *-orga)
		ORGA=${compose%-orga}
		echo "save ${ORGA}"
		if grep -q "cloud:" "${KAZ_COMP_DIR}/${compose}/docker-compose.yml" 2> /dev/null ; then
		    echo "    => cloud"
		    saveDB "${ORGA}-DB" "${nextcloud_MYSQL_USER}" "${nextcloud_MYSQL_PASSWORD}" "${nextcloud_MYSQL_DATABASE}" "${ORGA}-cloud"
		fi
		if grep -q "agora:" "${KAZ_COMP_DIR}/${compose}/docker-compose.yml" 2> /dev/null ; then
		    echo "    => mattermost"
		    saveDB "${ORGA}-DB" "${mattermost_MYSQL_USER}" "${mattermost_MYSQL_PASSWORD}" "${mattermost_MYSQL_DATABASE}" "${ORGA}-mattermost"
		fi
		if grep -q "wordpress:" "${KAZ_COMP_DIR}/${compose}/docker-compose.yml" 2> /dev/null ; then
		    echo "    => wordpress"
		    saveDB "${ORGA}-DB" "${wp_MYSQL_USER}" "${wp_MYSQL_PASSWORD}" "${wp_MYSQL_DATABASE}" "${ORGA}-wordpress"
		fi
		;;
	esac
    done
}

if [ "$#" -eq 0 ] ; then
    usage
fi

if [ "$1" == "-h" ] ; then
    usage
    shift
fi

if [ "$1" == "-n" ] ; then
    export SIMU=echo
    shift
fi

DCK_CMD=""
SAVE_CMD=""
case "$1" in
    start)
	DCK_CMD="startComposes"
	shift
	;;
    
    stop)
	DCK_CMD="stopComposes"
	shift
	;;

    save)
	SAVE_CMD="saveComposes"
	shift
	;;

    status)
	DCK_CMD="statusComposes"
	shift
	;;
    *)
	usage
	;;
esac

if [ $# -eq 0 ] ; then
    enableComposesNoNeedMail=("${availableComposesNoNeedMail[@]}")
    enableMailComposes=("${availableMailComposes[@]}")
    enableComposesNeedMail=("${availableComposesNeedMail[@]}")
    enableProxyComposes=("${availableProxyComposes[@]}")
else
    if [ "${DCK_CMD}" = "startComposes" ] ; then
	enableProxyComposes=("${availableProxyComposes[@]}")
    fi
fi

for compose in $*
do
    compose=${compose%/}
    if [[ ! " ${knownedComposes[@]} " =~ " ${compose} " ]]; then
	declare -a subst
	subst=()
	for item in "${knownedComposes[@]}"; do
	    [[ "${item}" =~ "${compose}" ]] && subst+=(${item})
	done
	if [ "${subst}" = "" ] ; then
	    echo
	    echo "Unknown compose: ${RED}${BOLD}${compose}${NC} not in ${YELLOW}${BOLD}${knownedComposes[*]}${NC}"
	    echo
	    exit 1
	else
	    echo "substitute compose: ${YELLOW}${BOLD}${compose} => ${subst[@]}${NC}"
	fi
    fi
    for item in "${availableMailComposes[@]}"; do
	[[ "${item}" =~ "${compose}" ]] && enableMailComposes+=("${item}")
    done
    for item in "${availableProxyComposes[@]}"; do
	[[ "${item}" =~ "${compose}" ]] && enableProxyComposes=("${item}")
    done
    for item in "${availableComposesNoNeedMail[@]}"; do
	[[ "${item}" =~ "${compose}" ]] && enableComposesNoNeedMail+=("${item}")
    done
    for item in "${availableComposesNeedMail[@]}"; do
	[[ "${item}" =~ "${compose}" ]] && enableComposesNeedMail+=("${item}")
    done
done

[[ ! -z "${DCK_CMD}" ]] && "${DCK_CMD}" && exit 0

[[ ! -z "${SAVE_CMD}" ]] && "${SAVE_CMD}" && exit 0

exit 1