#!/bin/bash

# live-webhelper - web interface to live-helper
# Copyright (C) 2007 Richard Nelson <unixabg@gmail.com>
# Copyright (C) 2007 Daniel Baumann <daniel@debian.org>
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# On Debian systems, the complete text of the GNU General Public License
# can be found in /usr/share/common-licenses/GPL-2 file.

# Reading common
. /usr/share/live-webhelper/common

# Reading defaults
if [ -r /etc/default/live-webhelper ]
then
        . /etc/default/live-webhelper
else
        echo "E: /etc/default/live-webhelper missing"
        exit 1
fi

# Sending http header
echo "Content-type: text/html"
echo

# Sending html header
cat "${TEMPLATES}"/header.html

# CGI
if [ -z "${QUERY_STRING}" ]
then
	# Sending html form
	sed -e "s#MIRROR_BOOTSTRAP_SECURITY#${MIRROR_BOOTSTRAP_SECURITY}#" \
	    -e "s#MIRROR_BOOTSTRAP#${MIRROR_BOOTSTRAP}#" \
	    -e "s#MIRROR_BINARY_SECURITY#${MIRROR_BINARY_SECURITY}#" \
	    -e "s#MIRROR_BINARY#${MIRROR_BINARY}#" \
	    -e "s/VERSION/${VERSION}/" \
	    -e "s/DATE/`date +%Y%m%d-%H:%M`/" \
	"${TEMPLATES}"/form.html
else
	# Converting spaces:	sed 's/+/ /g'
	# Converting '@':	sed 's/%40/@/g'
	# Converting ':':	sed 's/%3A/:/g'
	# Converting ';':	sed 's/%3B/\;/g'
	# Converting '/':	sed 's/%2F/\//g'
	# Converting '~':	sed 's/%7E/\~/g'

	# Translate parameters
        QUERY_STRING=$(echo ${QUERY_STRING} | sed -e 's/%2F/\//g' -e 's/+/ /g' -e 's/%3B/;/g' -e 's/%7E/~/g' -e 's/%3A/:/g' -e 's/%40/@/g')
	# Debug the filtering string
	# echo ${QUERY_STRING}

	# Email
	EMAIL=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])email=[0-9,a-z,A-Z,.,_,-,@]+' | cut -f 2 -d '=' | head -n1)

	# Standard options
	BINARY_IMAGES=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])binary_images=[a-z]+' | cut -f 2 -d '=' | head -n1)
	DISTRIBUTION=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])distribution=[a-z]+' | cut -f 2 -d '=' | head -n1)
	PACKAGES_LISTS=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])packages_lists=[0-9,a-z,-]+' | cut -f 2 -d '=' | head -n1)
	PACKAGES=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])packages=[0-9,a-z,A-Z,., ,_,-]+' | cut -f 2 -d '=' | head -n1)

	# Advanced bootstrap options
	ARCHITECTURE=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])architecture=[0-9,a-z]+' | cut -f 2 -d '=' | head -n1)
	BOOTSTRAP_FLAVOUR=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])bootstrap_flavour=[a-z]+' | cut -f 2 -d '=' | head -n1)
	MIRROR_BOOTSTRAP=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])mirror_bootstrap=[0-9,a-z,A-Z,.,~,:,/,_,-]+' | cut -f 2 -d '=' | head -n1)
	MIRROR_BOOTSTRAP_SECURITY=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])mirror_bootstrap=[0-9,a-z,A-Z,.,~,:,/,_,-]+' | cut -f 2 -d '=' | head -n1)
	MIRROR_BINARY=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])mirror_binary=[0-9,a-z,A-Z,.,~,:,/,_,-]+' | cut -f 2 -d '=' | head -n1)
	MIRROR_BINARY_SECURITY=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])mirror_binary_security=[0-9,a-z,A-Z,.,~,:,/,_,-]+' | cut -f 2 -d '=' | head -n1)
	SECTIONS=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])sections=[a-z]+' | cut -f 2 -d '=' | head -n1)

	# Advanced chroot options
	CHROOT_FILESYSTEM=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])chroot_filesystem=[0-9,a-z]+' | cut -f 2 -d '=' | head -n1)
	LINUX_FLAVOURS=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])linux_flavours=[0-9,a-z,-]+' | cut -f 2 -d '=' | head -n1)
	SECURITY=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])security=[a-z]+' | cut -f 2 -d '=' | head -n1)
	SYMLINKS=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])symlinks=[a-z]+' | cut -f 2 -d '=' | head -n1)
	SYSVINIT=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])sysvinit=[a-z]+' | cut -f 2 -d '=' | head -n1)

	# Advanced binary options
	BINARY_INDICES=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])binary_indices=[a-z]+' | cut -f 2 -d '=' | head -n1)
	BOOTAPPEND=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])bootappend=[0-9,a-z,A-Z,., ,_,+,-]+' | cut -f 2 -d '=' | head -n1)
	BOOTLOADER=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])bootloader=[a-z]+' | cut -f 2 -d '=' | head -n1)
	DEBIAN_INSTALLER=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])debian_installer=[a-z]+' | cut -f 2 -d '=' | head -n1)
	ENCRYPTION=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])encryption=[0-9,a-z,A-Z]+' | cut -f 2 -d '=' | head -n1)
	HOSTNAME=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])hostname=[0-9,a-z,A-Z,.,-]+' | cut -f 2 -d '=' | head -n1)
	ISO_APPLICATION=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])iso_application=[0-9,a-z,A-Z,., ,~,;,:,/,_,-]+' | cut -f 2 -d '=' | head -n1)
	ISO_PREPARER=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])iso_preparer=[0-9,a-z,A-Z,., ,~,;,:,/,_,-]+' | cut -f 2 -d '=' | head -n1)
	ISO_PUBLISHER=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])iso_publisher=[0-9,a-z,A-Z,., ,~,;,:,/,_,/,-]+' | cut -f 2 -d '=' | head -n1)
	ISO_VOLUME=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])iso_volume=[0-9,a-z,A-Z,., ,~,;,:,/,_,-]+' | cut -f 2 -d '=' | head -n1)
	MEMTEST=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])memtest=[0-9,a-z,A-Z,.,_,-]+' | cut -f 2 -d '=' | head -n1)
	NET_PATH=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])net_path=[0-9,a-z,A-Z,.,_,/,-]+' | cut -f 2 -d '=' | head -n1)
	NET_SERVER=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])net_server=[0-9,a-z,A-Z,.,_,-]+' | cut -f 2 -d '=' | head -n1)
	USERNAME=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])username=[0-9,a-z,A-Z,.,-]+' | cut -f 2 -d '=' | head -n1)

	# Advanced source options
	SOURCE=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])source=[a-z]+' | cut -f 2 -d '=' | head -n1)
	SOURCE_IMAGES=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])source_images=[a-z]+' | cut -f 2 -d '=' | head -n1)

	# Unofficial options
	CUSTOM_BOOTSTRAP=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])custom_bootstrap=[0-9,a-z,A-Z,., ,~,;,:,/,_,-]+' | cut -f 2 -d '=' | head -n1)
	CUSTOM_BINARY=$(echo ${QUERY_STRING} | grep -oE '(^|[?&])custom_binary=[0-9,a-z,A-Z,., ,~,;,:,/,_,-]+' | cut -f 2 -d '=' | head -n1)

	# FIXME: filter invalid options
	unset QUERY_STRING

	if [ -z "${EMAIL}" ]
	then
		echo "<h2><div style='color: red;'>Error: No email address specified.</div></h2>"

		sed -e "s#MIRROR_BOOTSTRAP_SECURITY#${MIRROR_BOOTSTRAP_SECURITY}#" \
		    -e "s#MIRROR_BOOTSTRAP#${MIRROR_BOOTSTRAP}#" \
		    -e "s#MIRROR_BINARY_SECURITY#${MIRROR_BINARY_SECURITY}#" \
		    -e "s#MIRROR_BINARY#${MIRROR_BINARY}#" \
		    -e "s/VERSION/${VERSION}/" \
		    -e "s/DATE/`date +%Y%m%d-%H:%M`/" \
		"${TEMPLATES}"/form.html

		exit
	fi

	# Getting build identifier
	BUILD=$(date +%Y%m%d.%H%M%S.%N)

	echo ${QUERY_STRING}
	# Sending html confirmation
	# Note: On each string remember to use a delimeter that is not in the string.
	sed -e "s/BUILD/${BUILD}/g" \
	    -e "s/EMAIL/${EMAIL}/" \
	    -e "s/BINARY_IMAGES/${BINARY_IMAGES}/" \
	    -e "s/DISTRIBUTION/${DISTRIBUTION}/" \
	    -e "s/PACKAGES_LISTS/${PACKAGES_LISTS}/" \
	    -e "s/PACKAGES/${PACKAGES}/" \
	    -e "s/ARCHITECTURE/${ARCHITECTURE}/" \
	    -e "s/BOOTSTRAP_FLAVOUR/${BOOTSTRAP_FLAVOUR}/" \
	    -e "s#MIRROR_BOOTSTRAP_SECURITY#${MIRROR_BOOTSTRAP_SECURITY}#" \
	    -e "s#MIRROR_BOOTSTRAP#${MIRROR_BOOTSTRAP}#" \
	    -e "s#MIRROR_BINARY_SECURITY#${MIRROR_BINARY_SECURITY}#" \
	    -e "s#MIRROR_BINARY#${MIRROR_BINARY}#" \
	    -e "s/SECTIONS/${SECTIONS}/" \
	    -e "s/CHROOT_FILESYSTEM/${CHROOT_FILESYSTEM}/" \
	    -e "s/LINUX_FLAVOURS/${LINUX_FLAVOURS}/" \
	    -e "s/SECURITY/${SECURITY}/" \
	    -e "s/SYMLINKS/${SYMLINKS}/" \
	    -e "s/SYSVINIT/${SYSVINIT}/" \
	    -e "s/BINARY_INDICES/${BINARY_INDICES}/" \
	    -e "s/BOOTAPPEND/${BOOTAPPEND}/" \
	    -e "s/BOOTLOADER/${BOOTLOADER}/" \
	    -e "s/DEBIAN_INSTALLER/${DEBIAN_INSTALLER}/" \
	    -e "s/ENCRYPTION/${ENCRYPTION}/" \
	    -e "s/HOSTNAME/${HOSTNAME}/" \
	    -e "s#ISO_APPLICATION#${ISO_APPLICATION}#" \
	    -e "s#ISO_PREPARER#${ISO_PREPARER}#" \
	    -e "s#ISO_PUBLISHER#${ISO_PUBLISHER}#" \
	    -e "s#ISO_VOLUME#${ISO_VOLUME}#" \
	    -e "s/MEMTEST/${MEMTEST}/" \
	    -e "s#NET_PATH#${NET_PATH}#" \
	    -e "s/NET_SERVER/${NET_SERVER}/" \
	    -e "s#SERVER#${SERVER}#g" \
	    -e "s/USERNAME/${USERNAME}/" \
	    -e "s/SOURCE_IMAGES/${SOURCE_IMAGES}/" \
	    -e "s/SOURCE/${SOURCE}/" \
	    -e "s#CUSTOM_BOOTSTRAP#${CUSTOM_BOOTSTRAP}#" \
	    -e "s#CUSTOM_BINARY#${CUSTOM_BINARY}#" \
	"${TEMPLATES}"/build.html

	# Creating temporary directory
	mkdir -p "${TEMPDIR}"

# Writing build file
cat > "${TEMPDIR}"/"${BUILD}".build << EOF
# live-webhelper ${VERSION} build file
# `date -R`

BUILD="${BUILD}"
EMAIL="${EMAIL}"

# Standard options
LH_BINARY_IMAGES="${BINARY_IMAGES}"
LH_DISTRIBUTION="${DISTRIBUTION}"
LH_PACKAGES_LISTS="${PACKAGES_LISTS}"
LH_PACKAGES="${PACKAGES}"

# Advanced bootstrap options
LH_ARCHITECTURE="${ARCHITECTURE}"
LH_MIRROR_BOOTSTRAP_SECURITY="${MIRROR_BOOTSTRAP}"
LH_MIRROR_BOOTSTRAP="${MIRROR_BOOTSTRAP}"
LH_MIRROR_BINARY_SECURITY="${MIRROR_BINARY_SECURITY}"
LH_MIRROR_BINARY="${MIRROR_BINARY}"
LH_SECTIONS="${SECTIONS}"

# Advanced chroot options
LH_CHROOT_FILESYSTEM="${CHROOT_FILESYSTEM}"
LH_LINUX_FLAVOURS="${LINUX_FLAVOURS}"
LH_SECURITY="${SECURITY}"
LH_SYMLINKS="${SYMLINKS}"
LH_SYSVINIT="${SYSVINIT}"

# Advanced binary options
LH_BINARY_INDICES="${BINARY_INDICES}"
LH_BOOTAPPEND="${BOOTAPPEND}"
LH_BOOTLOADER="${BOOTLOADER}"
LH_DEBIAN_INSTALLER="${DEBIAN_INSTALLER}"
LH_ENCRYPTION="${ENCRYPTION}"
LH_HOSTNAME="${HOSTNAME}"
LH_ISO_APPLICATION="${ISO_APPLICATION}"
LH_ISO_PREPARER="${ISO_PREPARER}"
LH_ISO_PUBLISHER="${ISO_PUBLISHER}"
LH_ISO_VOLUME="${ISO_VOLUME}"
LH_MEMTEST="${MEMTEST}"
LH_NET_PATH="${NET_PATH}"
LH_NET_SERVER="${NET_SERVER}"
LH_USERNAME="${USERNAME}"

# Advanced source options
LH_SOURCE_IMAGES="${SOURCE_IMAGES}"
LH_SOURCE="${SOURCE}"

# Unofficial options
LH_CUSTOM_BOOTSTRAP="${CUSTOM_BOOTSTRAP}"
LH_CUSTOM_BINARY="${CUSTOM_BINARY}"
EOF

	echo "$(date +%b\ %d\ %H:%M:%S) ${HOSTNAME} live-webhelper: add web build (${BUILD})." >> /var/log/live
	echo "$(date +%b\ %d\ %H:%M:%S) ${HOSTNAME} live-webhelper: options ${BUILD} |email ${EMAIL}|binary_images ${BINARY_IMAGES}|distribution ${DISTRIBUTION}|packages_lists ${PACKAGES_LISTS}|packages ${PACKAGES}|architecture ${ARCHITECTURE}|mirror_bootstrap_security ${MIRROR_BOOTSTRAP}|mirror_bootstrap ${MIRROR_BOOTSTRAP}|mirror_binary_security ${MIRROR_BINARY_SECURITY}|mirror_binary ${MIRROR_BINARY}|sections ${SECTIONS}|chroot_filesystem ${CHROOT_FILESYSTEM}|linux_flavours ${LINUX_FLAVOURS}|security ${SECURITY}|symlinks ${SYMLINKS}|sysvinit ${SYSVINIT}|binary_indices ${BINARY_INDICES}|bootappend ${BOOTAPPEND}|bootloader ${BOOTLOADER}|debian_installer ${DEBIAN_INSTALLER}|encryption ${ENCRYPTION}|hostname ${HOSTNAME}|iso_application ${ISO_APPLICATION}|iso_preparer ${ISO_PREPARER}|iso_publisher ${ISO_PUBLISHER}|iso_volume ${ISO_VOLUME}|memtest ${MEMTEST}|net_path ${NET_PATH}|net_server ${NET_SERVER}|username ${USERNAME}|source_images ${SOURCE_IMAGES}|source ${SOURCE}|" >> /var/log/live
fi

sed -e "s/VERSION/${VERSION}/" "${TEMPLATES}"/footer.html
