#!/bin/bash

# YAPT-Builder 0.1
#
# This is a little script that can rebuild the complete installed system like can do
# apt-build. I have write this script because apt-build don't works under ppc, so 
# here you have yapt-builder (Yet AnoTher Power Tool Builder)
# feel free to modify this script because it's under GNU License of Free Foundation
# In order to speed and optimize the bandwidth, better to have apt-proxy installed
# Please note. Currently this utility don't install the new packages optimized. 
# It only create a Reposity ready for a manual install with "dpkg -i ..." or 
# "apt-get install --reinstall ..."
#
# 2005-09-02 Massimo Biffi
#########################################################################################

# Here you can add your Optimizations.
# Please check yours best values in http://linuxreviews.org/howtos/compiling/safe-cflags/
# Here you have the best values optimized for a PPC G3 processor 
export CHOST="powerpc-unknow-linux-gnu"
export CFLAGS="-mcpu=750 -02 -pipe -fsigned-char -mpowerpc-gfxopt"
export CXXFLAGS="-mcpu=750 -02 -pipe -fsigned-char -mpowerpc-gfxopt"
export CC="gcc-3.4"

# here you can change the place where compiling the world
BUILDPLACE=/var/World-Builder


##########################################################################################
# Start script - don't touch below if you don't know what are you doing
clear
echo YAPT-Builder 0.1
echo Checking the environment...
CREATION=0
touch $BUILDPLACE/.test 2> /dev/null || CREATION=1
if [ $CREATION = 1 ]; then
    mkdir -p 	$BUILDPLACE/Build
    mkdir 	$BUILDPLACE/Reposity
    mkdir 	$BUILDPLACE/Pacchetti
    mkdir 	$BUILDPLACE/Pool    
    mkdir 	$BUILDPLACE/ToDo
    mkdir 	$BUILDPLACE/Done
    mkdir 	$BUILDPLACE/Processing
fi
CONTINUE=0
if [ -f $BUILDPLACE/installed.list ]; then
    # echo The packages list exist
    CONTINUE=1
else
    echo Getting the list of the installed packages
    # with grep we'll cut out all "doc" and "-dev" packages
    dpkg --get-selections | cut -f1 | grep -v -e "doc" -e "\-dev" > $BUILDPLACE/installed.list
fi
echo "Please wait, i'm cleaning the list..."
cd $BUILDPLACE
# here we'll obtain a cleaned list
if [ $CONTINUE = 0 ]; then
    for i in `cat installed.list`; do
        touch Pacchetti/$i
	echo "*" | tr -d '\n'
    done
    cd Pacchetti 
    for p in *; do
        mkdir ../Pool/$p
        mv "$p"-* ../Pool/$p/ 2> /dev/null
        mv $p ../Pool/$p/ 2> /dev/null
	echo "*" | tr -d '\n'
    done
    cd ../Pool
    for D in *; do
        cd $D
        LIST=`ls | wc -l | tr -d ' '`
        cd ../
        if [ $LIST = "0" ]; then
	    rm -rf $D
        fi
	echo "*" | tr -d '\n'
    done
    for L in *; do
        cd $L
        Pack=`ls | head -n1`
        mv $Pack ../../ToDo/
        cd ../
	echo "*" | tr -d '\n'
    done
    cd ../
    rm -rf Pool Pacchetti
fi    

# the party is starting...
cd $BUILDPLACE/Build
for package in ../ToDo/*; do
    mv ../ToDo/$package $BUILDPLACE/Processing/
    sourcename=`basename $package`
    # now we install the build dependences or exit
    apt-get -y --force-yes build-dep $sourcename || exit
    # here we try to compiling the packagename or exit
    apt-get source -b $sourcename || exit    
    # moving the .deb in the Reposity
    mv *deb ../Reposity
    rm -rf *
    cd ../Reposity
    # and now we'll create a Packages.gz for apt-get
    dpkg-scanpackages . /dev/null | gzip -c9 > Packages.gz
    cd ../Build
    mv ../Processing/$sourcename ../Done/
    apt-get clean
done

# add the correct line to /etc/apt/sources.list if needed, and update with apt-get update
APTCHECK=`cat /etc/apt/sources.list | grep $BUILDPLACE | wc -l`
if [ $APTCHECK = 0 ]; then
    mv /etc/apt/sources.list /etc/apt/sources.tmp
    echo "deb file:$BUILDPLACE/Repository/ ./" > /etc/apt/sources.list
    cat /etc/apt/sources.tmp >> /etc/apt/sources.list
    rm /etc/apt/sources.tmp
fi
apt-get update    
echo "Finish!...at last!"
