123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/bash
  2. . <(ffoom init)
  3. FFOO_INI_PATH="__FFOO_INI_PATH__"
  4. ffoo import core
  5. ffoo import ini
  6. DEFAULT_TARGET=$HOME
  7. TRY_RELPATH=true
  8. FORCE=false
  9. PY_SCRIPT="
  10. import os.path
  11. import sys
  12. src, dst = sys.argv[1:]
  13. r = os.path.relpath(src, dst)
  14. a = os.path.abspath(src)
  15. if len(a) < len (r):
  16. print a
  17. else:
  18. print r
  19. "
  20. usage() {
  21. local u
  22. u="[-d|--debug] [-f|--force] [-v|--verbose]"
  23. u="$u [-t|--target TARGET] SOURCE [NAME]"
  24. usage_is "$u"
  25. }
  26. link() {
  27. local src=$1
  28. local dst=$2
  29. debug -v src dst
  30. if $TRY_RELPATH;
  31. then
  32. src=$(python <(echo "$PY_SCRIPT") "$src" "$(dirname $dst)")
  33. fi
  34. think "linking $src to $dst"
  35. rm -f $dst
  36. ln -s $src $dst
  37. }
  38. load() {
  39. iniread -p ln.dir.$1 | expand_tilde | debug_pipe out
  40. }
  41. while true; do case $1 in
  42. -d|--debug) FFOO_DEBUG=true; shift ;;
  43. -f|--force) FORCE=true; shift ;;
  44. -v|--verbose) FFOO_VERBOSE=true; shift ;;
  45. -t|--target) target=$2 shift 2 ;;
  46. "") usage ;;
  47. *) src=$1; name=$2; break ;;
  48. esac done
  49. test -z "$target" && target=$(load target)
  50. test -z "$prefix" && prefix=$(load prefix)
  51. test -z "$target" && target=$DEFAULT_TARGET
  52. case $prefix.$name in
  53. .) die "provide at least name or set prefix in ln.ini";;
  54. $prefix.) name=$prefix;;
  55. .$name) :;;
  56. *) name=$prefix.$name;;
  57. esac
  58. debug -v name target
  59. mkdir -p $target
  60. link $src $target/$name