#!/bin/sh

if [ "$1" = "--help" ]; then
	cat <<EOF
usage: vbuilder build <options> [-o <build_options1>] <source1> [...]

options:
  --help                   show this text
  -n, --name <container>   name of environment (required)
  -t, --tmpfs              use tmpfs for the upper layer of overlayfs
  -o, --options <options>  options passed to rpmbuild
  <source1> ...            source rpm(s) to build

example:
vbuilder build -n buildenv -t -o '--with foo' foo-1.0-1.src.rpm -o '--with bar' bar-0.1-1.src.rpm

EOF
	exit 0
fi

LIBEXECDIR="/usr/libexec/vbuilder"
CONTAINER=""
SOURCES=""
INVALID=0
TMPFS=0
TMP_OPTIONS=""
declare -A RPM_OPTIONS

while [ $# -gt 0 ]; do
	case $1 in
		-n|--name)
			shift
			CONTAINER=$1
			;;
		-t|--tmpfs)
			TMPFS=1
			;;
		-o|--options)
			shift
			TMP_OPTIONS="$TMP_OPTIONS $1"
			;;
		*)
			if [ -f $1 ]; then
				if rpm -qp $1 >/dev/null; then
					SOURCES="$SOURCES $1"
					RPM_OPTIONS[$1]="$TMP_OPTIONS"
					TMP_OPTIONS=""
				else
					INVALID=1
				fi
			else
					echo "$1: file not found." >&2
					INVALID=1
			fi
			;;
	esac

	shift
done

if [ "$SOURCES" = "" ]; then
	echo "no source specified." >&2
	exit 1
fi

if [ $INVALID -ne 0 ]; then
	exit 1
fi

. $LIBEXECDIR/common.sh

prepare_container

JOBS=$(mktemp -d --tmpdir vbuilder.XXXXXXXXXX)
mkdir -p $JOBS/{src,result}
chmod 1777 $JOBS
mkdir -p $CONTAINER_PATH/tmp
mount -o bind,_netdev $JOBS $CONTAINER_PATH/tmp

start_container

	cat <<EOF >> $JOBS/build.sh
export TERM=$TERM
export LANG=$LANG

apt-get update || exit 1
apt-get -y dist-upgrade || exit 1

EOF

for f in $SOURCES; do
	cp -f $f $JOBS/src/
	OPTIONS=$(echo ${RPM_OPTIONS[$f]})
	FILENAME=$(basename $f)
	SPECFILE=$(rpm -qlp $f | egrep -e '\.spec$')

	cat <<EOF >> $JOBS/build.sh
sudo -u vbuilder rpm -ivh /tmp/jobs/src/$FILENAME || exit 1

SRCRPM=\$(LANG=C sudo -u vbuilder rpmbuild -bs ~vbuilder/rpm/SPECS/$SPECFILE | egrep '^Wrote: ' | awk '{print \$2;}')
if [ "\$SRCRPM" = "" ]; then
	exit 1
fi

apt-get -y build-dep \$SRCRPM || exit 1

echo "Executing rpmbuild --rebuild $OPTIONS \$SRCRPM ..."

sudo -u vbuilder rpmbuild --rebuild $OPTIONS \$SRCRPM || exit 1

sudo -u vbuilder rpmbuild --rmsource --rmspec \$SRCRPM >/dev/null 2>&1 ||:
rm -f /tmp/jobs/src/$FILENAME

PKGS=\$(find ~vbuilder/rpm/RPMS -type f -print | grep -v -e '/compat32-')
apt-get -y install \$PKGS


for f in \$(find ~vbuilder/rpm/RPMS ~vbuilder/rpm/SRPMS -type f -print); do
	mv -f \$f /tmp/jobs/result/
done

find ~vbuilder/rpm/SOURCES/ -maxdepth 1 -type d -empty | xargs rmdir

EOF
done

chmod 755 $JOBS/build.sh

wait_container_ready

VINE_RELEASE=$(lxc-attach -n $CONTAINER -- cat /etc/vine-release)
if echo $VINE_RELEASE | grep -q VineSeed; then
	VINE_VERSION=VineSeed
else
	VINE_VERSION=$(echo $VINE_RELEASE | awk '{print $3;}' | sed -e 's/\..*$//')
fi

lxc-attach -n $CONTAINER -- bash --login -c /tmp/jobs/build.sh ||:

shutdown_container

umount $CONTAINER_PATH/tmp

umount_tmpfs

if [ "x$SUDO_USER" != "" ]; then
	ORIG_HOME=$(sudo -u $SUDO_USER sh -c 'echo $HOME')
	DEST_BASE=$ORIG_HOME/rpm/vbuilder/$VINE_VERSION
	mkdir -p $DEST_BASE/{RPMS,SRPMS}
	find $JOBS/result -type f -name '*.src.rpm' -exec mv -f -t $DEST_BASE/SRPMS/ {} \;
	for f in $(find $JOBS/result -type f -name '*.rpm'); do
		PKG_ARCH=$(rpm -qp --qf '%{arch}' $f)
		mkdir -p $DEST_BASE/RPMS/$PKG_ARCH
		mv -f $f $DEST_BASE/RPMS/$PKG_ARCH/
	done

	chown -R $SUDO_UID:$SUDO_GID $ORIG_HOME/rpm/vbuilder

	rm -rf $JOBS
fi

