#!/bin/sh
#
# This file is part of OpenMediaVault.
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Volker Theile <volker.theile@openmediavault.org>
# @copyright Copyright (c) 2009-2012 Volker Theile
#
# OpenMediaVault 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
# any later version.
#
# OpenMediaVault 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 OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

OMV_ISSUE_CONFIG=${OMV_ISSUE_CONFIG:-"/etc/issue"}

mkconf() {
	prdname=$(xmlstarlet sel -t -v //name ${OMV_PRODUCTINFO_FILE} | xmlstarlet unesc)
	packagename=$(xmlstarlet sel -t -v //packagename ${OMV_PRODUCTINFO_FILE} | xmlstarlet unesc)
	version=$(dpkg-query -W -f='${Version}' ${packagename})
	versionname=$(xmlstarlet sel -t -v //versionname ${OMV_PRODUCTINFO_FILE} | xmlstarlet unesc)
	copyright=$(xmlstarlet sel -t -v //copyright ${OMV_PRODUCTINFO_FILE} | xmlstarlet unesc)
	url=$(xmlstarlet sel -t -v //url ${OMV_PRODUCTINFO_FILE} | xmlstarlet unesc)

	cat <<EOF > ${OMV_ISSUE_CONFIG}
${prdname} ${version} (${versionname}) \n \l
${copyright}

To manage the system visit the ${prdname} web administration
interface via a web browser on:

EOF

	ifaces=$(ls /sys/class/net | grep -vi "lo")
	if [ -n "${ifaces}" ]; then
		for iface in ${ifaces} ; do
			ipaddr=$(omv_get_ipaddr ${iface})
			if [ -n "${ipaddr}" ]; then
				echo "${iface}: ${ipaddr}" >> ${OMV_ISSUE_CONFIG}
			fi
		done
	else
		echo "No interface(s) available" >> ${OMV_ISSUE_CONFIG}
	fi

	cat <<EOF >> ${OMV_ISSUE_CONFIG}

For more information regarding this appliance, please visit
the web site: ${url}

EOF
}

case "$1" in
	mkconf|*)
		mkconf
		;;
esac
