#!/bin/bash
# Description: This Script is a Testbuildhelper for Ubuntu Packages
# 
# Version:
# 0.1 Initial Version

# source functions
source /usr/lib/make-test-release.lib
                
console-init
box
box_full
box_content "Step 1: Config file"
box_full                
echo -n "Sourcing $cfg..."
source $cfg
print_okay
echo -n "Trying to find the recipe file..."
pkg=`find *.recipe`
if [ -f $pkg ]
then
    print_okay
else
    pkg=`dialog --inputbox "What is the name of the recipe file?" 0 0 "daps.recipe" 3>&1 1>&2 2>&3`
    if [ -f $pkg ]
    then
        print_okay
    else
    print_missing
    echo "Aborting..."
    print_aborted
    exit 1
fi
box_full
box_content "Step 2: Checking directories"
box_full
dir=$logs
checkdir                
dir=$wd
checkdir
box_full
box_content "Step 3: Resultdir"
box_full
echo -n "Cleanup $resultdir..."
cleanup-results
if [ $? -ne 0 ]
then
    print_failure
    echo "Old content cant deleted. But you can go further."
else
    print_okay
fi
box_full
box_content "Step 4: Chroot"
box_full
echo -n "Updating chroot..."
sudo pbuilder --update $dist | tee logs/pbuilder-update-$stamp.txt
if [ $? -ne 0 ]
then
    print_failure
    echo "Updating chroot failed."
    print_aborted
    exit 1
else
    print_okay
fi
box_full
box_content "Step 5: Working dir"
box_full
echo -n "Cleaning up working dir..."
rm -r working-dir/*
if [ $? -ne 0 ]
then
    print_failure
    echo "Cleaning up working dir failed."
else
    print_okay
fi
box_full
box_content "Step 6: Recipe"
box_full
echo -n "Starting recipe to working-dir..."
recipe=$pkg
touch logs/workingdir-$stamp.txt
bzr dailydeb --allow-fallback-to-native $recipe working-dir | tee logs/workingdir-$stamp.txt
if [ $? -ne 0 ]
then
    print_failure
    echo "Recipe failed."
    print_aborted
    exit 1
else
    print_okay
fi
box_full
box_content "Test build"
box_full
echo -n "Starting test build..."
touch logs/testbuild-$stamp.txt
dsc=`ls working-dir/*.dsc`
sudo pbuilder build $dsc | tee logs/testbuild-$stamp.txt
if [ $? -ne 0 ]
then
    print_failure
    echo "Test build failed."
    print_aborted
    exit 1
else
    print_okay
fi
echo "This files are produced now:"
result=`ls $resultdir`
echo $result
exit 0

