#!/bin/sh
# Copyright (C) Jamin W. Collins <jcollins@asgardsrealm.net>
#                and Jordan Mantha <mantha@ubuntu.com>
# Copyright 2007 (C) Siegfried-A. Gevatter <siggi.gevatter@gmail.com>
# License: GPLv2 or later
#
# This script is a wrapper to use pbuilder with many different
# distributions / versions. (It was originally created because of
# bug #255165 in Debian.)
#
# If you want to use this copy of the script only for a single distribution
# / version, rename it to 'pbuilder-dapper', 'pbuilder-feisty', 'pbuilder-gutsy', 
# or whatever it is. If you have an amd64, you can also use names like
# 'pbuilder-feisty-i386', etc.

# Base directory where pbuilder will put all the files it creates
BASE_DIR="$HOME/pbuilder"

# Enable additional repositories by default? (universe and multiverse in Ubuntu,
# contrib and non-free in Debian.)
EXTRACOMP=1

# Save the log of the last operation in a dot-file? ('.lastlog' in BASE_DIR)
SAVELOG=0

# Allow this script to use /var/cache/apt/archives/ when possible
SYSCACHE=1


################################

ARCH=`dpkg-architecture -qDEB_HOST_ARCH`
SYSDIST=`lsb_release -cs`

help()
{
        echo "Insufficient number of arguments."
        echo "Usage: $0 "$( [ "$1" != 'show-dist-flag' ] || echo "<distribution> " )$( [ $ARCH != "amd64" ] || echo "[i386|amd64] " )"[mainonly|allcomp] [withlog|nolog] <operation>"
        exit 1
}

if [ ! -z `echo \`basename $0\` | grep -- '-'`  ] && [ `basename $0` != 'pbuilder-dist' ]
then
    if [ $# -lt 1 ]
    then
        help
    fi
    
    BINARCH=`basename $0 | cut -f3 -d '-'`
    DISTRIBUTION=`basename $0 | cut -f2 -d '-'`
else
    if [ $# -lt 2 ]
    then
        help show-dist-flag
    fi
    
    DISTRIBUTION=$1
    shift 1
fi


if [ $1 = "i386" ]  || [ $1 = "amd64" ]
then
    if [ $ARCH = "amd64" ]; then
        BINARCH=$1
    else
        echo "Warning: Architecture switching is not supported on your system; ignoring argument."
    fi
    
    shift 1
fi


if [ $1 = "mainonly" ]; then
    EXTRACOMP=0
    shift 1
elif [ $1 = "allcomp" ]; then
    EXTRACOMP=1
    shift 1
fi


if [ $1 = "withlog" ]; then
    SAVELOG=1
    shift 1
elif [ $1 = "nolog" ]; then
    SAVELOG=0
    shift 1
fi


distdata()
{
    if [ "$1" = "debian" ]
    then
        # Set Debian specific data
        ARCHIVE="http://ftp.debian.org"
        COMPONENTS="main"$( [ $EXTRACOMP = 0 ] || echo " contrib non-free" )
    else
        # Set Ubuntu specific data
        ARCHIVE="http://archive.ubuntu.com/ubuntu"
        COMPONENTS="main restricted"$( [ $EXTRACOMP = 0 ] || echo " universe multiverse" )
    fi
}


case $DISTRIBUTION in
    dapper|edgy|feisty|gutsy|hardy)	# warty|hoary|breezy
        distdata ubuntu
        ;;
    
    oldstable|sarge|stable|etch|testing|lenny|unstable|sid|experimental)
        distdata debian
        ;;
    
    *)
        echo "Warning: Unknown distribution «$DISTRIBUTION»."
        echo -n "Continue [y/N]? "
        read continue
        
        if [ "$continue" != 'y' ] && [ "$continue" != 'Y' ]
        then
            echo "Aborting..."
            exit 1
        fi
        
        distdata
        ;;
esac


OPERATION=$1

case $OPERATION in
    create|update|build|clean|login|execute)
        shift 1
        ;;
    
    *)
        echo "Unrecognized argument. Please use one of those:"
        echo "    create"
        echo "    update"
        echo "    build"
        echo "    clean"
        echo "    login"
        echo "    execute"
        exit 1
        ;;
esac

FOLDERBASE="${DISTRIBUTION}-$( ([ "$BINARCH" != "" ] && echo $BINARCH) || echo $ARCH )"

if [ ! -d $BASE_DIR/${FOLDERBASE}_result ]
then
    mkdir -p $BASE_DIR/${FOLDERBASE}_result
fi

if [ $SYSCACHE != 0 ] && [ "$SYSDIST" = "$DISTRIBUTION" ] && [ "$ARCH" = "$BINARCH" -o -z $BINARCH ]
then
	DEBCACHE='/var/cache/apt/archives/'
fi

sudo pbuilder $OPERATION \
    --basetgz $BASE_DIR/$FOLDERBASE-base.tgz \
    --distribution $DISTRIBUTION \
    $( [ -z $BINARCH ] || echo "--debootstrapopts \"--arch\" --debootstrapopts \"$BINARCH\"" ) \
    $( [ $SAVELOG = 0 ] || echo "--logfile $BASE_DIR/.lastlog" ) \
    $( [ -z $DEBCACHE ] || echo "--aptcache $DEBCACHE" ) \
    --buildresult $BASE_DIR/${FOLDERBASE}_result \
    --mirror "$ARCHIVE" \
    --othermirror "deb $ARCHIVE $DISTRIBUTION $COMPONENTS" \
    $@