aboutsummaryrefslogtreecommitdiffstats
path: root/ves-unset.sh
blob: d25a178f04b676200c3c8def791bb47428570148 (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
56
57
#!/bin/sh
#
# Remove a variable from an sh-ves environment entirely; the inverse of
# ves export. If the target environment is currently active, the live
# variable is restored to its pre-activation value, just as ves deactivate
# would do.
#
ves_unset() {
    env=""
    case $1 in
        --env=*)
            env="${1#--env=}"
            shift
            ;;
    esac

    if [ "$#" -lt 1 ]; then
        printf "ERROR: No variable specified. usage: ves unset [--env=<name>] <variable>\n" >&2
        return 2
    fi

    var="$1"

    if ! env=$(_shves_resolve_env "$env"); then
        return 1
    fi

    if ! _shves_check_var_name "$var"; then
        return 1
    fi

    fname="$SHVES_ENV_DIR/$env"
    if ! _shves_has_var "$fname" "$var"; then
        printf "ERROR: Variable [%s] does not exist in environment [%s].\n" "$var" "$env" >&2
        return 1
    fi

    _shves_del_var "$fname" "$var"

    # If the target environment is active and the variable was part of the
    # activation save-state, restore the pre-activation value in the live
    # shell and drop the variable from the bookkeeping.
    if [ "$env" = "$SHVES_ENV_NM" ] && _shves_is_saved "$var"; then
        _shves_restore_var "$var"

        new_saved=""
        for v in $SHVES_SAVED_VARS; do
            if [ "$v" != "$var" ]; then
                new_saved="$new_saved $v"
            fi
        done
        SHVES_SAVED_VARS="$new_saved"
        export SHVES_SAVED_VARS
    fi

    return 0
}