#!/bin/bash
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2011  Michael Tremer & Christian Schmidt                      #
#                                                                             #
# 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/>.       #
#                                                                             #
###############################################################################

# Setup logging.
LOG_FACILITY="network-hotplug-rename"
LOG_DISABLE_STDOUT="true"

. /usr/lib/network/functions

# Setup the locking.
LOCKFILE="${RUN_DIR}/.rename_lock"
function cleanup() {
	lock_release ${LOCKFILE}
}
trap cleanup EXIT TERM KILL

# Check if the INTERFACE variable is properly set.
assert isset INTERFACE

# Log what we are doing here.
log DEBUG "Called for interface '${INTERFACE}'."

# Just check if the device has already vanished.
device_exists ${INTERFACE} || exit ${EXIT_ERROR}

# Acquiring lock for this operation.
lock_acquire ${LOCKFILE}

# Check if the device is already in use and
# prevent the script to touch it in any way.
if ! device_is_free ${INTERFACE}; then
	log ERROR "The device '${INTERFACE}' is in use."
	exit ${EXIT_ERROR}
fi

# Determine the type of the device and then see what
# we need to do with it.
type=$(device_get_type ${INTERFACE})
log DEBUG "Interface '${INTERFACE}' is of type '${type}'."

case "${type}" in
	ethernet|real)
		# Search within all the port configurations
		# if this port has already been configured.
		for port in $(ports_get_all); do
			port_cmd hotplug_rename ${port} ${INTERFACE} &>/dev/null
			if [ $? -eq ${EXIT_TRUE} ]; then
				echo "${port}"
				exit ${EXIT_OK}
			fi
		done

		# If no port configuration could be found,
		# we search for the next unused name and return that.
		port=$(port_find_free ${PORT_PATTERN})
		echo "${port}"

		log DEBUG "Could not find an existing port configuration for '${INTERFACE}'."
		log DEBUG "${INTERFACE} --> ${port}"
		;;
esac

exit ${EXIT_OK}
