Skip to content
system-upgrade 2.41 KiB
Newer Older
Seblu's avatar
Seblu committed
#!/bin/bash

# Copyright © Sébastien Luttringer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

# Smart way to update your archlinux system

declare SCREEN=screen
declare -i WAIT=0
declare -a PACMAN_OPTS=()
Seblu's avatar
Seblu committed

# run system upgrade and checkservices
upgrade() {
Seblu's avatar
Seblu committed
    if pacman --sync --refresh --sysupgrade "${PACMAN_OPTS[@]}"; then
      checkservices
    else
      printf '\e[7;33;40mcheckservices not run because pacman did not exit zero\e[m\n'
    fi
Seblu's avatar
Seblu committed
# We need to be root
Seblu's avatar
Seblu committed
enforce_root() {
Seblu's avatar
Seblu committed
  if (( $UID != 0 )); then
    if type -P sudo >/dev/null; then
      exec sudo -- "$0" "$@"
    elif type -P su >/dev/null; then
      exec su -c "$0" "$@"
    fi
    printf '\e[7;31;40mYou need to be root\e[m\n'
    exit 1
  fi
Seblu's avatar
Seblu committed
}

# display application usage and exit 1
usage() {
    echo "usage ${0##*/} [options] -- [pacman options]"
    echo "description: archlinux system upgrade with checkservices"
    echo 'options:'
    echo '  -h: this help' >&2
    echo "  -S: no screen" >&2
    exit 1
}

# parse command line arguments
# set options as global vars
argparse() {
    local opt
Seblu's avatar
Seblu committed
    while getopts 'hSw' opt; do
Seblu's avatar
Seblu committed
        case $opt in
Seblu's avatar
Seblu committed
            w) WAIT=1;;
Seblu's avatar
Seblu committed
            *) usage;;
        esac
    done
    shift $((OPTIND - 1));
    PACMAN_OPTS=("$@")
}

# emulated program entry point
main() {
    # parse command line options
    argparse "$@"

Seblu's avatar
Seblu committed
    if [[ -z "$STY" && -z "$TMUX" && -n "$SCREEN" ]] && type -P $SCREEN >/dev/null
    then
Seblu's avatar
Seblu committed
      exec $SCREEN "$0" -S -w "$@"
Seblu's avatar
Seblu committed
    fi
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
    # be root
    # -S to never start a screen as root (offer a root shell)
    # and sudo strip environment so we loose $STY or $TMUX variables
    enforce_root -S "$@"
Seblu's avatar
Seblu committed
    # relly do the job
    upgrade
Seblu's avatar
Seblu committed

    if (( $WAIT == 1 )); then
      read -r -p "Type enter to quit"
    fi
Seblu's avatar
Seblu committed
}

main "$@"