MAJOR=4
MINOR=45
DEVNAME=tty45
      ˆl–ßi~”d
e¶¥8 ¸î	µ1hßÌ0¿É  #   ˆl–Ým_Jõ/”Šy@=q·nå1hßÍ„"y¿ÀÊ ?÷     #!/bin/bash
#
# Translate tool from bonding configuration to team.
#
# Copyright (C) 2013 Flavio Leitner <fbl@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2,
# as published by the Free Software Foundation.
#
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
#

VERSION="0.97"

PR_QUIET=0
PR_ERR=1
PR_WARN=2
PR_INFO=3
PR_DBG=4

#defaults
ARGC=$#

FORMAT_IFCFG=0
FORMAT_JANSSON=1
OUTPUT_FORMAT=${FORMAT_IFCFG}

MODE_IFCFG=0
MODE_NOIFCFG=1
MODE=${MODE_IFCFG}

OUTPUT_FILE=
RENAME=
DEVICE=
BOND_MASTER=
STDOUT=1
BONDING_OPTS=
CONFIGDIR="/etc/sysconfig/network-scripts"
PR_LVL=3
OUTPUT_DIR=
OUTPUT_TMP_DIR=
TMP_FILES=()
RUNNER_OPTS=()
LWATCH_OPTS=()
# array: ( 'port ifname', 'opt1', 'opt2', 'optn', 'opt4', 'port ifname', ... )
PORT_LIST=
PORTS_OPTS=()
PRIMARY=
PRIMARY_RESELECT=

# building file scratch memory area
VFILE=

show_examples()
{
	cat << EOF

The following commands will deliver the ifcfg files into a temporary
directory. You can review the files and copy to the right location.

Add the following argument to the commands below to print the output
to the screen instead of writing to files.
  --stdout

Add the following arguments to the commands below to set the
destination directory for the output files.
  --outputdir </path/to/dir>

Add the following argument to the commands below to output the
files in teamd format (JSON) instead of the default ifcfg format.
  --json

To convert the current "bond0" ifcfg configuration to team ifcfg:
# $0 --master bond0

To convert the current "bond0" ifcfg configuration out of the
standard ifcfg-:
# $0 --master bond0 --configdir </path/to/ifcfg>

To convert the current "bond0" ifcfg configuration to team ifcfg
renaming the interface name to "team0". (carefull: firewall rules,
aliases interfaces, etc., will break after the renaming because the
tool will only change the ifcfg file, nothing else)
# $0 --master bond0 --rename team0

To convert given bonding parameters without any ifcfg:
# $0 --bonding_opts "mode=1 miimon=500"

To convert given bonding parameters without any ifcfg with ports:
# $0 --bonding_opts "mode=1 miimon=500 primary=eth1 primary_reselect-0" \\
     --port eth1 --port eth2 --port eth3 --port eth4

EOF
}
usage()
{
	cat << EOF
usage: $0 [options]

This tool translates bonding configuration to team.
See bond2team(1) for detailed information.

OPTIONS:

  --master <interface>	set the master interface name or ifcfg

  --rename <iface>	rename the master interface to <iface>

  --ifcfg		set the output format to ifcfg style

  --json		set the output format to teamd style

  --bonding_opts	pass the bonding options instead of reading
			from the ifcfg- file

  --port <iface>	add the interface to the port's list

  --configdir <dir>	set where the ifcfg- files are
			default: /etc/sysconfig/network-scripts

  --outputdir <dir>	set the output diretory
			default: temporary diretory

  --stdout		print to stdout instead of
			modify the system's files.

  --debug		increase debug level

  --quiet		no messages

  --version		show the tool version

  --help		this screen

  --examples		show command examples

EOF
}

# Output Functions
pr()
{
	if [ $1 -le $PR_LVL ]; then
		shift;
		echo "$*" > /dev/stderr
	fi
}

pr_error()
{
	pr ${PR_ERR} "ERROR: " $*
}

pr_warn()
{
	pr ${PR_WARN} "WARNING: " $*
}

pr_info()
{
	pr ${PR_INFO} "INFO: " $*
}

pr_dbg()
{
	pr ${PR_DBG} "DEBUG: " $*
}

to_stdout()
{
	return ${STDOUT}
}

create_output_file()
{
	local f=$1

	if [ ! -d "${OUTPUT_TMP_DIR}