29f137df5f89acaa63075826a3801c2cf5333569
2 # Copyright (C) 2014 Ian Kelling
3 # This program is under GPL v. 3 or later, see <http://www.gnu.org/licenses/>
6 local help="lnf - Create symlinks conveniently and forcefully.
8 lnf -T TARGET LINK_NAME (1st form)
10 lnf TARGET... DIRECTORY (3rd form)
12 Remove existing file in the using trash-put or rm -rf if it is not available.
13 Create directory if needed. Slightly more restrictive arguments than ln.
15 In the 1st form, create a link to TARGET with the name LINK_NAME. In the 2nd
16 form, create a link to TARGET in the current directory. In the 3rd form, create
17 links to each TARGET in DIRECTORY."
19 if [[ $1 == --help ||
$# -eq 0 ]]; then
25 if [[ $1 == -T ]]; then
28 if (( $# != 2 )); then
29 echo "lnf error: expected 2 arguments with -T flag. Got $#"
34 local reset_extglob
=false
35 ! shopt extglob
>/dev
/null
&& reset_extglob
=true
40 if type -P trash-put
>/dev
/null
; then
47 if [[ -e "$2" ||
-L "$2" ]]; then
49 elif ! mkdir
-p "$(dirname "$2")"; then
50 echo "lnf error: failed to make directory $(dirname "$2")"
53 elif (( $# >= 2 )); then
54 if [[ -d ${!#} ]]; then
57 for x
in "${@:1:$(( $# - 1 ))}"; do # all but last arg
58 # remove any trailing slashes, uses extglob
60 # remove any leading directory components
62 [[ -e "$x" ||
-L "$x" ]] && $remove "$x"
66 if ! mkdir
-p "${!#}"; then
67 echo "lnf error: failed to make directory ${!#}"
71 elif [[ $# -eq 1 ]]; then
72 [[ -e "${1##*/}" || -L "${1##*/}" ]] && $remove "${1##*/}"
75 $reset_extglob && shopt -u extglob