+ local help="Usage:
+ lnf [OPTIONS] -T TARGET LINK_NAME (1st form)
+ lnf [OPTIONS] TARGET (2nd form)
+ lnf [OPTIONS] TARGET... DIRECTORY (3rd form)
+Create symlinks forcefully
+
+If the link already exists, make it's ownership be the same as if it was
+newly created (only chown if we are root). Removes existing files using
+rm -rf. Create directory of link if needed. Slightly more restrictive
+arguments than ln.
+
+In the 1st form, create a link to TARGET with the name LINK_NAME. In the 2nd
+form, create a link to TARGET in the current directory. In the 3rd form, create
+links to each TARGET in DIRECTORY.
+
+-n|--dry-run Do verbose dry run.
+-v|--verbose Print commands which modify the filesystem.
+-h|--help Print help and exit.
+"
+
+
+ local temp nodir
+ local verbose=false
+ local dry_run=false
+ local do_exit=false
+ temp=$(getopt -l help,dry-run,verbose hnTv "$@") || usage 1
+ eval set -- "$temp"
+ while true; do
+ case $1 in
+ -n|--dry-run) dry_run=true; verbose=true; shift ;;
+ -T) nodir=-T; shift ;;
+ -v|--verbose) verbose=true; shift ;;
+ -h|--help) echo "$help"; return 0 ;;
+ --) shift; break ;;
+ *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
+ esac
+ done
+
+ if (( $# == 0 )); then
+ echo "$help"
+ return 1
+ fi
+
+ if [[ $nodir ]]; then
+ if (( $# != 2 )); then
+ echo "lnf: error: expected 2 arguments with -T flag. Got $#"
+ return 1
+ fi
+ fi
+
+ local reset_extglob=false
+ ! shopt extglob >/dev/null && reset_extglob=true
+ shopt -s extglob
+
+
+ local -a to_remove to_link
+ local x ret prefix dest_dir grp target
+ local mkdir=false
+
+ if [[ $nodir ]]; then
+ dest_file="$2"
+ dest_dir="$(dirname "$dest_file")"
+ _lnf_existing_link "$1" "$dest_file" "$dest_dir" || return $?
+ if $do_exit; then return 0; fi
+ if [[ ! -d $dest_dir ]]; then
+ mkdir=true
+ if [[ -e $dest_dir || -L $dest_dir ]]; then
+ to_remove+=("$dest_dir")
+ fi
+ fi
+ to_link+=("$dest_file")
+ elif (( $# >= 2 )); then
+ dest_dir="${!#}"
+ if [[ -d $dest_dir ]]; then
+ prefix="$dest_dir" # last arg
+ for target in "${@:1:$(( $# - 1 ))}"; do # all but last arg
+ # Remove 1 or more trailing slashes, using.
+ dest_file="${target%%+(/)}"
+ # remove any leading directory components, add prefix
+ dest_file="$prefix/${target##*/}"
+ _lnf_existing_link "$target" "$dest_file" "$dest_dir"
+ done