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