#!/bin/sh

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

pkgs=1
files=1

usage() {
  cat <<__EOF__
Usage: $0 [<option>...]

Options:
  -p        Update the packages list only.
  -f        Update the files list only.
  -b        Update both (default).
  -h        This help message.
__EOF__
}

usage_error() {
  printf "%s\n" "$*" >/dev/stderr
  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_error "unknown option: '$opt'"
    ;;
  esac
done
shift $((OPTIND - 1))

# See ionice(1)
if command -v ionice >/dev/null; then
  # 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
fi

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

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