summaryrefslogtreecommitdiffstats
path: root/src/ifplugd.action
blob: 4acb45b4887d0dcf3ae4fba460778344550ebedf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
#
# ifplugd.action script for netctl

. /usr/lib/network/globals

PROFILE_FILE="$STATE_DIR/ifplugd_$1.profile"

case "$2" in
  up)
    # Look for a dhcp based profile to try first
    # dhcp can actually outright fail, whereas
    # it's difficult to tell if static succeeded
    # Also check profile is same iface and is right connection
    echo "up"
    declare -a preferred_profiles
    declare -a dhcp_profiles
    declare -a static_profiles
    while read -r profile; do
      (
        echo "Reading profile '$profile'"
        source "$PROFILE_DIR/$profile"
        [[ "$Interface" == "$1" && "$Connection" == "ethernet" ]] || continue
        is_yes "${AutoWired:-no}" && exit 1 # user preferred AUTO profile
        [[ "$IP" == "dhcp" ]] && exit 2 # dhcp profile
        exit 3 # static profile
      )
      case $? in
        1) preferred_profiles+=("$profile");;
        2) dhcp_profiles+=("$profile");;
        3) static_profiles+=("$profile");;
      esac
    done < <(list_profiles)
    if [[ ${#preferred_profiles[@]} > 1 ]]; then
      echo "AutoWired flag for '$1' set in more than one profile (${preferred_profiles[*]})"
    fi
    for profile in "${preferred_profiles[@]}" "${dhcp_profiles[@]}" "${static_profiles[@]}"; do
      if "$SUBR_DIR/nework" start "$profile"; then
        mkdir -p "$(dirname "$PROFILE_FILE")"
        printf "%s" "$profile" > "$PROFILE_FILE"
        exit 0
      fi
    done
  ;;
  down)
    if [[ -e "$PROFILE_FILE" ]]; then
      "$SUBR_DIR/nework" stop "$(< "$PROFILE_FILE")" && rm -f "$PROFILE_FILE"
    fi
  ;;
  *)
    echo "Wrong arguments" > /dev/stderr
  ;;
esac

exit 1