aboutsummaryrefslogtreecommitdiff
path: root/bin/eds
blob: 1855fb345efd2fe7d8d4604a9ef129595b4ca251 (plain) (blame)
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
#!/usr/bin/env bash
# Create and edit executable scripts in a directory EDSPATH (defaults to ~/.local/bin)

# Give up completely if no BASH_VERSINFO (<2.0)
[ -n "$BASH_VERSINFO" ] || exit

# Process options, including detecting requests for help
declare -a opts
for arg ; do
    case $arg in
        --)
            shift
            break
            ;;
        -*)
            shift
            opts[${#opts[@]}]=$arg
            ;;
    esac
done

# Need at least one file after options are parsed out
if ! (($#)) ; then
    printf >&2 'eds: Need at least one script name\n'
    exit 2
fi

# Create the script directory if it doesn't exist yet
edspath=${EDSPATH:-$HOME/.local/bin}
if [[ ! -d $edspath ]] ; then
    mkdir -p -- "$edspath" || exit
fi

# Create a new array with the script directory prepended to the given names
declare -a files
files=("${@/#/"$edspath"/}")

# Collect the names of any scripts that don't exist yet so we can make them
# executable after we're done editing
declare -a creations
for file in "${files[@]}" ; do
    [[ -e $file ]] && continue
    creations[${#creations[@]}]=$file
done

# Run the editor; if EDITOR isn't set, use vi(1)
"${EDITOR:-vi}" "${opts[@]}" -- "${files[@]}"

# Make any created scripts executable if they now appear to be files
for creation in "${creations[@]}" ; do
    [[ -f $creation ]] || continue
    chmod +x -- "$creation"
done