#! /bin/bash

. /usr/lib/netctl/globals
. "$SUBR_DIR/ip"

export INTERFACE="$1"
export ACTION="$2"
PROFILE_FILE="$STATE_DIR/netctl-auto-$INTERFACE.profile"

case $ACTION in
  CONNECTED)
    load_profile "$WPA_ID_STR"
    DhcpcdOptions+=" -K -L"
    ip_set || exit 1
    mkdir -p "$(dirname "$PROFILE_FILE")"
    printf '%s' "$Profile" > "$PROFILE_FILE"
    # Sandbox the eval
    if ! ( do_debug eval "$ExecUpPost" ); then
        # Failing ExecUpPost will take the connection down
        netctl-auto stop "$INTERFACE"
        exit 1
    fi
  ;;
  DISCONNECTED)
    if [[ -s "$PROFILE_FILE" ]]; then
        load_profile "$(< "$PROFILE_FILE")"
        rm -f "$PROFILE_FILE"
        # Sandbox the eval
        if ! ( do_debug eval "$ExecDownPre" ); then
            exit 1
        fi
        ip_unset
    fi
  ;;
esac


# vim: ft=sh ts=4 et sw=4:
