#!/bin/bash set -eu # set -x set -o pipefail use_rpm=False use_deb=False use_brew=False rpm_packages="" deb_packages="" brew_packages="" function usage { retval="$1" case "$retval" in 0) ;; 1) exec 1>&2 ;; *) echo "$0: Internal error: argument to usage must be 0 or 1" 1>&2 exit 1 ;; esac echo "$0 --force-rpm --force-deb --force-brew --rpm-packages --deb-packages --brew-packages" echo "If you do not give --force-rpm, --force-deb or --force-brew, this script will guess which to use" echo echo "Example:" echo " $0 --rpm-packages 'coreutils sharutils' --deb-packages 'coreutils sharutils' --brew-packages ''" echo echo "Note that package names commonly differ from rpm to deb systems" exit "$retval" } while [ "$#" -ge 1 ] do case "$1" in --force-rpm) use_rpm=True ;; --force-deb) use_deb=True ;; --force-brew) use_brew=True ;; --rpm-packages) rpm_packages="$2" shift ;; --deb-packages) deb_packages="$2" shift ;; --brew-packages) brew_packages="$2" shift ;; --help|-h) usage 0 ;; *) echo "$0: Unrecognized option: $1" 1>&2 usage 1 ;; esac shift done case "$use_rpm-$use_deb-$use_brew" in True*True) echo "$0: Only specify zero or one of --force-rpm, --force-deb and --force-brew" 1>&2 exit 1 ;; esac case "$use_rpm-$use_deb-$use_brew" in False-False-False) # Neither rpm, deb nor brew forced - try to guess if type -p rpm > /dev/null 2>&1 then use_rpm=True fi if type -p apt-get > /dev/null 2>&1 then use_deb=True fi if type -p brew > /dev/null 2>&1 then use_brew=True fi esac function rpm_all_found { # shellcheck disable=SC2048 for package in $* do # FIXME: This might not be the correct way to test if an rpm is already installed if rpm -q "$package" > /dev/null 2>&1 then continue else # We didn't find one - return negative-logic false return 1 fi done # We found them all - return negative-logic true return 0 } function deb_all_found { # shellcheck disable=SC2048 for package in $* do # See https://stackoverflow.com/questions/1298066/check-if-a-package-is-installed-and-then-install-it-if-its-not # These don't work # if dpkg -p "$package" > /dev/null 2>&1 # if dpkg -s "$package" > /dev/null 2>&1 # if dpkg-query -l "$package" > /dev/null 2>&1 # if dpkg-query -p "$package" > /dev/null 2>&1 if dpkg-query -s "$package" 2> /dev/null | grep '^Status: ' | tr ' ' '\n' | grep -Fqx installed then continue else # We didn't find one - return negative-logic false return 1 fi done # We found them all - return negative-logic true return 0 } function brew_all_found { # We don't really need this - brew does not run as root. But it might be a little faster - not sure really. # shellcheck disable=SC2048 for package in $* do # See https://apple.stackexchange.com/questions/284379/with-homebrew-how-to-check-if-a-software-package-is-installed if brew list "$package" > /dev/null 2>&1 then continue else # We didn't find one - return negative-logic false return 1 fi done # We found them all - return negative-logic true return 0 } function deb_install_packages { # shellcheck disable=SC2048,SC2086 sudo apt-get install -y $* } function rpm_install_packages { # shellcheck disable=SC2048,SC2086 sudo yum install -y $* } function brew_install_packages { # shellcheck disable=SC2048,SC2086 brew install $* } case "$use_rpm-$use_deb-$use_brew" in True*True) echo "$0: Internal error: \$use_rpm-\$use_deb-\$use_brew has a strange value: $use_rpm-$use_deb-$use_brew" 1>&2 exit 1 ;; False-False-False) echo "$0: Internal error: \$use_rpm, \$use_deb and \$use_brew are all False" 1>&2 exit 1 ;; True-*-*) if rpm_all_found "$rpm_packages" then echo "$0: All rpm packages found, nothing to do" 1>&2 exit 0 else rpm_install_packages "$rpm_packages" fi ;; *-True-*) # shellcheck disable=SC2086 if deb_all_found $deb_packages then echo "$0: All deb packages found, nothing to do" 1>&2 exit 0 else # shellcheck disable=SC2086 deb_install_packages $deb_packages fi ;; *-*-True) # shellcheck disable=SC2086 if brew_all_found $brew_packages then echo "$0: All brew packages found, nothing to do" 1>&2 exit 0 else # shellcheck disable=SC2086 brew_install_packages $brew_packages fi ;; *) echo "$0: Internal error: \$use_rpm has a strange value: $use_rpm" 1>&2 exit 1 ;; esac