#!/bin/sh -e

# psx -- fetch and extract pkgsrc archive

case "$PWD" in
/usr/pkgsrc/*/*) ;;
*) echo "${0##*/}: not in package directory" 1>&2; exit 1 ;;
esac

make fetch
f=/usr/pkgsrc/distfiles/$(sed -n '/.*(\(.*\)).*/{s//\1/;p;q}' distinfo)
d=$(tar -tf "$f" | head -1)

opt=
if [ -e "$d" ]; then
	printf "%s: $d already exists; overwrite? " "${0##*/}" 1>&2
	read ans </dev/tty
	case $ans in
	y*) opt=-U ;;
	*) exit 1 ;;
	esac
fi

echo extracting archive... 1>&2
tar -x $opt -f "$f" 1>&2
echo "$d"