summaryrefslogtreecommitdiff
path: root/dot/local/bin/xchdir
blob: 574b1877f832dd93e060ef71109c249e29aa3cae (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
#!/bin/bash
err_script()
{
    if [ "$2" ]; then
        msg="Error: could not change to directory '$1': $2"
    else
        msg="Error: could not change to directory '$1'."
    fi
    printf "printf '%s\n' %q\n" "$msg"
    cat <<EOF
echo Press enter to quit.
read
EOF
}

target=$1
errmsg=
case "$target" in
    file:///) target=${target#file://} ;;
esac

try_chdir()
{
    if [ -d "$target" ]; then
        cd "$target"
    elif [ -e "$target" ]; then
        cd "$(dirname "$target")"
    else
        errmsg='does not exist'
        false
    fi
}

if try_chdir; then
    exec xterm
else
    err_script=$(err_script "$target" "$errmsg")
    exec xterm -e "$err_script"
fi