#!/bin/sh 

BPROG=$0

ABI="32"
#CFLAGS="-I/usr/local/include"
#CXXFLAGS="-I/usr/local/include"

SITE_GNU=ftp://ftp.mirrorservice.org/sites/ftp.gnu.org/gnu

NCPU=${NCPU:-`/usr/sbin/psrinfo -p`}
ARCH=`uname -m`
OSVER=`uname -r`
OSNAME=`uname -s`
OS="${OSNAME}-${OSVER}"




OPT_INSTALL=0
OPT_RECURSE=0
OPT_FORCE=0
OPT_CLEAN=0



push() {
	DIR="$1"

	PREVDIR_10="${PREVDIR_9}"
	PREVDIR_9="${PREVDIR_8}"
	PREVDIR_8="${PREVDIR_7}"
	PREVDIR_7="${PREVDIR_6}"
	PREVDIR_6="${PREVDIR_5}"
	PREVDIR_5="${PREVDIR_4}"
	PREVDIR_4="${PREVDIR_3}"
	PREVDIR_3="${PREVDIR_2}"
	PREVDIR_2="${PREVDIR_1}"
	PREVDIR_1="${PREVDIR}"
	PREVDIR="`pwd`"

	cd "${DIR}"
}

pop() {
	cd "${PREVDIR}"
	PREVDIR="${PREVDIR_1}"
	PREVDIR_1="${PREVDIR_2}"
	PREVDIR_2="${PREVDIR_3}"
	PREVDIR_3="${PREVDIR_4}"
	PREVDIR_4="${PREVDIR_5}"
	PREVDIR_5="${PREVDIR_6}"
	PREVDIR_6="${PREVDIR_7}"
	PREVDIR_7="${PREVDIR_8}"
	PREVDIR_8="${PREVDIR_9}"
	PREVDIR_9="${PREVDIR_10}"
}


hook() {
	FN="hook_$1"
	type $FN 2>/dev/null | grep function > /dev/null && $FN
}




set -- `getopt icfs $*`
for i in $*
do
	case $i in
	-f) OPT_FORCE=1; shift;;
	-i) OPT_INSTALL=1; shift;;
	-s) OPT_RECURSE=1; shift;;
	-c) OPT_CLEAN=1; shift;;
	--) shift; break;;
	esac
done


if [ ! -f "PKGCONF" ]; then
	echo "Missing PKGCONF file."
	exit 10
fi

. PKGCONF

if [ -z "$URL" ]; then
	echo "URL missing from PKGCONF."
	exit 11
fi

HERE=`pwd`
SRCDIR="${HERE}/src"
DESTDIR="${HERE}/staging"
PKGDIR="${HERE}/pkg"
DISTFILES="${HERE}/../../distfiles"
PKGSTORE="${HERE}/../../packages/${CATEGORY}"

export DESTDIR
export DISTFILES



URL_PROTO=`echo "$URL" | cut -f1 -d':'`

cleanup() {
	printf "Cleaning up ... src "
	rm -rf src
	printf "pkg "
	rm -rf pkg
	printf "staging "
	rm -rf staging
	rm -f .download .extract .patchsrc .configure .build .install .package .installpkg
	echo "... done"
}

download() {

	case $URL_PROTO in
		http|https|ftp)
			/usr/sfw/bin/wget -c -O "${DISTFILES}/${SRC}" "${URL}"
			;;
		git)
			P=`echo "${URL}" | cut -f2 -d':'`
			H=`echo "$P" | cut -f1 -d'@'`
			T=`echo "$P" | cut -f2 -d'@'`
			hook git_clone_pre
			git clone --depth 1 --branch "$T" https:$H "${SRCDIR}/${PACKAGE}-${VERSION}"
			hook git_clone_post
			;;
		*)
			echo "Unknown dowload protocol: ${URL_PROTO}"
			return 15
			;;
	esac
	return 0

}

extract() {
	if [ "$URL_PROTO" != "git" ]; then
		cd "${SRCDIR}"
		printf "Extracting ${SRC} ... "
		case $SRC in
			*.gz)
				gzip -d < "${DISTFILES}/${SRC}" | tar xf -
				;;

			*.xz)
				xz -d < "${DISTFILES}/${SRC}" | tar xf -
				;;
			*)
				echo "Error: no method to extract ${SRC}"
				return 10
				;;
		esac
		echo "done"
		cd "${HERE}"
	fi
}

package() {
	echo "PKG=\"${PACKAGE}\"" > "${HERE}/pkginfo"
	echo "NAME=\"${PACKAGE} ${VERSION}\"" >> "${HERE}/pkginfo"
	echo "VERSION=\"${VERSION}\"" >> "${HERE}/pkginfo"
	echo "ARCH=\"${ARCH}\"" >> "${HERE}/pkginfo"
	echo "CLASSES=\"none\"" >> "${HERE}/pkginfo" 
	echo "CATEGORY=\"${CATEGORY}\"" >> "${HERE}/pkginfo" 
	echo "VENDOR=\"Local\"" >> "${HERE}/pkginfo" 
	echo "BASEDIR=\"/\"" >> "${HERE}/pkginfo" 

	cd "${DESTDIR}"
	echo "i pkginfo" > "${HERE}/Prototype"

	if [ ! -z "${PREINSTALL}" ]; then
		cp "${PREINSTALL}" preinstall
		echo "i preinstall" >> "${HERE}/Prototype"
	fi

	if [ ! -z "${POSTINSTALL}" ]; then
		cp "${POSTINSTALL}" postinstall
		echo "i postinstall" >> "${HERE}/Prototype"
	fi

	if [ ! -z "${CHECKINSTALL}" ]; then
		cp "${CHECKINSTALL}" checkinstall
		echo "i checkinstall" >> "${HERE}/Prototype"
	fi



	find . | pkgproto | awk '{print $1 " " $2 " /" $3 " " $4 " root root"}' >> "${HERE}/Prototype"
	cd "${HERE}"

	pkgmk -o -r "${DESTDIR}" -d "${PKGDIR}"  -f Prototype

	pkgtrans "${PKGDIR}" "${PKGSTORE}/${PACKAGE}_${VERSION}_${ARCH}_${OS}.pkg" ${PACKAGE}

#	cd "${DESTDIR}"
#	tar -cf "${HERE}/${PACKAGE}_${VERSION}_${ARCH}_${OS}.tar" *
#	cd "${HERE}"
#	compress -f ${PACKAGE}_${VERSION}_${ARCH}_${OS}.tar

	rm -f "${HERE}/Prototype"
	rm -f "${HERE}/pkginfo"
	rm -f "${HERE}/preinstall"
	rm -f "${HERE}/postinstall"
	rm -f "${HERE}/checkinstall"
}

runonce() {
	FUNC=$1
	if [ ! -f "${HERE}/.${FUNC}" ]; then
		echo "*** STAGE: ${FUNC}"
		if $1 "$2" "$3" "$4" "$5" "$6" ; then
			printf ""
		else
			echo "Failed: $?"
			exit 12
		fi
	fi
	touch "${HERE}/.${FUNC}"
}

installpkg() {
	su root -c "/usr/sbin/pkgadd -d \"${PKGSTORE}/${PACKAGE}_${VERSION}_${ARCH}_${OS}.pkg\""
}

checkdepend() {
	if [ ! -z "${DEPENDS}" ]; then
		FAIL=0
		for DEP in ${DEPENDS}; do
			CATNAME=`echo $DEP | cut -f1 -d'/'`
			PKGNAME=`echo $DEP | cut -f2 -d'/'`
			if pkginfo -q -c $CATNAME $PKGNAME; then
				printf ""
			else
				echo "Missing requirement: $DEP"
				if [ "${OPT_RECURSE}" = "1" ]; then
					(
						cd "${HERE}/../../$CATNAME/$PKGNAME"
						$BPROG -s -i
						cd "${HERE}"
					)
				else
					FAIL=1
				fi
			fi
		done
		if [ "$FAIL" = "1" ]; then
			exit 13
		fi
	fi
}

patchsrc() {
	if [ -d "${HERE}/patches" ]; then
		cd "${SRCDIR}"
		for P in ${HERE}/patches/*.patch; do
			patch -p1 < "$P"
		done
		cd "${HERE}"
	fi
}
	


if [ "${OPT_FORCE}" = "1" ]; then
	cleanup
fi

if [ "${OPT_CLEAN}" = "1" ]; then
	cleanup
	exit 0
fi

checkdepend


mkdir -p "${DISTFILES}"
runonce download

mkdir -p "${SRCDIR}"
runonce extract
runonce patchsrc

runonce configure
runonce build

mkdir -p "${DESTDIR}"
runonce install 

mkdir -p "${PKGDIR}"
mkdir -p "${PKGSTORE}"
runonce package

if [ "${OPT_INSTALL}" = "1" ]; then
	runonce installpkg
fi
