#!/bin/bash
# written by Erik Hanson <erik@slackbuilds.org>
# Modified by Eric Hameleers <alien@slackbuilds.org> 20061230

# TODO:
#  cleanup() on exit 1 and after successful package build
#  better handling of files that already exist (not rm -f)
#  more info when downloads fail
#  search (pipedream right now, but possible)

# Some variables
TMP=${TMP:-/tmp/SBo}
REPO=${REPO:-http://slackbuilds.org/ports/}
WGET=$(which wget 2> /dev/null)
SUM=$(which md5sum 2> /dev/null)
LISTING=$($WGET --quiet -O - $REPO.SBo | sort)

usage() {
	echo "SBo-ports usage:"
	echo " Build a package:         $0 SlackBuild"
	echo " Search for a SlackBuild: $0 --search Pattern"
	echo " List all SlackBuilds:    $0 --list"
	exit 0
}

list_all() {
	echo " * Listing all"
	for i in $LISTING ; do
		basename $i .tar.gz
	done
}

search() {
	echo -n " * Searching $1 ..."
	for i in $LISTING ; do
		PKG=$( basename $i .tar.gz )
		if [ "$PKG" == $1 ]; then
			echo " found! Download as ${REPO}${1}.tar.gz"
			exit 0
		fi
	done
	echo " not found in the repository. Consider writing and submitting a '$1' SlackBuild yourself!"
}

cleanup() {
	echo "This will be called to do some cleanup, eventually."
}

build_package() {
	# Make sure we are root, have wget and md5sum
	if [ $(id -u) -ne "0" ]; then
		echo "$0: You need to be root, aborting."
		exit 1
	fi
	BALL="$1"

	# Grab the BuildBall
	mkdir -p $TMP
	pushd $TMP 1> /dev/null
	rm -f $BALL.tar.gz
	echo " * Downloading $BALL.tar.gz"
	$WGET -c --quiet $REPO$BALL.tar.gz
	if [ ! -e $BALL.tar.gz ]; then
		echo " ! Download failed, aborting."
		exit 1
	fi
	echo " * Extracting $BALL.tar.gz"
	tar zxf $BALL.tar.gz
	rm -f $BALL.tar.gz
	chown -R root:root $BALL

	# Collect some info
	pushd $BALL 1> /dev/null
	VERSION=$(grep VERSION $BALL.info | cut -d= -f2)
	SOURCE=$(grep DOWNLOAD $BALL.info | cut -d= -f2)
	ASUM=$(grep MD5SUM $BALL.info | cut -d= -f2 | cut -d" " -f1)
	BSUM=0
	echo " * $BALL version: $VERSION"

	# Check pre-existing source 
	if [ -e $(basename $SOURCE) ]; then
		echo -n " * Pre-existing file $(basename $SOURCE) .. "
		BSUM=$($SUM $(basename $SOURCE) | cut -d" " -f1)
		if [ "$ASUM" == "$BSUM" ]; then
			echo "MD5SUM matches"
		else
			echo "MD5SUM does not match"
		fi
	fi
	if [ "$BSUM" == "0" -o "$ASUM" != "$BSUM" ]; then
		# Grab source
		echo " * Downloading $(basename $SOURCE)"
		$WGET -c --quiet $SOURCE
		if [ ! -e $(basename $SOURCE) ]; then
		        echo " ! Download failed, aborting."
		        exit 1
		fi

		# Check md5sum
		echo -n " * Checking $(basename $SOURCE) MD5SUM.. "
		BSUM=$($SUM $(basename $SOURCE) | cut -d" " -f1)
		if [ "$ASUM" != "$BSUM" ]; then
			echo
			echo " ! Checksum does not match, aborting."
			exit 1
		else
			echo "Passed."
		fi
	fi

	# Start building
	echo " * Building $BALL version $VERSION"
	/bin/sh $BALL.SlackBuild
	echo " * Package for $BALL-$VERSION created in /tmp if all went according to plan."

	popd -1 1> /dev/null
}

# Check arguments, give usage info
# --search --list --build (default)
if [ "$1" == "--list" ]; then
	list_all
elif [ "$1" == "--search" ]; then
	if [ -z "$2" ]; then
		usage
	fi
	search "$2"
else
	if [ -z "$1" ]; then
		usage
	fi
	build_package "$1"
fi

