eeln.in 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/bash
  2. . <(ffoom init)
  3. FFOO_INIPATH="__FFOO_INIPATH__"
  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 [-i|--intermediate] [-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 ln.ini | expand_tilde | debug_pipe out
  40. }
  41. while true;
  42. do
  43. case $1 in
  44. -d|--debug)
  45. FFOO_DEBUG=true
  46. shift
  47. ;;
  48. -f|--force)
  49. FORCE=true
  50. shift
  51. ;;
  52. -i|--intermediate)
  53. intermediate=$2
  54. shift 2
  55. ;;
  56. -v|--verbose)
  57. FFOO_VERBOSE=true
  58. shift
  59. ;;
  60. -t|--target)
  61. target=$2
  62. shift 2
  63. ;;
  64. "")
  65. usage
  66. ;;
  67. *)
  68. src=$1
  69. name=$2
  70. break
  71. ;;
  72. esac
  73. done
  74. test -z "$target" && target=$(load target)
  75. test -z "$intermediate" && intermediate=$(load intermediate)
  76. test -z "$prefix" && prefix=$(load prefix)
  77. test -z "$target" && target=$DEFAULT_TARGET
  78. case $prefix.$name in
  79. .) die "provide at least name or set prefix in ln.ini";;
  80. $prefix.) name=$prefix;;
  81. .$name) :;;
  82. *) name=$prefix.$name;;
  83. esac
  84. debug -v name intermediate target
  85. mkdir -p $target
  86. if test -n "$intermediate";
  87. then
  88. mkdir -p $intermediate
  89. link $src $intermediate/$name
  90. link $intermediate/$name $target/$name
  91. else
  92. link $src $target/$name
  93. fi