|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +WORK_DIR="$(dirname "$0")" |
| 4 | +DISTRO_NAME=$(lsb_release -sc) |
| 5 | +OS_REQUIREMENTS_FILENAME="requirements-$DISTRO_NAME.apt" |
| 6 | + |
| 7 | +cd $WORK_DIR |
| 8 | + |
| 9 | +# Check if a requirements file exist for the current distribution. |
| 10 | +if [ ! -r "$OS_REQUIREMENTS_FILENAME" ]; then |
| 11 | + cat <<-EOF >&2 |
| 12 | + There is no requirements file for your distribution. |
| 13 | + You can see one of the files listed below to help search the equivalent package in your system: |
| 14 | + $(find ./ -name "requirements-*.apt" -printf " - %f\n") |
| 15 | + EOF |
| 16 | + exit 1; |
| 17 | +fi |
| 18 | + |
| 19 | +# Handle call with wrong command |
| 20 | +function wrong_command() |
| 21 | +{ |
| 22 | + echo "${0##*/} - unknown command: '${1}'" >&2 |
| 23 | + usage_message |
| 24 | +} |
| 25 | + |
| 26 | +# Print help / script usage |
| 27 | +function usage_message() |
| 28 | +{ |
| 29 | + cat <<-EOF |
| 30 | + Usage: $WORK_DIR/${0##*/} <command> |
| 31 | + Available commands are: |
| 32 | + list Print a list of all packages defined on ${OS_REQUIREMENTS_FILENAME} file |
| 33 | + help Print this help |
| 34 | +
|
| 35 | + Commands that require superuser permission: |
| 36 | + install Install packages defined on ${OS_REQUIREMENTS_FILENAME} file. Note: This |
| 37 | + does not upgrade the packages already installed for new versions, even if |
| 38 | + new version is available in the repository. |
| 39 | + upgrade Same that install, but upgrade the already installed packages, if new |
| 40 | + version is available. |
| 41 | + EOF |
| 42 | +} |
| 43 | + |
| 44 | +# Read the requirements.apt file, and remove comments and blank lines |
| 45 | +function list_packages(){ |
| 46 | + grep -v "#" "${OS_REQUIREMENTS_FILENAME}" | grep -v "^$"; |
| 47 | +} |
| 48 | + |
| 49 | +function install_packages() |
| 50 | +{ |
| 51 | + list_packages | xargs apt-get --no-upgrade install -y; |
| 52 | +} |
| 53 | + |
| 54 | +function upgrade_packages() |
| 55 | +{ |
| 56 | + list_packages | xargs apt-get install -y; |
| 57 | +} |
| 58 | + |
| 59 | +function install_or_upgrade() |
| 60 | +{ |
| 61 | + P=${1} |
| 62 | + PARAN=${P:-"install"} |
| 63 | + |
| 64 | + if [[ $EUID -ne 0 ]]; then |
| 65 | + cat <<-EOF >&2 |
| 66 | + You must run this script with root privilege |
| 67 | + Please do: |
| 68 | + sudo $WORK_DIR/${0##*/} $PARAN |
| 69 | + EOF |
| 70 | + exit 1 |
| 71 | + else |
| 72 | + |
| 73 | + apt-get update |
| 74 | + |
| 75 | + # Install the basic compilation dependencies and other required libraries of this project |
| 76 | + if [ "$PARAN" == "install" ]; then |
| 77 | + install_packages; |
| 78 | + else |
| 79 | + upgrade_packages; |
| 80 | + fi |
| 81 | + |
| 82 | + # cleaning downloaded packages from apt-get cache |
| 83 | + apt-get clean |
| 84 | + |
| 85 | + exit 0 |
| 86 | + fi |
| 87 | +} |
| 88 | + |
| 89 | +# Handle command argument |
| 90 | +case "$1" in |
| 91 | + install) install_or_upgrade;; |
| 92 | + upgrade) install_or_upgrade "upgrade";; |
| 93 | + list) list_packages;; |
| 94 | + help|"") usage_message;; |
| 95 | + *) wrong_command "$1";; |
| 96 | +esac |
0 commit comments