Improve install_os_dependencies.sh

This commit is contained in:
David Díaz 2016-06-21 23:44:20 +02:00
parent 308141d5b9
commit 1aa7bfcf98

View File

@ -2,39 +2,48 @@
WORK_DIR="$(dirname "$0")" WORK_DIR="$(dirname "$0")"
DISTRO_NAME=$(lsb_release -sc) DISTRO_NAME=$(lsb_release -sc)
OS_REQUIREMENTS_FILENAME="requirements-$DISTRO_NAME.apt"
OS_REQUIREMENTS_FILENAME="$WORK_DIR/requirements-$DISTRO_NAME.apt" cd $WORK_DIR
# Check if a requirements file exist for the current distribution.
if [ "$DISTRO_NAME" != "xenial" ] && [ "$DISTRO_NAME" != "trusty" ] && [ "$DISTRO_NAME" != "jessie" ]; then if [ ! -r "$OS_REQUIREMENTS_FILENAME" ]; then
echo "Only the Ubuntu 14.04 (Trusty), 16.04 (Xenial) and Debian 8.x (Jessie) is supported by this script"; cat <<-EOF >&2
echo "You can see requirements-trusty.apt, requirements-xenial.apt or requirements-jessie.apt file to help search the equivalent package in your system"; There is no requirements file for your distribution.
exit 1; You can see one of the files listed below to help search the equivalent package in your system:
`find ./ -name "requirements-*.apt" -printf " - %f\n"`
EOF
exit 1;
fi fi
# Handle call with wrong command # Handle call with wrong command
function wrong_command() function wrong_command()
{ {
echo "${0##*/} - unknown command: '${1}'" echo "${0##*/} - unknown command: '${1}'" >&2
usage_message usage_message
} }
# Print help / script usage # Print help / script usage
function usage_message() function usage_message()
{ {
echo "usage: ./${0##*/} <command>" cat <<-EOF
echo "available commands are:" Usage: $WORK_DIR/${0##*/} <command>
echo -e "\tlist\t\tPrint a list of all packages defined on ${OS_REQUIREMENTS_FILENAME} file" Available commands are:
echo -e "\thelp\t\tPrint this help" list Print a list of all packages defined on ${OS_REQUIREMENTS_FILENAME} file
echo -e "\n\tCommands that require superuser permission:" help Print this help
echo -e "\tinstall\t\tInstall packages defined on ${OS_REQUIREMENTS_FILENAME} file. Note: This\n\t\t\t does not upgrade the packages already installed for new\n\t\t\t versions, even if new version is available in the repository."
echo -e "\tupgrade\t\tSame that install, but upgrate the already installed packages,\n\t\t\t if new version is available."
Commands that require superuser permission:
install Install packages defined on ${OS_REQUIREMENTS_FILENAME} file. Note: This
does not upgrade the packages already installed for new versions, even if
new version is available in the repository.
upgrade Same that install, but upgrade the already installed packages, if new
version is available.
EOF
} }
# Read the requirements.apt file, and remove comments and blank lines # Read the requirements.apt file, and remove comments and blank lines
function list_packages(){ function list_packages(){
grep -v "#" "${OS_REQUIREMENTS_FILENAME}" | grep -v "^$"; grep -v "#" "${OS_REQUIREMENTS_FILENAME}" | grep -v "^$";
} }
function install_packages() function install_packages()
@ -47,18 +56,17 @@ function upgrade_packages()
list_packages | xargs apt-get install -y; list_packages | xargs apt-get install -y;
} }
function install_or_upgrade() function install_or_upgrade()
{ {
P=${1} P=${1}
PARAN=${P:-"install"} PARAN=${P:-"install"}
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
echo -e "\nYou must run this with root privilege" 2>&1 cat <<-EOF >&2
echo -e "Please do:\n" 2>&1 You must run this script with root privilege
echo "sudo ./$WORK_DIR/${0##*/} $PARAN" 2>&1 Please do:
echo -e "\n" 2>&1 sudo $WORK_DIR/${0##*/} $PARAN
EOF
exit 1 exit 1
else else
@ -76,16 +84,13 @@ function install_or_upgrade()
exit 0 exit 0
fi fi
} }
# Handle command argument # Handle command argument
case "$1" in case "$1" in
install) install_or_upgrade;; install) install_or_upgrade;;
upgrade) install_or_upgrade "upgrade";; upgrade) install_or_upgrade "upgrade";;
list) list_packages;; list) list_packages;;
help) usage_message;; help|"") usage_message;;
*) wrong_command "$1";; *) wrong_command "$1";;
esac esac