summaryrefslogtreecommitdiffstatshomepage
path: root/scripts/td.sh
blob: 44ca0abe56a2148ba2846f03909593846f038b54 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/sh

SCRIPT="$(basename $0)"

help() {
    cat <<-EOF
$SCRIPT
Toby Vincent <tobyv13@gmail.com>

$SCRIPT is a script for managing and navigating a persistent temp directory. In
order to function properly, this script must be sourced in the currently shell.
Consider setting an alias such as

    alias td=". $SCRIPT"

USAGE:
    $SCRIPT [OPTIONS] <COMMAND>
    $SCRIPT [OPTIONS]

OPTIONS:
    -h, --help      Display this message
    -f, --force     Use the --force argument when removing TD

ARGS:
    toggle (default)
        Switch between TD_ORIGIN and TD. This will create TD if it does not
        exist. This is the default if no command is provided.

    new | create      
        Creates a new temp directory and cds into it it stores the path
        to the temp directory in TD, and the path to the previous directory in 
        TD_ORIGIN.

    rm | remove
        Removes the directory stored in TD unsets TD and TD_ORIGIN.

    ls | list | show
        Shows the current values for TD and TD_ORIGIN
EOF
}

echo_err() {
    echo >&2 "$SCRIPT: $@"
}

rm_args="-r"
while test $# -gt 0; do
    case $1 in
    --help | -h)
        help
        return 0
        ;;
    --force | -f)
        rm_args="-rf"
        shift
        ;;
    *) break ;;

    esac
done

show() {
    if [ ! -n "$TD" ]; then
        echo_err "Not set"
        return 1
    fi
    echo "TD=$TD"
    echo "TD_ORIGIN=$TD_ORIGIN"
}

remove() {
    if [ ! -n "$TD" ]; then
        echo_err "Not set"
        return 1
    fi

    rm "$rm_args" "$TD"

    echo "removed $TD"

    if [ "$PWD" = "$TD" ]; then
        cd "$TD_ORIGIN"
    fi

    unset TD
    unset TD_ORIGIN
}

create() {
    remove 2>/dev/null

    td=$(mktemp -d)

    export TD="$td"
    export TD_ORIGIN="$PWD"

    echo "created $TD"
    cd "$TD"
}

toggle() {
    if [ "$PWD" = "$TD" ]; then
        cd "$TD_ORIGIN"
    elif [ -n "$TD" ]; then
        cd "$TD"
    else
        create
    fi
}

case $1 in
ls | list | show)
    show
    ;;
rm | remove)
    remove
    ;;
new | create)
    create
    ;;
* | toggle)
    toggle
    ;;
esac