#!/bin/sh

# $Id: link-change.in,v 1.13 2003/07/08 18:02:08 cph Exp $
#
# Copyright 2000,2001,2002,2003 Massachusetts Institute of Technology
#
# This file is part of laptop-net.
#
# Laptop-net 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.
#
# Laptop-net 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 laptop-net; 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.

set -e

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

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

# Either "managed" or "unmanaged".
MANAGED="${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}"

[ "${MANAGED}" = "managed" ] || exit 0

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

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

exit 0
