Skip to content
system-upgrade 2.27 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

Seblu's avatar
Seblu committed
SCREEN=screen
PACMAN_OPTS=()

# 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
    while getopts 'hS' opt; do
        case $opt in
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
      exec $SCREEN "$0" -S "$@"
    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
}

main "$@"