|
1 | 1 | #!/bin/bash
|
2 |
| -########################################################################################### |
| 2 | +################################################################################################## |
3 | 3 | #
|
4 | 4 | # This script checks for the required Debian packages are installed
|
5 | 5 | # to build OpenCV.
|
6 | 6 | # Commandline parameters:
|
7 |
| -# $@ - These are the names of the packages to check with 'dpkg' |
| 7 | +# $@ These are the names of the packages to check with 'dpkg'. Multiple values may |
| 8 | +# be specified per package by using pipe as a delimiter, e.g. libpng-dev|libpng12-dev. |
| 9 | +# Multiple values are evaluated left-to-right and the first found prevents checking of |
| 10 | +# the remaining package options. |
| 11 | +# |
| 12 | +# -o <package_name> Specifying this switch with a package name marks it as optional |
| 13 | +# i.e. it is not required to be installed. |
8 | 14 | #
|
9 | 15 | # Returns:
|
10 | 16 | # 0 - All packages installed (success)
|
|
13 | 19 | # Kerry Billingham <contact (at) avionicengineers (d0t) com>
|
14 | 20 | # 20 April 2016
|
15 | 21 | #
|
16 |
| -########################################################################################### |
| 22 | +################################################################################################## |
17 | 23 | red=$'\e[1;31m'
|
18 | 24 | green=$'\e[1;32m'
|
| 25 | +yellow=$'\e[1;33m' |
19 | 26 | end=$'\e[0m'
|
20 |
| -check_message="Checking for 'dpkg'" |
| 27 | +check_message="Checking for " |
| 28 | +declare -i packageMissing=0 |
| 29 | +declare -i installed=1 |
| 30 | + |
| 31 | +######################### |
| 32 | +# Function declarations. |
| 33 | +######################### |
| 34 | +function check_package() { |
| 35 | + check_message="Checking for package " |
| 36 | + dpkg -s $1 &>/dev/null |
| 37 | + is_installed=$? |
| 38 | + if [ ${is_installed} -ne 0 ]; then |
| 39 | + printf "%-80s%s\n" "$2${check_message}${red}$1" " MISSING.${end}" |
| 40 | + packageMissing=1 |
| 41 | + else |
| 42 | + printf "%-80s%s\n" "$2${check_message}${green}$1" " INSTALLED.${end}" |
| 43 | + packageMissing=0 |
| 44 | + fi |
| 45 | + return $is_installed |
| 46 | +} |
| 47 | + |
| 48 | +# Main part of script. |
| 49 | +ORIGINAL_IFS=$IFS |
| 50 | + |
21 | 51 | dpkg -? &>/dev/null
|
22 | 52 | if [ $? -ne 0 ]; then
|
23 |
| - printf "%-80s%s\n" "${check_message}" "${red} MISSING.${end}" |
| 53 | + printf "%-80s%s\n" "${check_message} ${red}'dpkg'" " MISSING.${end}" |
24 | 54 | exit 1
|
25 | 55 | else
|
26 |
| - printf "%-80s%s\n" "${check_message}" "${green} INSTALLED.${end}" |
| 56 | + printf "%-80s%s\n" "${check_message} ${green}'dpkg'" " INSTALLED.${end}" |
27 | 57 | fi
|
28 | 58 |
|
29 |
| -declare -i packageMissing=0 |
30 |
| -packageArray=( "$@" ) |
| 59 | +while getopts o: option; do |
| 60 | + case $option in |
| 61 | + o) |
| 62 | + IFS="|" |
| 63 | + packageChoices=( ${OPTARG} ) |
| 64 | + if [ ${#packageChoices[@]} -gt 1 ]; then |
| 65 | + echo "Optional package. One of ${yellow}${packageChoices[@]}${end} can be installed." |
| 66 | + for choice in ${packageChoices[@]}; do |
| 67 | + check_package ${choice} " " |
| 68 | + if [ $? -eq 0 ]; then |
| 69 | + break |
| 70 | + fi |
| 71 | + done |
| 72 | + else |
| 73 | + echo "Optional package ${yellow}${packageChoices}${end}" |
| 74 | + check_package ${OPTARG} " " |
| 75 | + fi |
| 76 | + IFS=$ORIGINAL_IFS |
| 77 | + ;; |
| 78 | + \?) |
| 79 | + echo "No option found" |
| 80 | + ;; |
| 81 | + esac |
| 82 | +done |
| 83 | + |
| 84 | +shift $((OPTIND-1)) |
| 85 | +packageArray=( $@ ) |
31 | 86 | for package in ${packageArray[@]}; do
|
32 |
| - check_message="Checking for package ${package}" |
33 |
| - dpkg -s ${package} &>/dev/null |
34 |
| - if [ $? -ne 0 ]; then |
35 |
| - printf "%-80s%s\n" "${check_message}" "${red} MISSING.${end}" |
36 |
| - packageMissing=1 |
| 87 | + IFS="|" |
| 88 | + packageChoices=( ${package} ) |
| 89 | + if [ ${#packageChoices[@]} -gt 1 ]; then |
| 90 | + echo "Multiple options. One of ${yellow}${packageChoices[@]}${end} must be installed." |
| 91 | + for choice in ${packageChoices[@]}; do |
| 92 | + check_package ${choice} " " |
| 93 | + if [ $? -eq 0 ]; then |
| 94 | + break |
| 95 | + fi |
| 96 | + done |
37 | 97 | else
|
38 |
| - printf "%-80s%s\n" "${check_message}" "${green} INSTALLED.${end}" |
| 98 | + check_package ${package} "" |
39 | 99 | fi
|
40 | 100 | done
|
41 | 101 |
|
|
0 commit comments