#!/bin/bash

# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Lists all package dependencies of a particular package.  Useful to find out
# all packages depended on by chromeos and chromeos-dev.

# --- BEGIN COMMON.SH BOILERPLATE ---
# Load common CrOS utilities.  Inside the chroot this file is installed in
# /usr/lib/crosutils.  Outside the chroot we find it relative to the script's
# location.
find_common_sh() {
  local common_paths=(/usr/lib/crosutils $(dirname "$(readlink -f "$0")"))
  local path

  SCRIPT_ROOT=
  for path in "${common_paths[@]}"; do
    if [ -r "${path}/common.sh" ]; then
      SCRIPT_ROOT=${path}
      break
    fi
  done
}

find_common_sh
. "${SCRIPT_ROOT}/common.sh" || { echo "Unable to load common.sh"; exit 1; }
# --- END COMMON.SH BOILERPLATE ---

# Script must be run inside the chroot.
assert_inside_chroot

get_default_board

# Flags.
DEFINE_string board "$DEFAULT_BOARD" \
  "The board for which the image was built." b

# Parse flags.
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"

[[ -z "${FLAGS_board}" ]] && die "A board must be specified."

if [[ -z "${FLAGS_ARGV}" ]]; then
  warn "Please specify package."
  die "Usage:  ./get_package_list.sh chromeos-base/chromeos > package.list"
fi

emerge-$FLAGS_board --emptytree --usepkgonly -p -v --columns \
  $1 2> /dev/null | sed -n -E -e 's/^\[binary[^]]*\] ([^ ]*) .*$/\1/p'
