#!/bin/bash
set -euo pipefail
exec 2>&1
export LC_ALL=C
export PATH=/usr/bin
umask 022

readonly PACMAN=/usr/bin/pacman
readonly LOCK=/var/lib/pacman/db.lck
readonly SYNC=/var/lib/pacman/sync

clear_bad_signatures() {
  local f first
  shopt -s nullglob
  for f in "$SYNC"/*.db.sig "$SYNC"/*.files.sig; do
    [ -f "$f" ] && [ ! -L "$f" ] || continue
    first=$(/usr/bin/head -c 256 -- "$f" | /usr/bin/tr -d ' \t\r\n\000' | /usr/bin/head -c 1 || true)
    if [ "$first" = "<" ]; then
      echo "removing a corrupt database signature (a mirror error page): ${f##*/}"
      /usr/bin/rm -f -- "$f"
    fi
  done
  shopt -u nullglob
}

upgrade_flags() {
  local channel=
  if [ -f /etc/fiordland/channel ] && [ ! -L /etc/fiordland/channel ]; then
    channel=$(/usr/bin/head -c 64 /etc/fiordland/channel | /usr/bin/tr -d '[:space:]')
  fi
  if [ "$channel" = stable ]; then
    echo -Syuu
  else
    echo -Syu
  fi
}

case "${1:-}" in
  upgrade)
    [ $# -eq 1 ] || exit 2
    if [[ -e $LOCK || -L $LOCK ]]; then
      echo "Package database locked. Finish the active transaction before retrying." >&2
      exit 1
    fi
    clear_bad_signatures
    flags=$(upgrade_flags)
    exec "$PACMAN" "$flags" --noconfirm
    ;;
  rollback)
    [ $# -eq 2 ] && [[ $2 =~ ^[1-9][0-9]{0,8}$ ]] || exit 2
    exec /usr/bin/fiordland-rollback -y "$2"
    ;;
  *)
    exit 2
    ;;
esac
