#!/bin/sh
#  Copyright (C) 2000-2007 SWsoft. All rights reserved.
#  Copyright (C) 2006-2007 Dmitry V. Levin <ldv@altlinux.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.
#
#

# Set the sane umask
umask 022
CP='/bin/cp -fp'
# overwrite copy arguments as a workaround for FC5 bug #190601 (ovz bug #297)
[ -f /etc/fedora-release ] && CP='/bin/cp -f --preserve=mode,ownership'

# Error codes
VZ_INVALID_PARAMETER_SYNTAX=20
VZ_FS_NO_DISK_SPACE=46
VZ_FS_BAD_TMPL=47
VZ_FS_NEW_VE_PRVT=48
VZ_CHANGEPASS=74
VZ_CANT_ADDIP=34
VZ_IP_INUSE=78

FAKEGATEWAY=192.0.2.1
FAKEGATEWAYNET=192.0.2.0

# Prints error message and exits
# Parameters:
#   $1 - error message
#   $2 - exit code
# Example of usage:
#   error "Fatal error" 1
error()
{
        # print errors to stdout too
        ERR=$?
        echo "$SELFNAME ERROR: $1"
        exit ${2}
}

# Puts line
# NAME="value"
# to config file. If NAME is found, line gets replaced,
# otherwise it is added to the end of file.
# Parameters:
# $1 - config file
# $2 - NAME
# $3 - value
put_param()
{
	local file="$1"
	local name="$2"
	local value="$3"
	local path="${file%/*}"

	[ -d "$path" ] ||
		mkdir -p "$path" ||
		error "Unable to create dir $path" ${VZ_FS_NO_DISK_SPACE}
	if grep -qs "^$name=.*" "$file"; then
		sed -i "s|^$name=.*|$name=\"$value\"|" "$file"
	else
		echo "$name=\"$value\"" >> "$file"
	fi ||
		error "Can't change file $file" ${VZ_FS_NO_DISK_SPACE}
}

# Adds value to variable NAME
# in config file. If NAME is found, value gets added,
# otherwise it is added to the end of file.
# Parameters:
# $1 - config file
# $2 - NAME
# $3 - value
add_param()
{
	local file="$1"
	local name="$2"
	local value="$3"
	local path="${file%/*}"

	[ -d "$path" ] ||
		mkdir -p "$path" ||
		error "Unable to create dir $path" ${VZ_FS_NO_DISK_SPACE}
	if grep -qs "^$name=" "$file"; then
		sed -i "s|^$name=\"\(.*\)\"|$name=\"\1 $value \"|" "$file"
	else
		echo "$name=\"$value\"" >> "$file"
	fi ||
		error "Can't change file $file" ${VZ_FS_NO_DISK_SPACE}
}

del_param()
{
	local file=$1
	local name=$2
	local value=$3
	local path="${file%/*}"

	[ -d "$path" ] ||
		mkdir -p "$path" ||
		error "Unable to create dir $path" ${VZ_FS_NO_DISK_SPACE}
	if grep -qs "^$name=" "$file"; then
		if [ -z "$value" ]; then
			sed -i "/^${name}=.*/d" "$file"
		else
			sed -i "s|^${name}=\(.*\)${value}\(.*\)|${name}=\1\2|" "$file"
		fi ||
			error "Can't change file $file" ${VZ_FS_NO_DISK_SPACE}
	fi
}

# Puts line
# NAME value
# to config file. If NAME is found, line gets replaced,
# otherwise it is added to the end of file.
# Parameters:
# $1 - config file
# $2 - NAME
# $3 - value
put_param2()
{
	local file="$1"
	local name="$2"
	local value="$3"
	local path="${file%/*}"

	[ -d "$path" ] ||
		mkdir -p "$path" ||
		error "Unable to create dir $path" ${VZ_FS_NO_DISK_SPACE}
	if grep -qs "^\<$name\>" "$file"; then
		sed -i "s|^\<$name\>.*|$name $value|" "$file"
	else
		echo "$name $value" >> "$file"
	fi ||
		error "Can't change file $file" ${VZ_FS_NO_DISK_SPACE}
}

# Puts line
# NAME=( value )
# to config file. If NAME is found, line gets replaced,
# otherwise it is added to the end of file.
# Parameters:
# $1 - config file
# $2 - NAME
# $3 - value
put_param3()
{
	local file="$1"
	local name="$2"
	local value="$3"
	local path="${file%/*}"

	[ -d "$path" ] ||
		mkdir -p "$path" ||
		error "Unable to create dir $path" ${VZ_FS_NO_DISK_SPACE}
	if grep -Eqs "^$name=\(.*\)" "$file"; then
		if [ -z "$value" ]; then
			sed -i "s|^$name=\(.*\)|$name=\( \)|" "$file"
		else
			sed -i "s|^$name=\(.*\)|$name=\( \"$value\" \)|" "$file"
		fi
	else
		if [ -z "$value" ]; then
			echo "$name=( )" >> "$file"
		else
			echo "$name=( \"$value\" )" >> "$file"
		fi
	fi ||
		error "Can't change file $file" ${VZ_FS_NO_DISK_SPACE}
}

# Adds value to array NAME
# in config file. If NAME is found, value gets added,
# otherwise it is added to the end of file.
# Parameters:
# $1 - config file
# $2 - NAME
# $3 - value
add_param3()
{
	local file="$1"
	local name="$2"
	local value="$3"
	local path="${file%/*}"

	[ -d "$path" ] ||
		mkdir -p "$path" ||
		error "Unable to create dir $path" ${VZ_FS_NO_DISK_SPACE}
	if grep -Eqs "^$name=\(.*\)" "$file"; then
		sed -i -r "s|^$name=\((.*)\)|$name=\( \1 \"$value\" \)|" "$file"
	else
		echo "$name=( \"$value\" )" >> "$file"
	fi ||
		error "Can't change file $file" ${VZ_FS_NO_DISK_SPACE}
}

# Removes value from array NAME
# in config file. If NAME is found, value gets removed,
# otherwise this is a noop function.
# Parameters:
# $1 - config file
# $2 - NAME
# $3 - value
del_param3()
{
	local file="$1"
	local name="$2"
	local value="$3"

	[ -f $file ] || return

	if grep -Eqs "^$name=\(.*\)" "$file"; then
		sed -i -r "s|^($name=\( .*)\"$value\"(.* \))|\1\2|" "$file" ||
		error "Can't change file $file" ${VZ_FS_NO_DISK_SPACE}
	fi
}

remove_debian_interface()
{
	local dev="$1"
	local cfg="$2"

	${CP} ${cfg} ${cfg}.$$ ||
		error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE

	awk '
		NF == 0 {next}
		$1 == "auto" && $2 ~/'${dev}'$/ {next}
		$1 == "iface" && $2 ~/'${dev}'$/ {skip = 1; next}
		/^\t/ && skip {next}
		{skip = 0; print}
	' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg}

	rm -f ${cfg}.$$ 2>/dev/null
}

remove_fake_old_route()
{
	local file="$1"

	[ -f "$file" ] || return 0
	grep -qs '191\.255\.255\.[0-1]' "$file" ||
		return 0

	sed -i '/191\.255\.255\.[0-1]/d' "$file"
}

change_hostname()
{
	local cfg="$1"
	local host="$2"
	local ip="$3"
	local comm='# Auto-generated hostname. Please do not remove this comment.'

	[ -f "$cfg" ] || touch "$cfg"
	if [ "$host" = 'localhost' -o "$host" = 'localhost.localdomain' ]; then
		put_param2 "$cfg" '127.0.0.1' 'localhost.localdomain localhost'
		return
	fi
	${CP} "$cfg" "$cfg.$$" ||
		error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE
	awk -v ip="$ip" -v host="$host" -v comm="$comm" '
		BEGIN {found = 0; skip = 0}
		$0 == comm {found = 1; next}
		found {
			if (ip == "") {ip = $1}
			next;
		}
		!found && $2 == host {skip = 1}
		$1 == "127.0.0.1" {
			print "127.0.0.1	localhost.localdomain localhost";
			next;
		}
		{print}
		END {
			if (skip) exit 0;
			if (ip == "") { ip = "127.0.0.1" }
			print comm;
			alias=""
			if ((i=index(host, ".")) > 1) {
				alias=substr(host, 1, i - 1);
			}
			print ip " " host " " alias;
		}
	' < "$cfg" > "$cfg.$$"
	if [ $? -ne 0 ]; then
		rm -f "$cfg.$$" 2>/dev/null
		error "Can't change file ${cfg}" $VZ_FS_NO_DISK_SPACE
	fi
	mv -f "$cfg.$$" "$cfg"
}
