#!/bin/sh

# $Id: link-change.in,v 1.5 2002/01/02 06:00:15 cph Exp $
#
# Copyright (c) 2000-2002 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.

# Notice when the user connects or disconnects the network cable, and
# change the interface state up or down as needed.  This script is
# called by ifd, a daemon that watches network interfaces and invokes
# this script on transitions.

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

# Interface name, e.g. "eth0".
INTERFACE="${1}"

# Either "watched" or "unwatched".
# "watched" means this interface was in a list passed to ifd.
WATCHED="${2}"

# Old and new status fields.  Each field is a comma-separated list of
# keywords:
#
# "up"/"down"
# Reflects the IFF_UP flag.
#
# "running"/"stopped"
# Reflects the IFF_RUNNING flag.
#
# "connected"/"disconnected"/"unknown"
# Indicates the link connection status, or "unknown" if this can't be
# determined.
#
# Alternatively either field may be the single keyword "unknown", e.g.
# when the driver for the interface is loaded or unloaded.
OLD_STATUS="${3}"
NEW_STATUS="${4}"

[ "${WATCHED}" = "watched" ] || exit 0

. "/usr/share/laptop-net/shared.sh"

case "${OLD_STATUS}:${NEW_STATUS}" in
up,*,connected:up,*,disconnected)
    bringdown disconnect
    ;;
down,*,disconnected:down,*,connected|unknown:down,*,connected)
    bringup connect
    ;;
esac

case "${NEW_STATUS}" in
up,*,*)
    echo "up" > ${STATE_FILE}
    ;;
down,*,*)
    echo "down" > ${STATE_FILE}
    ;;
esac

exit 0
