#!/bin/bash

# $Id: profile-change.in,v 1.12 2001/10/09 16:44:32 cph Exp $
#
# Copyright (c) 2001 Massachusetts Institute of Technology
#
# 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., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

# This script is called by the network-control scripts, after ifup or
# ifdown is run, and is intended to adjust the machine's profile as
# needed for the current network environment.

set -e

export PATH=/bin:/usr/bin:/sbin:/usr/sbin

PROFILE_DIR="/etc/laptop-net/profiles"
PROFILE_FILE="/var/state/laptop-net/profile"
PROFILE_CHANGE="/usr/share/laptop-net/profile-change"
GET_ADDR="/usr/lib/laptop-net/get-addr"
LOGGER="/usr/bin/logger -p daemon.info -t laptop-net"

INTERFACE="${1}"
IP_ADDRESS="${2}"

run_init_scripts ()
{
    PROFILE="${1}"
    KEYWORD="${2}"
    RCDIR="${PROFILE_DIR}/${PROFILE}/rc.d"
    if [ "${KEYWORD}" = "start" ]; then
	PREFIX="S"
    else
	PREFIX="K"
    fi
    if [ -d "${RCDIR}" ]; then
	for FILENAME in ${RCDIR}/${PREFIX}[0-9][0-9]*; do
	    if [ -f "${FILENAME}" ]; then
		case "${FILENAME}" in
		*.sh)
		    /bin/sh "${FILENAME}" "${KEYWORD}" || true
		    ;;
		*)
		    "${FILENAME}" "${KEYWORD}" || true
		    ;;
		esac
	    fi
	done
    fi
}

run_script ()
{
    PROFILE="${1}"
    NAME="${2}"
    if [ -x "${PROFILE_DIR}/${PROFILE}/${NAME}" ]; then
	"${PROFILE_DIR}/${PROFILE}/${NAME}" "${PROFILE_DIR}" "${PROFILE}" \
	    || true
    fi
}

NEW_PROFILE=""
if [ -d "${PROFILE_DIR}" ]; then
    if [ -z "${IP_ADDRESS}" ]; then
	# Either an IP address, "down", or "unknown".
	# "down" means the interface is down.
	# "unknown" means we were unable to examine the interface.
	IP_ADDRESS="$("${GET_ADDR}" "${INTERFACE}" 2> /dev/null)"
    fi

    for P in $(cd "${PROFILE_DIR}"; /bin/ls -d [a-z0-9]* 2> /dev/null); do
	D="${PROFILE_DIR}/${P}"
	if [ -r "${D}/patterns" ]; then
	    for PATTERN in $(cat "${D}/patterns");do
		case "${IP_ADDRESS}" in
		${PATTERN})
		    NEW_PROFILE="${P}"
		    break 2
		    ;;
		esac
	    done
	fi
    done
fi

if [ -r "${PROFILE_FILE}" ]; then
    OLD_PROFILE="$(cat "${PROFILE_FILE}")"
else
    OLD_PROFILE=""
fi

# Do nothing if the profile isn't changing.
[ "${NEW_PROFILE}" != "${OLD_PROFILE}" ] || exit 0

if [ -n "${OLD_PROFILE}" ]; then
    ${LOGGER} "Deselecting network profile \"${OLD_PROFILE}\""
    run_init_scripts "${OLD_PROFILE}" stop
    run_script "${OLD_PROFILE}" "deselect"
fi

if [ -n "${NEW_PROFILE}" ]; then
    ${LOGGER} "Selecting network profile \"${NEW_PROFILE}\""
    run_script "${NEW_PROFILE}" "before-select"
    if [ -d "${PROFILE_DIR}/${NEW_PROFILE}/files.d" ]; then
	(
	    cd "${PROFILE_DIR}/${NEW_PROFILE}/files.d"
	    if [ -n "$(/bin/ls)" ]; then
		/bin/cp -a * /
	    fi
	)
    fi
    run_init_scripts "${NEW_PROFILE}" start
    run_script "${NEW_PROFILE}" "after-select"
fi

echo "${NEW_PROFILE}" > "${PROFILE_FILE}"

exit 0
