#!/bin/bash

set -e

#Use library
export ALEXPRO100_LIB_LOCATION="${ALEXPRO100_LIB_LOCATION:-./lib/alexpro100_lib.sh}"
# shellcheck disable=SC1091
source ./lib/common/lib_connect.sh

# Check options.
if [[ -z $3 ]]; then
  echo "Create custom arch repo with custom packages."
  echo "Example: $0 /tmp/arch_offline_repo offline_repo base"
  msg_print note "Requries installed pacman."
  exit 1
fi
if ! command_exists pacman; then
  return_err "Binary pacman not found!"
fi

dir="$1"; name="$2"; shift 2; packages="$*"

mkdir -p "$dir/used_repos_db"
pacman --noconfirm --dbpath "$dir/used_repos_db" -Syu -w --cachedir "$dir" $packages
find $dir/ -name "*.pkg.*" -0 | xargs -I {} -n 50 repo-add "$dir/$name.db.tar.gz"

echo "Succesfully created repo $name in $dir."

# =)
echo "Script succesfully ended its work. Have a nice day!"