update-alternatives.in 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/bin/sh
  2. # update-alternatives
  3. #
  4. # Copyright (C) 2001 Carl D. Worth
  5. #
  6. # This program was inspired by the Debian update-alternatives program
  7. # which is Copyright (C) 1995 Ian Jackson. This version of
  8. # update-alternatives is command-line compatible with Debian's for a
  9. # subset of the options, (only --install, --remove, and --help)
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2, or (at your option)
  14. # any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. set -e
  21. # admin dir
  22. ad="$OPKG_OFFLINE_ROOT@opkglibdir@/opkg/alternatives"
  23. usage() {
  24. echo "update-alternatives: $*
  25. Usage: update-alternatives --install <link> <name> <path> <priority>
  26. update-alternatives --remove <name> <path>
  27. update-alternatives --help
  28. <link> is the link pointing to the provided path (ie. /usr/bin/foo).
  29. <name> is the name in $ad/alternatives (ie. foo)
  30. <path> is the name referred to (ie. /usr/bin/foo-extra-spiffy)
  31. <priority> is an integer; options with higher numbers are chosen.
  32. " >&2
  33. exit 2
  34. }
  35. quit() {
  36. echo "update-alternatives: $*" >&2
  37. exit 2
  38. }
  39. register_alt() {
  40. [ $# -lt 2 ] && return 1
  41. local name="$1"
  42. local link="$2"
  43. if [ ! -d $ad ]; then
  44. mkdir -p $ad
  45. fi
  46. if [ -e "$ad/$name" ]; then
  47. local olink=`head -n 1 $ad/$name`
  48. if [ "$link" != "$olink" ]; then
  49. echo "update-alternatives: Error: cannot register alternative $name to $link since it is already registered to $olink" >&2
  50. return 1
  51. fi
  52. else
  53. echo "$link" > "$ad/$name"
  54. fi
  55. return 0
  56. }
  57. protect_slashes() {
  58. sed -e 's/\//\\\//g'
  59. }
  60. remove_alt() {
  61. [ $# -lt 2 ] && return 1
  62. local name="$1"
  63. local path="$2"
  64. [ ! -f $ad/$name ] && return 0
  65. path=`echo $path | protect_slashes`
  66. sed -ne "/^$path\>.*/!p" $ad/$name > $ad/$name.new
  67. mv $ad/$name.new $ad/$name
  68. }
  69. add_alt() {
  70. [ $# -lt 3 ] && return 1
  71. local name="$1"
  72. local path="$2"
  73. local priority="$3"
  74. remove_alt $name $path
  75. echo "$path $priority" >> $ad/$name
  76. }
  77. find_best_alt() {
  78. [ $# -lt 1 ] && return 1
  79. [ ! -f $ad/$name ] && return 0
  80. link=$OPKG_OFFLINE_ROOT/`head -n 1 $ad/$name`
  81. prio=`sed -ne "1!p" $ad/$name | sed -e "s/\(.*\) \(.*\)/\2 \1/g" | sort -nr | head -n 1 | sed 's/ [^ ]*$//'`
  82. if [ -z "$prio" ]; then
  83. echo "update-alternatives: removing $link as no more alternatives exist for it"
  84. rm $ad/$name
  85. if [ -L $link ]; then
  86. rm $link
  87. fi
  88. return 0
  89. fi
  90. ## Find last line with highest priority.
  91. path=`grep "${prio}$" $ad/$name | tail -n 1 | sed 's/ [^ ]*$//'`
  92. if [ ! -e $link -o -L $link ]; then
  93. local link_dir=`dirname $link`
  94. if [ ! -d $link_dir ]; then
  95. mkdir -p $link_dir
  96. fi
  97. if [ -h $link -a -d $link ]; then
  98. # If $link exists and the target is a directory,
  99. # 'ln -sf $path $link' doesn't replace the link to
  100. # that directory, it creates new link inside.
  101. echo "update-alternatives: Removing $link".
  102. rm -f $link
  103. fi
  104. ln -sf $path $link
  105. echo "update-alternatives: Linking $link to $path"
  106. else
  107. echo "update-alternatives: Error: not linking $link to $path since $link exists and is not a link"
  108. return 1
  109. fi
  110. return 0
  111. }
  112. do_install() {
  113. if [ $# -lt 4 ]; then
  114. usage "--install needs <link> <name> <path> <priority>"
  115. fi
  116. local link="$1"
  117. local name="$2"
  118. local path="$3"
  119. local priority="$4"
  120. path=`echo $path | sed 's|/\+|/|g'`
  121. # This is a bad hack, but I haven't thought of a cleaner solution yet...
  122. [ -n "$OPKG_OFFLINE_ROOT" ] && path=`echo $path | sed "s|^$OPKG_OFFLINE_ROOT/*|/|"`
  123. register_alt $name $link
  124. add_alt $name $path $priority
  125. find_best_alt $name
  126. }
  127. do_remove() {
  128. if [ $# -lt 2 ]; then
  129. usage "--remove needs <name> <path>"
  130. fi
  131. local name="$1"
  132. local path="$2"
  133. path=`echo $path | sed 's|/\+|/|g'`
  134. # This is a bad hack, but I haven't thought of a cleaner solution yet...
  135. [ -n "$OPKG_OFFLINE_ROOT" ] && path=`echo $path | sed "s|^$OPKG_OFFLINE_ROOT/*|/|"`
  136. remove_alt $name $path
  137. find_best_alt $name
  138. }
  139. ###
  140. # update-alternatives "main"
  141. ###
  142. while [ $# -gt 0 ]; do
  143. arg="$1"
  144. shift
  145. case $arg in
  146. --help)
  147. usage "help:"
  148. exit 0
  149. ;;
  150. --install)
  151. do_install $*
  152. exit $?
  153. ;;
  154. --remove)
  155. do_remove $*
  156. exit $?
  157. ;;
  158. *)
  159. usage "unknown argument \`$arg'"
  160. ;;
  161. esac
  162. done
  163. usage "at least one of --install or --remove must appear"
  164. exit 0