Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- if (( UID != 0 )); then
- echo >&2 "E: you must be root in order to run this script."
- exit 1
- fi
- usage() {
- cat <<EOF
- USAGE: ${0##*/} [-a | -i | -n] [-d DEBIAN_DIR]
- OPTIONS:
- -a: install already installed packages only (default)
- -i: install all non-debug packages plus already installed debug ones
- -n: don't install anything
- -d DEBIAN_DIR:
- specify an alternative debian/ directory. It will be copied
- to the current directory and the copy will be removed after build.
- If './debian' already exists, it will be renamed to './debian.old'
- (and renamed back after removing the copy). If it already exists too,
- you will be asked to resolve that manually.
- EOF
- exit 2
- }
- DEBIAN_DIR=
- INSTALL_POLICY=a
- while getopts 'aind:' option; do
- case "$option" in
- [ain]) INSTALL_POLICY=$option ;;
- d) DEBIAN_DIR=$OPTARG ;;
- *) usage ;;
- esac
- done
- shift "$(( OPTIND - 1 ))" || usage
- if (( $# != 0 )); then
- echo >&2 "E: unexpected positional argument."
- usage
- fi
- is_installed() {
- dpkg-query -s -- "$1" 2>/dev/null | grep -q '^Status: .* installed$'
- }
- case "$INSTALL_POLICY" in
- a)
- should_be_installed() {
- is_installed "$1"
- }
- ;;
- i)
- should_be_installed() {
- [[ $1 != *-dbg ]] || is_installed "$1"
- }
- ;;
- n)
- should_be_installed() {
- false
- }
- ;;
- *)
- echo >&2 "E: unknown INSTALL_POLICY: '$INSTALL_POLICY'. This is a bug, exiting."
- exit 2
- ;;
- esac
- copy_debian_dir_if_needed() {
- if [[ -z $DEBIAN_DIR ]]; then
- return
- fi
- if [[ -e debian ]]; then
- if [[ -e debian.old ]]; then
- echo >&2 "E: 'debian.old' already exists, resolve this manually."
- return 1
- fi
- mv debian debian.old || return "$?"
- fi
- cp -r -- "$DEBIAN_DIR" debian || return "$?"
- }
- remove_debian_dir_if_needed() {
- if [[ -z $DEBIAN_DIR ]]; then
- return
- fi
- rm -r --one-file-system debian
- if [[ -e debian.old ]]; then
- mv debian.old debian || return "$?"
- fi
- }
- compile_and_install() {
- if ! [[ -d debian ]]; then
- echo >&2 "E: ./debian/ does not exists."
- return 1
- fi
- mk-build-deps --install debian/control || return "$?"
- dpkg-buildpackage -b -uc -us || return "$?"
- local -a debs_to_install=()
- while IFS= read -r line; do
- local debfile=${line%% *}
- local pkg=${debfile%%_*}
- if should_be_installed "$pkg"; then
- debs_to_install+=(../"$debfile")
- fi
- local changesfile=${debfile%.deb}.changes
- if [[ -e ../$changesfile ]]; then
- rm ../"$changesfile" || true
- fi
- done < debian/files || return "$?"
- if (( ${#debs_to_install[@]} )); then
- install-deb "${debs_to_install[@]}" || return "$?"
- fi
- }
- copy_debian_dir_if_needed || exit "$?"
- failed=0
- if ! compile_and_install; then
- echo >&2 "*** E: compile_and_install failed, now cleaning up (if needed) and exiting."
- failed=1
- fi
- remove_debian_dir_if_needed || exit "$?"
- if (( failed )); then
- echo >&2 "*** E: now exiting (see above)."
- exit 1
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement