initial commit
[lnf] / lnf
1 #!/bin/bash
2 # Copyright (C) 2014 Ian Kelling
3 # This program is under GPL v. 3 or later, see <http://www.gnu.org/licenses/>
4
5 lnf() {
6 local help="lnf [--help] LN_ARGUMENTS...
7 Create symlinks conveniently.
8 Remove existing file/links using trash-put or rm -rf if it is not available.
9 Create directory if needed. Finally, ln -s -- LN_ARGUMENTS"
10 if [[ $1 == --help ]]; then
11 echo "$help"
12 return 0
13 fi
14
15 local remove x
16 if type -P dircolors >/dev/null; then
17 remove=trash-put
18 else
19 remove="rm -rf"
20 fi
21
22 if [[ $# -ge 3 && ! -d ${!#} ]]; then
23 mkdir -p "${!#}"
24 elif [[ $# -ge 2 && -d ${!#} ]]; then
25 local oldcwd=$PWD
26 cd ${!#} # last arg
27 for x in "${@:1:$(($#-1))}"; do # all but last arg
28 # remove any trailing slashes
29 x="${x%%+(/)}"
30 # remove any leading directory components
31 x="${x##*/}"
32 [[ -e "$x" || -L "$x" ]] && $remove "$x"
33 done
34 cd "$oldcwd"
35 elif [[ $# -eq 2 ]]; then
36 if [[ -e "$2" || -L "$2" ]]; then
37 $remove "$2"
38 elif [[ ! -d "$2/.." ]]; then
39 mkdir -p "$2/.."
40 fi
41 elif [[ $# -eq 1 ]]; then
42 [[ -e "${1##*/}" || -L "${1##*/}" ]] && $remove "${1##*/}"
43 fi
44 ln -s -- "$@"
45 }
46 lnf "$@"