#!/bin/bash

###########################################################################
#
# MODULE:       GUI
# COPYRIGHT:    (C) 2009-2025 by CacheGuard Technologies Ltd (UK)
# COPYRIGHT:    (C) 2026-2026 by CacheGuard Technologies SAS (FR)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################

download-conf()
{
    test -n "${1}" || return 1
    local filename=${1}

    local file=${ADMIN_TMP_DIR}/${filename}
    test -f ${file} || return 11

    local size=$(get-characters-in-file ${file})

    echo "Content-Disposition: attachment; filename=\"${filename}\""
    echo "Content-Length: ${size}"
    echo "Cache-control: private"

    echo-http-header

    cat ${file}
}

set-conf-io-operation()
{
    local operation="${VALUES[0]}"
    local protocol="${VALUES[1]:1}"

    local version server filename

    case ${operation} in
	load|save)
	    local i
	    for ((i=2 ; i<ATTRIBUTE_NB ; i++))
	    do
		case ${ATTRIBUTES[${i}]} in
		    version)
			version="${VALUES[${i}]}"
			;;
		    server)
			server="${VALUES[${i}]}"
			;;
		    filename)
			filename="${VALUES[${i}]}"
			;;
		    *)
			;;
		esac
	    done

	    if test -z "${filename}" ; then
		gen-gui-error 6
		return 0
	    fi

	    case "${operation}" in
		save)
		    case "${version}" in
			new)
			    version=" ${version}"
			    ;;
			current)
			    unset version
			    ;;
			*)
			    unset version
			    ;;
		    esac
	    
		    log-command "conf" "save ${protocol} ${server} ${filename}${version}"
		    execute-command "conf save ${protocol} ${server} ${filename}${version}"
		    ;;
		load)
		    log-command "conf" "load ${protocol} ${server} ${filename}"
		    execute-command "conf load ${protocol} ${server} ${filename}"
		    ;;
		*)
		    return 255
		    ;;
	    esac
	    ;;
	upload)
	    filename="${VALUES[2]}"
	    if test -z "${filename}" ; then
		gen-gui-error 6
	    else
		local uploaded_filename=${UPLOADED_FILES[0]}
		if test -n "${uploaded_filename}" -a -f ${ADMIN_TMP_DIR}/${uploaded_filename} ; then
		    execute-command "conf inject ${uploaded_filename}"
		    rm -f ${ADMIN_TMP_DIR}/${uploaded_filename}
		else
		    rm -f ${ADMIN_TMP_DIR}/${uploaded_filename}
		    gen-gui-error 16
		fi
	    fi
	    ;;
	download)
	    test "${protocol}" == 'web' || exit 0
	    version=${VALUES[2]}
	    filename=${VALUES[3]}
	    test -n "${filename}" || filename=${DEFAULT_SHOSTNAME}.conf

	    case ${version} in
		'cur')
		    unset version
		    ;;
		'new')
		    version=" ${version}"
		    ;;
		*)
		    unset version
		    ;;
	    esac

	    execute-command "conf create ${filename}${version}"
	    download-conf ${filename}
	    execute-command "conf clear ${filename}"
	    reset-gui-error-log
	    exit 0 # important
	    ;;
	*)
	    return 255
	    ;;
    esac
}

set-conf-io-operation
