#!/bin/bash
set -e
cd /

# This is just an example of how pm2ml can be used in your own scripts.
# TODO: maybe use "--conf-path" and put this in /etc/ppl.conf

# Arguments for aria2c.
aria2_args=(
  --metalink-file=-
  --conf-path=/etc/ppl.conf
)
if [[ $1 == '-h' ]] \
|| [[ $1 == '--help' ]]
then
  cat <<HELP
DESCRIPTION
  $0 is a convenience wrapper for pm2ml. It is just a simple Bash script.
  Use it to download database and package files, or edit it to suit your needs.

USAGE
  $0 [-y] [-u] [pkgnames]

  -y    download database files to Pacman sync database
  -u    download upgradable packages

  [pkgnames] may be package names or group names

CONFIGURATION
  Aria2 options are read from /etc/ppl.conf

HELP
  exit
elif [[ $1 == '-y' ]]
# Update the database
then
  shift
  pm2ml -yso var/lib/pacman/sync | aria2c "${aria2_args[@]}"
fi

if [[ ! -z  "$*" ]]
# Download the files.
then
  pm2ml -o var/cache/pacman/pkg "$@" -r -p http -l 50 | aria2c "${aria2_args[@]}"
fi
