#!/bin/sh

DPKGLIST='/var/lib/dlocate/dpkg-list'

pkgs=1
files=1

usage() {
[ -n "$*" ] && printf "%s\n" "$*" > /dev/stderr && exit 1

cat <<__EOF__
Usage:
      $0 [options]

Options:
    -p    Update the packages list only
    -f    Update the files list only
    -b    Update both (default)

    -h    This help message

__EOF__
exit 1
}

while getopts "pfbh" opt; do
    case "$opt" in
        p) pkgs=1  ; files='' ;;
        f) pkgs='' ; files=1 ;;
        b) pkgs=1  ; files=1 ;;
        h) usage ;;
        *) usage "%s\n" "Unknown option: '$opt'" ;;
    esac
done
shift $((OPTIND-1))

# See ionice(1)
if [ -x /usr/bin/ionice ]; then
  # don't run ionice if kernel version < 2.6.13
  KVER=$(uname -r)
  case "$KVER" in
    2.[012345]*) ;;

    2.6.[0-9])   ;;
    2.6.[0-9].*) ;;

    2.6.1[012]*) ;;

    # Redirect ionice output to /dev/null because VSERVER & OPENVZ
    # & probably other container environments don't like it.  See
    # Bug#456292
    *) ionice -c3 -p$$ > /dev/null 2>&1 ;;
  esac
fi

if [ -n "$files" ] ; then
  # update dlocate database
  test -x /usr/share/dlocate/updatedb && /usr/share/dlocate/updatedb >/dev/null
fi

if [ -n "$files" ] ; then
  # update dpkg-list
  test -x /usr/share/dlocate/update-dpkg-list && /usr/share/dlocate/update-dpkg-list >/dev/null
fi

