#!/usr/bin/env bash
# paccat -- cat a file from a pacman package

. lib.bash || exit

package=$1
file=$2

if [[ $file != /* ]]; then
	if [[ $PWD/ == /home/* ]]; then
		file=/$file
	fi
	file=$(readlink -m "$file")
fi

url=$(pacman -Sddp "$package")
pkg=$(basename "$url")
pkg=/var/cache/pacman/pkg/$pkg

if [[ ! -s $pkg ]]; then
	sudo pacman -Sddw --noconfirm --quiet "$package" >&2
fi

if [[ ! -s $pkg ]]; then
	die "could not download '${pkg##*/}'"
fi

bsdtar xfO "$pkg" "${file#/}"
