#!/usr/bin/bash
##
## Do `rpm -U *.rpm` except already updated packages
## Copyright (c) 2013 SATOH Fumiyasu @ OSSTech Corp., Japan
##
## License: DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2
##

set -u

rpm="${RPM_COMMAND-/usr/bin/rpm}"

rpm_args=()
for rpm_arg in "$@"; do
  if [[ -z ${rpm_arg%%*.rpm} && -f $rpm_arg ]]; then
    rpm_test=$(LC_ALL=C "$rpm" -U --test "$rpm_arg" 2>&1 >/dev/null)
    if [[ -z ${rpm_test%%* is already installed} ]]; then
      echo "$rpm_test (ignored)" 1>&2
      continue
    fi
  fi

  rpm_args+=("$rpm_arg")
done

if [[ ${#rpm_args[@]} -gt 0 ]]; then
  exec "$rpm" -U "${rpm_args[@]}"
fi

exit 1

