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