#!/bin/bash

if [[ -z $1 ]]
then
  echo "usage: $0 <pkgfile>"
  exit 1
fi

pkgfile="$(readlink -f "$1")"

# Uncomment the following to enabled PKGBUILD retrieval with pbget.

# pkgname="${pkgfile%-*-*-*.pkg.*.*}"
# pkgname="${pkgname##*/}"
#
# if [[ $pkgfile == $pkgname ]] \
# || [[ -z $pkgname ]]
# then
#   echo "error: failed to parse pkgname from $pkgfile"
#   exit 1
# fi
#
# pbget --aur "$pkgname"
# echo ""
#
# echo "About to cd into $pkgname and run makepkg."
# echo "Verify that the PKGBUILD and associated files"
# echo "are correct and safe before continuing."
# echo ""
# read -p "Proceed? [y/N] " ans_
#
#
# if [[ $ans_ != 'y' ]] && [[ $ans_ != 'Y' ]]
# then
#   exit 0
# fi
#
# cd "$pkgname" || exit 1

cp PKGBUILD rePKGBUILD

echo """

package()
{
  msg "repackaging \$pkgname..."
  bsdtar -C "\$pkgdir" -xf "$pkgfile"
}

""" >> rePKGBUILD

makepkg -fRp rePKGBUILD "$@"
