#!/bin/bash

# Copyright 2007-2009 Anibal Monsalve Salazar <anibal@debian.org>
#
# 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

. /etc/elida/elida.conf
. $FUNCTIONS

$LOGGER -p syslog.info $BASENAME: starting

makedirs

piuparts_mirrors=
pbuilder_mirrors=
piuparts_bindmounts=
pbuilder_bindmounts=
regex="^file:(.*)"
for mirror in $MIRRORS
do
    if [ -z "$pbuilder_mirrors" ]
    then
        pbuilder_mirrors="deb $mirror sid main"
        piuparts_mirrors="-m '$mirror main'"
    else
        pbuilder_mirrors="$pbuilder_mirrors | deb $mirror sid main"
        piuparts_mirrors="$piuparts_mirrors -m '$mirror main'"
    fi

    if [[ $mirror =~ $regex ]]
    then
        if [ -z "$pbuilder_bindmounts" ]
        then
            pbuilder_bindmounts="${BASH_REMATCH[1]}"
            piuparts_bindmounts="--bindmount ${BASH_REMATCH[1]}"
        else
            pbuilder_bindmounts="$pbuilder_bindmounts ${BASH_REMATCH[1]}"
            piuparts_bindmounts="$piuparts_bindmounts --bindmount ${BASH_REMATCH[1]}"
        fi
    fi
done

pbuilder_mirrors="'$pbuilder_mirrors'"

if [ -n "$pbuilder_bindmounts" ]
then
    pbuilder_bindmounts="'$pbuilder_bindmounts'"
    pbuilder_bindmounts_option="--bindmounts"
fi

tmp=$(mktemp $TMP-XXXXXX)

if [ -n "$(ls $ELIDAHOME/inprogress)" ]; then
    mv $ELIDAHOME/inprogress/* $ELIDAHOME/ready
fi

while [ ! -f $ELIDAHOME/elida-stop ]
do
    #dt=$(perl -e '$dt=time-(stat("'$ELIDAHOME/pbuilder_update_unstable.log'"))[9];print"$dt\n"')
    if [ -f $ELIDAHOME/pbuilder_update_unstable.log ]; then
        dt=$(( $( date +%s ) - $( date --reference=$ELIDAHOME/pbuilder_update_unstable.log +%s ) ))
    else
        dt=9999999
    fi
    if [ $dt -gt 10800 ]; then
        log=$ELIDAHOME/pbuilder_update_unstable.log
        echo pbuilder update --override-config --distribution unstable $pbuilder_bindmounts_option $pbuilder_bindmounts --othermirror $pbuilder_mirrors >> $log
        eval pbuilder update --override-config --distribution unstable $pbuilder_bindmounts_option $pbuilder_bindmounts --othermirror $pbuilder_mirrors >> $log 2>&1
        chmod 664 $log
        chown $USERNAME:$GROUPNAME $log
    fi
   
    for pdv in $(ls -1rt $ELIDAHOME/ready)
    do
        [ -f $ELIDAHOME/elida-stop ] && break

        log=$ELIDAHOME/apt-get_update.log
        apt-get update > $log 2>&1
        chmod 664 $log
        chown $USERNAME:$GROUPNAME $log

        [ -f $ELIDAHOME/ready/$pdv ] || continue

        mv $ELIDAHOME/ready/$pdv $ELIDAHOME/inprogress/$pdv
        pkg=${pdv%_*}
        ver=${pdv#*_}
        . $ELIDAHOME/inprogress/$pdv

        if [ -d "$ELIDAHOME/result/$pkg" ]; then
            dsc_old=$( echo $ELIDAHOME/result/$pkg/${pkg}_*.dsc )
            if [ -f "$dsc_old" ]; then
                pdv_old=${dsc_old##*/}
                pdv_old=${pdv_old%*.dsc}
                if [ "$pdv" == "$pdv_old" ]; then
                    rm -fr $ELIDAHOME/result/$pkg/*
                else
                    rm -fr $ELIDAHOME/result/$pdv_old
                    mv $ELIDAHOME/result/$pkg $ELIDAHOME/result/$pdv_old
                    makedir $ELIDAHOME/result/$pkg
                fi
            else
                rm -fr $ELIDAHOME/result/$pkg/*
            fi
        else
            makedir $ELIDAHOME/result/$pkg
        fi

        log=$ELIDAHOME/result/$pkg/${pdv}_$ARCH.result.log
        rm -f $ELIDAHOME/result/$pkg/*$ver*

        date -R > $log
        chmod 664 $log
        echo $(hostname; dpkg --print-architecture; dpkg -l elida lintian piuparts pbuilder | grep ^ii | awk '{print $2,$3}') >> $log
        echo >> $log
        echo pbuilder build --override-config --distribution unstable $pbuilder_bindmounts_option $pbuilder_bindmounts --othermirror $pbuilder_mirrors --buildresult $ELIDAHOME/result/$pkg $ELIDAHOME/source/$pkg/$pdv.dsc "2>&1 | grep -v 'using uid of process'" >> $log
        eval pbuilder build --override-config --distribution unstable $pbuilder_bindmounts_option $pbuilder_bindmounts --othermirror $pbuilder_mirrors --buildresult $ELIDAHOME/result/$pkg $ELIDAHOME/source/$pkg/$pdv.dsc 2>&1 | grep -v 'using uid of process' >> $log
        chmod 2775 $ELIDAHOME/result $ELIDAHOME/result/$pkg
        chmod 664 $ELIDAHOME/result/$pkg/*
        chown $USERNAME:$GROUPNAME $ELIDAHOME/result/$pkg/*

        if [ -e $ELIDAHOME/result/$pkg/${pdv}_$ARCH.changes ]; then
            echo >> $log
            echo "$ELIDAHOME/result/$pkg/${pdv}_$ARCH.changes:" >> $log
            cat $ELIDAHOME/result/$pkg/${pdv}_$ARCH.changes >> $log

            for f in $ELIDAHOME/result/$pkg/*$ver*deb
            do
                echo >> $log
                echo "dpkg -I $f" >> $log
                dpkg -I $f >> $log 2>&1
            done

            for f in $ELIDAHOME/result/$pkg/*$ver*deb
            do
                echo >> $log
                echo "dpkg -c $f" >> $log
                dpkg -c $f >> $log 2>&1
            done

            if [ -x "$LOCALARCHIVESCRIPT" ]
            then
                su - $USERNAME -c "$LOCALARCHIVESCRIPT $pkg"
            fi

            for f in $ELIDAHOME/result/$pkg/*$ver*deb
            do
                echo >> $log
                echo piuparts -p -N $piuparts_bindmounts $piuparts_mirrors $f "2>&1 | grep -v '/dev/pts not mounted'" >> $log
                eval piuparts -p -N $piuparts_bindmounts $piuparts_mirrors $f 2>&1 | grep -v '/dev/pts not mounted' >> $log 2>&1
            done

            echo >> $log
            echo "lintian -i --show-overrides --pedantic $ELIDAHOME/result/$pkg/${pdv}_$ARCH.changes" >> $log
            su - $USERNAME -c "lintian -i --show-overrides --pedantic $ELIDAHOME/result/$pkg/${pdv}_$ARCH.changes" >> $log 2>&1
        fi

        bzip2 -f -9 $log
        log="$log.bz2"

        rm -f $tmp
        mpack -s "Re: $subject" -o $tmp $log
        (echo -e "To: $from\nFrom: $FROM\nBcc: $BCC\nIn-Reply-To: $message_id\nUser-Agent: elida/$ELIDAVERSION"; cat $tmp) | $SENDMAIL -t

        mv $ELIDAHOME/inprogress/$pdv "$ELIDAHOME/done/$pdv"

        if [ -f $ELIDAHOME/elida-hup ]; then
            rm -f $ELIDAHOME/elida-hup
            break
        fi
    done
    sleep 10
done

rm -f $ELIDAHOME/elida-stop

exit 0
