#!/bin/sh

# buildpkg - calls pwbuilder
# Copyright (c) 2002 Jaroslaw Swierczynski <swiergot@intersec.pl>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

# You should find the copy of GNU General Public License in file
# /usr/doc/pwbuilder-*/COPYING.

echo "buildpkg 0.2.4 - calls pwbuilder"
    
if [ "$#" = "0" ]; then

    echo "Usage:"
    echo
    echo "buildpkg [build-options] [--global] <packagename>"
    echo "         build a package from installed sources"
    echo "buildpkg --build [build-options] [--global] <filename>"
    echo "         build a package from src.tgz archive"
    echo "buildpkg [build-options] [--global] <filename>"
    echo "         build a package from src.tgz archive"
    echo "buildpkg --install [--global] <filename>"
    echo "         install sources from src.tgz archive"
    echo "buildpkg --remove [--global] <packagename>"
    echo "         remove installed sources"
    echo "buildpkg --pack [--global] <packagename>"
    echo "         pack installed sources into src.tgz archive"
    echo "buildpkg --help <packagename>"
    echo "         display available schemes for a package with installed sources"
    echo
    echo "Instead of long parameters you can use short ones, ie. -i for --install."
    echo "For more informations see manual page buildpkg(8)."
    exit 1
fi

schemes=""

if [ ! -d ~/tgz/sources ]; then

    mkdir -p ~/tgz/sources

fi

if [ ! -d ~/tgz/packages ]; then

    mkdir -p ~/tgz/packages

fi

sources_dir=~/tgz/sources

while [ "$#" -gt "0" ]; do
    case "$1" in
	"--help"|"-h") shift; if [ -d ~/tgz/sources/$1 -a "$1" != "" ]; then
					echo
					ls ~/sources/$1/scheme.* | awk 'BEGIN { FS = "/scheme[.]" } { print $2 }'
				    elif [ -d /usr/src/tgz/sources/$1 -a "$1" != "" ]; then
				        echo
					ls /usr/src/tgz/sources/$1/scheme.* | awk 'BEGIN { FS = "/scheme[.]" } { print $2 }'
				    else
				        echo "buildpkg: $1: no such package"
				    fi
				    exit ;;
	"--auto"|"-a") auto="automatic=y" ;;
	"--output"|"-o") output="output=y" ;;
	"--debug"|"-d") debug="debug=y" ;;
	"--target"|"-t") shift; target_archs="$1" ;;
	"--scheme"|"-s") shift; schemes="$schemes $1" ;;
	"--no-pw"|"-n") nopw="disable_pw=y" ;;
	"--install"|"--remove"|"--pack"|"--build"|"-i"|"-r"|"-p"|"-b") func="$1" ;;
	"--global"|"-g") sources_dir=/usr/src/tgz/sources ;;
	"--no-color"|"-c") nocolor="nocolor=y" ;;
	"--build-number"|"-m") shift; build_number="build_number=$1" ;;
	"--print-results"|"-r") print_results="print_results=y" ;;
	-*) echo -e "buildpkg: $1: invalid option"; exit ;;
	*) package="$1" ;;
    esac
    shift
done

if [ "$package" = "" ]; then
    exit
fi

unid="$RANDOM$RANDOM"

function test_write() {

    touch $1/test_write-$unid > /dev/null 2>&1
    if [ -f "$1/test_write-$unid" ]; then
    
	rm -f $1/test_write-$unid
	return 0

    else

	return 1

    fi

}

if [ "$func" = "" -a -r "$package" -a "`basename $package .src.tgz`" != "`basename $package`" ]; then

    func="--build"

fi

if [ "$func" = "--install" -o "$func" = "-i" ]; then

    if [ -f "$package" ]; then

	if ! test_write $sources_dir ; then

	    echo "You can't write in $sources_dir."
	    exit

	fi

	pdir="`dirname $package`"
	pfile="`basename $package`"
	cwd="`pwd`"
	[ "$pdir" = "." ] && pdir="$cwd"
	echo "$pdir" | grep "^/" > /dev/null 2>&1 || pdir="$cwd/$pdir"
	tmp="/tmp/builder-$unid"
	rm -rf $tmp
	mkdir -p $tmp
	cd $tmp
	tar zxf $pdir/$pfile
	pack="`ls -1 --indicator-style=none $tmp/usr/src/tgz/sources`"
	if [ -d "$sources_dir/$pack" ]; then
	    cd $cwd
	    rm -rf $tmp
	    echo "buildpkg: $pack: already exists"
	else
	    mv -f $tmp/usr/src/tgz/sources/$pack $sources_dir
	    cd $cwd
	    rm -rf $tmp
	    echo "buildpkg: $package: installed"
	fi

    else

	echo "buildpkg: $package: no such file"

    fi

elif [ "$func" = "--remove" -o "$func" = "-r" ]; then

    if [ -d "$sources_dir/$package" ]; then

	rm -rf $sources_dir/$package
	echo "buildpkg: $package: removed"

    else

	echo "buildpkg: $package: no such source"

    fi

elif [ "$func" = "--pack" -o "$func" = "-p" ]; then

    if [ -d "$sources_dir/$package" ]; then

	if [ ! "`id -u`" = "0" ]; then

	    if ! which fakeroot > /dev/null 2>&1 ; then

		echo "You need fakeroot to do that."
		exit

	    fi

	fi

	if ! test_write `pwd` ; then

	    echo "You can't write in the current directory."
	    exit

	fi

	cwd="`pwd`"
	eval "`egrep \"^version=|^build=|^vendor_id=\" $sources_dir/$package/$package.builder`"
	tmp="/tmp/builder-$unid"
	rm -rf $tmp
	mkdir -p $tmp/usr/src/tgz/sources
	cp -R $sources_dir/$package $tmp/usr/src/tgz/sources
	if [ ! "$build_number" = "" ]; then
	    eval $build_number
	    build=$build_number
	    echo -e ",s}^build=\".*\"$}build=\"$build\"}\nw\nq" | ed $tmp/usr/src/tgz/sources/$package/$package.builder
	fi
	if [ "`id -u`" = "0" ]; then
	    ( cd $tmp ; tar zcf $cwd/$package-$version-$build$vendor_id.src.tgz usr/src/tgz/sources/$package )
	else
	    ( cd $tmp ; fakeroot "tar zcf $cwd/$package-$version-$build$vendor_id.src.tgz usr/src/tgz/sources/$package" )
	fi
	rm -rf $tmp
	echo "buildpkg: packed to $package-$version-$build$vendor_id.src.tgz"

    else

	echo "buildpkg: $package: no such source"

    fi

elif [ "$func" = "--build" -o "$func" = "-b" ]; then

    if [ -f "$package" ]; then

	if [ ! "`id -u`" = "0" ]; then

	    if ! which fakeroot > /dev/null 2>&1 ; then

		echo "You need fakeroot to do that."
		exit

	    fi

	fi

	if ! test_write "`dirname $sources_dir`/packages" ; then

	    echo "You can't write in `dirname $sources_dir`/packages."
	    exit

	fi

	pdir="`dirname $package`"
	pfile="`basename $package`"
	cwd="`pwd`"
	[ "$pdir" = "." ] && pdir="$cwd"
	echo "$pdir" | grep "^/" > /dev/null 2>&1 || pdir="$cwd/$pdir"
	tmp="/tmp/builder-$unid"
	rm -rf $tmp
	mkdir -p $tmp
	cd $tmp
	tar zxf $pdir/$pfile
	pack="`ls -1 --indicator-style=none $tmp/usr/src/tgz/sources`"
	if [ -d "$sources_dir/$pack" ]; then
	    rnd="$RANDOM$RANDOM"
	    mv -f $sources_dir/$pack $sources_dir/$pack-$rnd
	fi
	mv -f $tmp/usr/src/tgz/sources/$pack $sources_dir/$pack
	cd $cwd
	rm -rf $tmp
	
	for scheme in $schemes; do

	    if [ -f "$sources_dir/$pack/scheme.$scheme" ]; then

		source "$sources_dir/$pack/scheme.$scheme"
    
    	    fi

        done

	if [ "$target_archs" = "" ]; then

	    target_archs="i386"

	fi

	for target_arch in $target_archs; do

	    eval "target_architecture=$target_arch $auto $output $debug $nopw $nocolor $build_number $print_results sources_dir=$sources_dir $sources_dir/$pack/$pack.builder"

	done

	rm -rf $sources_dir/$pack
	if [ ! "$rnd" = "" ]; then
	    mv $sources_dir/$pack-$rnd $sources_dir/$pack
	fi

    else

	echo "buildpkg: $package: no such file"

    fi

else

    if [ ! "`id -u`" = "0" ]; then

	if ! which fakeroot > /dev/null 2>&1 ; then

    	    echo "You need fakeroot to do that."
    	    exit

	fi

    fi

    if ! test_write `dirname $sources_dir`/packages ; then

        echo "You can't write in `dirname $sources_dir`/packages."
        exit

    fi

    for scheme in $schemes; do

	if [ -f "$sources_dir/$package/scheme.$scheme" ]; then

	    source "$sources_dir/$package/scheme.$scheme"
    
        fi

    done

    if [ "$target_archs" = "" ]; then

        target_archs="i386"

    fi

    for target_arch in $target_archs; do

	eval "target_architecture=$target_arch $auto $output $debug $nopw $nocolor $build_number $print_results sources_dir=$sources_dir $sources_dir/$package/$package.builder"

    done

fi
