dpkg-maintscript-helper.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. #!/bin/sh
  2. #
  3. # Copyright © 2007,2011-2013 Guillem Jover <guillem@debian.org>
  4. # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
  5. # Copyright © 2008 Joey Hess <joeyh@debian.org>
  6. # Copyright © 2005 Scott James Remnant (original implementation on www.dpkg.org)
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. # The conffile related functions are inspired by
  21. # https://wiki.debian.org/DpkgConffileHandling
  22. # This script is documented in dpkg-maintscript-helper(1)
  23. ##
  24. ## Functions to remove an obsolete conffile during upgrade
  25. ##
  26. rm_conffile() {
  27. local CONFFILE="$1"
  28. local LASTVERSION="$2"
  29. local PACKAGE="$3"
  30. if [ "$LASTVERSION" = "--" ]; then
  31. LASTVERSION=""
  32. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  33. fi
  34. if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  35. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  36. fi
  37. # Skip remaining parameters up to --
  38. while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
  39. [ $# -gt 0 ] || badusage "missing arguments after --"
  40. shift
  41. [ -n "$PACKAGE" ] || error "couldn't identify the package"
  42. [ -n "$1" ] || error "maintainer script parameters are missing"
  43. [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  44. error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  45. debug "Executing $0 rm_conffile in $DPKG_MAINTSCRIPT_NAME" \
  46. "of $DPKG_MAINTSCRIPT_PACKAGE"
  47. debug "CONFFILE=$CONFFILE PACKAGE=$PACKAGE" \
  48. "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  49. case "$DPKG_MAINTSCRIPT_NAME" in
  50. preinst)
  51. if [ "$1" = "install" -o "$1" = "upgrade" ] && [ -n "$2" ] &&
  52. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  53. prepare_rm_conffile "$CONFFILE" "$PACKAGE"
  54. fi
  55. ;;
  56. postinst)
  57. if [ "$1" = "configure" ] && [ -n "$2" ] &&
  58. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  59. finish_rm_conffile "$CONFFILE"
  60. fi
  61. ;;
  62. postrm)
  63. if [ "$1" = "purge" ]; then
  64. rm -f "$CONFFILE.dpkg-bak" "$CONFFILE.dpkg-remove" \
  65. "$CONFFILE.dpkg-backup"
  66. fi
  67. if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  68. [ -n "$2" ] &&
  69. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  70. abort_rm_conffile "$CONFFILE" "$PACKAGE"
  71. fi
  72. ;;
  73. *)
  74. debug "$0 rm_conffile not required in $DPKG_MAINTSCRIPT_NAME"
  75. ;;
  76. esac
  77. }
  78. prepare_rm_conffile() {
  79. local CONFFILE="$1"
  80. local PACKAGE="$2"
  81. [ -e "$CONFFILE" ] || return 0
  82. ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
  83. local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
  84. local old_md5sum="$(dpkg-query -W -f='${Conffiles}' $PACKAGE | \
  85. sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
  86. if [ "$md5sum" != "$old_md5sum" ]; then
  87. echo "Obsolete conffile $CONFFILE has been modified by you."
  88. echo "Saving as $CONFFILE.dpkg-bak ..."
  89. mv -f "$CONFFILE" "$CONFFILE.dpkg-backup"
  90. else
  91. echo "Moving obsolete conffile $CONFFILE out of the way..."
  92. mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
  93. fi
  94. }
  95. finish_rm_conffile() {
  96. local CONFFILE="$1"
  97. if [ -e "$CONFFILE.dpkg-backup" ]; then
  98. mv -f "$CONFFILE.dpkg-backup" "$CONFFILE.dpkg-bak"
  99. fi
  100. if [ -e "$CONFFILE.dpkg-remove" ]; then
  101. echo "Removing obsolete conffile $CONFFILE ..."
  102. rm -f "$CONFFILE.dpkg-remove"
  103. fi
  104. }
  105. abort_rm_conffile() {
  106. local CONFFILE="$1"
  107. local PACKAGE="$2"
  108. ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
  109. if [ -e "$CONFFILE.dpkg-remove" ]; then
  110. echo "Reinstalling $CONFFILE that was moved away"
  111. mv "$CONFFILE.dpkg-remove" "$CONFFILE"
  112. fi
  113. if [ -e "$CONFFILE.dpkg-backup" ]; then
  114. echo "Reinstalling $CONFFILE that was backupped"
  115. mv "$CONFFILE.dpkg-backup" "$CONFFILE"
  116. fi
  117. }
  118. ##
  119. ## Functions to rename a conffile during upgrade
  120. ##
  121. mv_conffile() {
  122. local OLDCONFFILE="$1"
  123. local NEWCONFFILE="$2"
  124. local LASTVERSION="$3"
  125. local PACKAGE="$4"
  126. if [ "$LASTVERSION" = "--" ]; then
  127. LASTVERSION=""
  128. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  129. fi
  130. if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  131. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  132. fi
  133. # Skip remaining parameters up to --
  134. while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
  135. [ $# -gt 0 ] || badusage "missing arguments after --"
  136. shift
  137. [ -n "$PACKAGE" ] || error "couldn't identify the package"
  138. [ -n "$1" ] || error "maintainer script parameters are missing"
  139. [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  140. error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  141. debug "Executing $0 mv_conffile in $DPKG_MAINTSCRIPT_NAME" \
  142. "of $DPKG_MAINTSCRIPT_PACKAGE"
  143. debug "CONFFILE=$OLDCONFFILE -> $NEWCONFFILE PACKAGE=$PACKAGE" \
  144. "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  145. case "$DPKG_MAINTSCRIPT_NAME" in
  146. preinst)
  147. if [ "$1" = "install" -o "$1" = "upgrade" ] && [ -n "$2" ] &&
  148. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  149. prepare_mv_conffile "$OLDCONFFILE" "$PACKAGE"
  150. fi
  151. ;;
  152. postinst)
  153. if [ "$1" = "configure" ] && [ -n "$2" ] &&
  154. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  155. finish_mv_conffile "$OLDCONFFILE" "$NEWCONFFILE" "$PACKAGE"
  156. fi
  157. ;;
  158. postrm)
  159. if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  160. [ -n "$2" ] &&
  161. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  162. abort_mv_conffile "$OLDCONFFILE" "$PACKAGE"
  163. fi
  164. ;;
  165. *)
  166. debug "$0 mv_conffile not required in $DPKG_MAINTSCRIPT_NAME"
  167. ;;
  168. esac
  169. }
  170. prepare_mv_conffile() {
  171. local CONFFILE="$1"
  172. local PACKAGE="$2"
  173. [ -e "$CONFFILE" ] || return 0
  174. ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
  175. local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
  176. local old_md5sum="$(dpkg-query -W -f='${Conffiles}' $PACKAGE | \
  177. sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
  178. if [ "$md5sum" = "$old_md5sum" ]; then
  179. mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
  180. fi
  181. }
  182. finish_mv_conffile() {
  183. local OLDCONFFILE="$1"
  184. local NEWCONFFILE="$2"
  185. local PACKAGE="$3"
  186. rm -f $OLDCONFFILE.dpkg-remove
  187. [ -e "$OLDCONFFILE" ] || return 0
  188. ensure_package_owns_file "$PACKAGE" "$OLDCONFFILE" || return 0
  189. echo "Preserving user changes to $NEWCONFFILE (renamed from $OLDCONFFILE)..."
  190. mv -f "$NEWCONFFILE" "$NEWCONFFILE.dpkg-new"
  191. mv -f "$OLDCONFFILE" "$NEWCONFFILE"
  192. }
  193. abort_mv_conffile() {
  194. local CONFFILE="$1"
  195. local PACKAGE="$2"
  196. ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
  197. if [ -e "$CONFFILE.dpkg-remove" ]; then
  198. echo "Reinstalling $CONFFILE that was moved away"
  199. mv "$CONFFILE.dpkg-remove" "$CONFFILE"
  200. fi
  201. }
  202. ##
  203. ## Functions to replace a symlink with a directory
  204. ##
  205. symlink_to_dir() {
  206. local SYMLINK="$1"
  207. local SYMLINK_TARGET="$2"
  208. local LASTVERSION="$3"
  209. local PACKAGE="$4"
  210. if [ "$LASTVERSION" = "--" ]; then
  211. LASTVERSION=""
  212. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  213. fi
  214. if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  215. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  216. fi
  217. # Skip remaining parameters up to --
  218. while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
  219. [ $# -gt 0 ] || badusage "missing arguments after --"
  220. shift
  221. [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  222. error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  223. [ -n "$PACKAGE" ] || error "cannot identify the package"
  224. [ -n "$SYMLINK" ] || error "symlink parameter is missing"
  225. [ -n "$SYMLINK_TARGET" ] || error "original symlink target is missing"
  226. [ -n "$1" ] || error "maintainer script parameters are missing"
  227. debug "Executing $0 symlink_to_dir in $DPKG_MAINTSCRIPT_NAME" \
  228. "of $DPKG_MAINTSCRIPT_PACKAGE"
  229. debug "SYMLINK=$SYMLINK -> $SYMLINK_TARGET PACKAGE=$PACKAGE" \
  230. "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  231. case "$DPKG_MAINTSCRIPT_NAME" in
  232. preinst)
  233. if [ "$1" = "install" -o "$1" = "upgrade" ] &&
  234. [ -n "$2" ] && [ -h "$SYMLINK" ] &&
  235. [ "$(readlink -f $SYMLINK)" = "$SYMLINK_TARGET" ] &&
  236. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  237. mv -f "$SYMLINK" "${SYMLINK}.dpkg-backup"
  238. fi
  239. ;;
  240. postinst)
  241. # We cannot bail depending on the version, as here we only
  242. # know what was the last configured version, and we might
  243. # have been unpacked, then upgraded with an unpack and thus
  244. # never been configured before.
  245. if [ "$1" = "configure" ] && [ -h "${SYMLINK}.dpkg-backup" ] &&
  246. [ "$(readlink -f ${SYMLINK}.dpkg-backup)" = "$SYMLINK_TARGET" ]
  247. then
  248. rm -f "${SYMLINK}.dpkg-backup"
  249. fi
  250. ;;
  251. postrm)
  252. if [ "$1" = "purge" ] && [ -h "${SYMLINK}.dpkg-backup" ]; then
  253. rm -f "${SYMLINK}.dpkg-backup"
  254. fi
  255. if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  256. [ -n "$2" ] &&
  257. [ ! -e "$SYMLINK" ] && [ -h "${SYMLINK}.dpkg-backup" ] &&
  258. [ "$(readlink -f ${SYMLINK}.dpkg-backup)" = "$SYMLINK_TARGET" ] &&
  259. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  260. echo "Restoring backup of $SYMLINK ..."
  261. mv "${SYMLINK}.dpkg-backup" "$SYMLINK"
  262. fi
  263. ;;
  264. *)
  265. debug "$0 symlink_to_dir not required in $DPKG_MAINTSCRIPT_NAME"
  266. ;;
  267. esac
  268. }
  269. ##
  270. ## Functions to replace a directory with a symlink
  271. ##
  272. dir_to_symlink() {
  273. local PATHNAME="$1"
  274. local SYMLINK_TARGET="$2"
  275. local LASTVERSION="$3"
  276. local PACKAGE="$4"
  277. if [ "$LASTVERSION" = "--" ]; then
  278. LASTVERSION=""
  279. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  280. fi
  281. if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  282. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  283. fi
  284. # Skip remaining parameters up to --
  285. while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
  286. [ $# -gt 0 ] || badusage "missing arguments after --"
  287. shift
  288. [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  289. error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  290. [ -n "$PACKAGE" ] || error "cannot identify the package"
  291. [ -n "$PATHNAME" ] || error "directory parameter is missing"
  292. [ -n "$SYMLINK_TARGET" ] || error "new symlink target is missing"
  293. [ -n "$1" ] || error "maintainer script parameters are missing"
  294. debug "Executing $0 dir_to_symlink in $DPKG_MAINTSCRIPT_NAME" \
  295. "of $DPKG_MAINTSCRIPT_PACKAGE"
  296. debug "PATHNAME=$PATHNAME SYMLINK_TARGET=$SYMLINK_TARGET" \
  297. "PACKAGE=$PACKAGE LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  298. case "$DPKG_MAINTSCRIPT_NAME" in
  299. preinst)
  300. if [ "$1" = "install" -o "$1" = "upgrade" ] &&
  301. [ -n "$2" ] &&
  302. [ ! -h "$PATHNAME" ] && [ -d "$PATHNAME" ] &&
  303. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  304. prepare_dir_to_symlink "$PACKAGE" "$PATHNAME"
  305. fi
  306. ;;
  307. postinst)
  308. # We cannot bail depending on the version, as here we only
  309. # know what was the last configured version, and we might
  310. # have been unpacked, then upgraded with an unpack and thus
  311. # never been configured before.
  312. if [ "$1" = "configure" ] &&
  313. [ -d "${PATHNAME}.dpkg-backup" ] &&
  314. [ ! -h "$PATHNAME" ] && [ -d "$PATHNAME" ] &&
  315. [ -f "$PATHNAME/.dpkg-staging-dir" ]; then
  316. finish_dir_to_symlink "$PATHNAME" "$SYMLINK_TARGET"
  317. fi
  318. ;;
  319. postrm)
  320. if [ "$1" = "purge" ] && [ -d "${PATHNAME}.dpkg-backup" ]; then
  321. rm -rf "${PATHNAME}.dpkg-backup"
  322. fi
  323. if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  324. [ -n "$2" ] &&
  325. [ -d "${PATHNAME}.dpkg-backup" ] &&
  326. [ \( ! -h "$PATHNAME" -a -d "$PATHNAME" -a \
  327. -f "$PATHNAME/.dpkg-staging-dir" \) -o \
  328. \( -h "$PATHNAME" -a \
  329. "$(readlink -f $PATHNAME)" = "$SYMLINK_TARGET" \) ] &&
  330. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  331. abort_dir_to_symlink "$PATHNAME"
  332. fi
  333. ;;
  334. *)
  335. debug "$0 dir_to_symlink not required in $DPKG_MAINTSCRIPT_NAME"
  336. ;;
  337. esac
  338. }
  339. prepare_dir_to_symlink()
  340. {
  341. local PACKAGE="$1"
  342. local PATHNAME="$2"
  343. # If there are conffiles we should not perform the switch.
  344. if dpkg-query -W -f='${Conffiles}' "$PACKAGE" | \
  345. grep -q "$PATHNAME/."; then
  346. error "directory '$PATHNAME' contains conffiles," \
  347. "cannot switch to symlink"
  348. fi
  349. # If there are locally created files or files owned by another package
  350. # we should not perform the switch.
  351. find "$PATHNAME" -print0 | xargs -0 -n1 sh -c '
  352. package="$1"
  353. file="$2"
  354. if ! dpkg-query -L "$package" | grep -q -x "$file"; then
  355. exit 1
  356. fi
  357. exit 0
  358. ' check-files-ownership "$PACKAGE" || \
  359. error "directory '$PATHNAME' contains files not owned by" \
  360. "package $PACKAGE, cannot switch to symlink"
  361. # At this point, we know that the directory either contains no files,
  362. # or only non-conffiles owned by the package.
  363. #
  364. # To do the switch we cannot simply replace it with the final symlink
  365. # just yet, because dpkg needs to remove any file present in the old
  366. # package that have disappeared in the new one, and we do not want to
  367. # lose files resolving to the same pathname in the symlink target.
  368. #
  369. # We cannot replace the directory with a staging symlink either,
  370. # because dpkg will update symlinks to their new target.
  371. #
  372. # So we need to create a staging directory, to avoid removing files
  373. # from other packages, and to trap any new files in the directory
  374. # to move them to their correct place later on.
  375. mv -f "$PATHNAME" "${PATHNAME}.dpkg-backup"
  376. mkdir "$PATHNAME"
  377. # Mark it as a staging directory, so that we can track things.
  378. touch "$PATHNAME/.dpkg-staging-dir"
  379. }
  380. finish_dir_to_symlink()
  381. {
  382. local PATHNAME="$1"
  383. local SYMLINK_TARGET="$2"
  384. # Move the contents of the staging directory to the symlink target,
  385. # as those are all new files installed between this package being
  386. # unpacked and configured.
  387. rm "$PATHNAME/.dpkg-staging-dir"
  388. find "$PATHNAME" -mindepth 1 -print0 | \
  389. xargs -0 -i% mv -f "%" "$SYMLINK_TARGET/"
  390. # Remove the staging directory.
  391. rmdir "$PATHNAME"
  392. # Do the actual switch.
  393. ln -s "$SYMLINK_TARGET" "$PATHNAME"
  394. # We are left behind the old files owned by this package in the backup
  395. # directory, just remove it.
  396. rm -rf "${PATHNAME}.dpkg-backup"
  397. }
  398. abort_dir_to_symlink()
  399. {
  400. local PATHNAME="$1"
  401. echo "Restoring backup of $PATHNAME ..."
  402. if [ -h "$PATHNAME" ]; then
  403. rm -f "$PATHNAME"
  404. else
  405. # The staging directory must be empty, as no other package
  406. # should have been unpacked inbetween.
  407. rm -f "$PATHNAME/.dpkg-staging-dir"
  408. rmdir "$PATHNAME"
  409. fi
  410. mv "${PATHNAME}.dpkg-backup" "$PATHNAME"
  411. }
  412. # Common functions
  413. ensure_package_owns_file() {
  414. local PACKAGE="$1"
  415. local FILE="$2"
  416. if ! dpkg-query -L "$PACKAGE" | grep -q -x "$FILE"; then
  417. debug "File '$FILE' not owned by package " \
  418. "'$PACKAGE', skipping $command"
  419. return 1
  420. fi
  421. return 0
  422. }
  423. debug() {
  424. if [ -n "$DPKG_DEBUG" ]; then
  425. echo "DEBUG: $PROGNAME: $*" >&2
  426. fi
  427. }
  428. error() {
  429. echo "$PROGNAME: error: $*" >&2
  430. exit 1
  431. }
  432. warning() {
  433. echo "$PROGNAME: warning: $*" >&2
  434. }
  435. usage() {
  436. cat <<END
  437. Usage: $PROGNAME <command> <parameter>... -- <maintainer-script-parameter>...
  438. Commands:
  439. supports <command>
  440. Returns 0 (success) if the given command is supported, 1 otherwise.
  441. rm_conffile <conffile> [<last-version> [<package>]]
  442. Remove obsolete conffile. Must be called in preinst, postinst and
  443. postrm.
  444. mv_conffile <old-conf> <new-conf> [<last-version> [<package>]]
  445. Rename a conffile. Must be called in preinst, postinst and postrm.
  446. symlink_to_dir <pathname> <old-symlink-target> [<last-version> [<package>]]
  447. Replace a symlink with a directory. Must be called in preinst,
  448. postinst and postrm.
  449. dir_to_symlink <pathname> <new-symlink-target> [<last-version> [<package>]]
  450. Replace a directory with a symlink. Must be called in preinst,
  451. postinst and postrm.
  452. help
  453. Display this usage information.
  454. END
  455. }
  456. badusage() {
  457. echo "$PROGNAME: error: $1" >&2
  458. echo >&2
  459. echo "Use '$PROGNAME help' for program usage information." >&2
  460. exit 1
  461. }
  462. # Main code
  463. set -e
  464. PROGNAME=$(basename $0)
  465. version="unknown"
  466. command="$1"
  467. [ $# -gt 0 ] || badusage "missing command"
  468. shift
  469. case "$command" in
  470. supports)
  471. case "$1" in
  472. rm_conffile|mv_conffile|symlink_to_dir|dir_to_symlink)
  473. code=0
  474. ;;
  475. *)
  476. code=1
  477. ;;
  478. esac
  479. if [ -z "$DPKG_MAINTSCRIPT_NAME" ]; then
  480. warning "environment variable DPKG_MAINTSCRIPT_NAME missing"
  481. code=1
  482. fi
  483. if [ -z "$DPKG_MAINTSCRIPT_PACKAGE" ]; then
  484. warning "environment variable DPKG_MAINTSCRIPT_PACKAGE missing"
  485. code=1
  486. fi
  487. exit $code
  488. ;;
  489. rm_conffile)
  490. rm_conffile "$@"
  491. ;;
  492. mv_conffile)
  493. mv_conffile "$@"
  494. ;;
  495. symlink_to_dir)
  496. symlink_to_dir "$@"
  497. ;;
  498. dir_to_symlink)
  499. dir_to_symlink "$@"
  500. ;;
  501. --help|help|-?)
  502. usage
  503. ;;
  504. --version)
  505. cat <<-END
  506. Debian $PROGNAME version $version.
  507. This is free software; see the GNU General Public License version 2 or
  508. later for copying conditions. There is NO warranty.
  509. END
  510. ;;
  511. *)
  512. badusage "command $command is unknown
  513. Hint: upgrading dpkg to a newer version might help."
  514. esac
  515. exit 0