Advertisement
Guest User

conpile

a guest
Apr 23rd, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.31 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. if (( UID != 0 )); then
  4.     echo >&2 "E: you must be root in order to run this script."
  5.     exit 1
  6. fi
  7.  
  8. usage() {
  9.     cat <<EOF
  10. USAGE: ${0##*/} [-a | -i | -n] [-d DEBIAN_DIR]
  11.     OPTIONS:
  12.         -a: install already installed packages only (default)
  13.         -i: install all non-debug packages plus already installed debug ones
  14.         -n: don't install anything
  15.         -d DEBIAN_DIR:
  16.           specify an alternative debian/ directory. It will be copied
  17.           to the current directory and the copy will be removed after build.
  18.           If './debian' already exists, it will be renamed to './debian.old'
  19.           (and renamed back after removing the copy). If it already exists too,
  20.           you will be asked to resolve that manually.
  21. EOF
  22.     exit 2
  23. }
  24.  
  25. DEBIAN_DIR=
  26. INSTALL_POLICY=a
  27. while getopts 'aind:' option; do
  28.     case "$option" in
  29.         [ain]) INSTALL_POLICY=$option ;;
  30.         d) DEBIAN_DIR=$OPTARG ;;
  31.         *) usage ;;
  32.     esac
  33. done
  34. shift "$(( OPTIND - 1 ))" || usage
  35. if (( $# != 0 )); then
  36.     echo >&2 "E: unexpected positional argument."
  37.     usage
  38. fi
  39.  
  40. is_installed() {
  41.     dpkg-query -s -- "$1" 2>/dev/null | grep -q '^Status: .* installed$'
  42. }
  43. case "$INSTALL_POLICY" in
  44.     a)
  45.         should_be_installed() {
  46.             is_installed "$1"
  47.         }
  48.         ;;
  49.     i)
  50.         should_be_installed() {
  51.             [[ $1 != *-dbg ]] || is_installed "$1"
  52.         }
  53.         ;;
  54.     n)
  55.         should_be_installed() {
  56.             false
  57.         }
  58.         ;;
  59.     *)
  60.         echo >&2 "E: unknown INSTALL_POLICY: '$INSTALL_POLICY'. This is a bug, exiting."
  61.         exit 2
  62.         ;;
  63. esac
  64.  
  65. copy_debian_dir_if_needed() {
  66.     if [[ -z $DEBIAN_DIR ]]; then
  67.         return
  68.     fi
  69.     if [[ -e debian ]]; then
  70.         if [[ -e debian.old ]]; then
  71.             echo >&2 "E: 'debian.old' already exists, resolve this manually."
  72.             return 1
  73.         fi
  74.         mv debian debian.old || return "$?"
  75.     fi
  76.     cp -r -- "$DEBIAN_DIR" debian || return "$?"
  77. }
  78.  
  79. remove_debian_dir_if_needed() {
  80.     if [[ -z $DEBIAN_DIR ]]; then
  81.         return
  82.     fi
  83.     rm -r --one-file-system debian
  84.     if [[ -e debian.old ]]; then
  85.         mv debian.old debian || return "$?"
  86.     fi
  87. }
  88.  
  89. compile_and_install() {
  90.     if ! [[ -d debian ]]; then
  91.         echo >&2 "E: ./debian/ does not exists."
  92.         return 1
  93.     fi
  94.     mk-build-deps --install debian/control || return "$?"
  95.     dpkg-buildpackage -b -uc -us || return "$?"
  96.     local -a debs_to_install=()
  97.     while IFS= read -r line; do
  98.         local debfile=${line%% *}
  99.         local pkg=${debfile%%_*}
  100.         if should_be_installed "$pkg"; then
  101.             debs_to_install+=(../"$debfile")
  102.         fi
  103.         local changesfile=${debfile%.deb}.changes
  104.         if [[ -e ../$changesfile ]]; then
  105.             rm ../"$changesfile" || true
  106.         fi
  107.     done < debian/files || return "$?"
  108.     if (( ${#debs_to_install[@]} )); then
  109.         install-deb "${debs_to_install[@]}" || return "$?"
  110.     fi
  111. }
  112.  
  113. copy_debian_dir_if_needed || exit "$?"
  114. failed=0
  115. if ! compile_and_install; then
  116.     echo >&2 "*** E: compile_and_install failed, now cleaning up (if needed) and exiting."
  117.     failed=1
  118. fi
  119. remove_debian_dir_if_needed || exit "$?"
  120. if (( failed )); then
  121.     echo >&2 "*** E: now exiting (see above)."
  122.     exit 1
  123. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement