#!/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

set -u
set -e

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

mail_in()
{
    local line
    local tmpline
    local header
    local value

    from=
    message_id=
    subject=
    reply_to=

    while IFS= read -r tmpline
    do
        if [[ "$tmpline" =~ ^[[:space:]] ]]
        then
            line="$line$tmpline"
        else
            if [ -n "$line" ]
            then
                IFS=' ' read -r header value <<< "$line"
                case "$header" in
                    [Ff][Rr][Oo][Mm]: )
                        from=${value//\"/\\\"} #"
                        ;;
                    [Mm][Ee][Ss][Ss][Aa][Gg][Ee]-[Ii][Dd]: )
                        message_id=${value//\"/\\\"} #"
                        ;;
                    [Ss][Uu][Bb][Jj][Ee][Cc][Tt]: )
                        subject=${value//\"/\\\"} #"
                        ;;
                    [Rr][Ee][Pp][Ll][Yy]-[Tt][Oo]: )
                        reply_to=${value//\"/\\\"} #"
                        ;;
                esac
            fi
            line=$tmpline
            if [ -z "$line" ]
            then
                break
            fi
        fi
    done

    return 0
}

$LOGGER -p syslog.info $BASENAME: starting

set +e
makedirs
set -e
mail_in

regex="^.*tp://.+\.dsc$"
if [[ "$subject" =~ $regex ]]
then
    dsc=${subject##*/}
    url=${subject%/*}
    pkg=${dsc%_*}
    pdv=${dsc%\.dsc}
    log=$ELIDAHOME/source/$pkg/${pdv}_$ARCH.source.log
    sig=$(mktemp $TMP-XXXXXX)
    md5=$(mktemp $TMP-XXXXXX)
    dif=$(mktemp $TMP-XXXXXX)
    tmp=$(mktemp $TMP-XXXXXX)
    set +e
    makedir $ELIDAHOME/source/$pkg
    set -e
    if [ -e $log.bz2 ]
    then
        bunzip2 -f $log.bz2
    fi
    if [ -e $log ]
    then
        sed -e '/^Format: 1.0$/,$ d' $log > $tmp
        rm -f $log
        mv $tmp $log
    else
        touch $log
    fi
    chmod 664 $log
    date -R >> $log
    echo >> $log
    set +e
    wget -nH -N -P $ELIDAHOME/source/$pkg/ $subject >> $log 2>&1
    set -e
    if [ ! -f $ELIDAHOME/source/$pkg/$dsc ]
    then
        $LOGGER -p syslog.crit $BASENAME: $dsc download error
        (echo -e "Subject: Re: $subject\nTo: $from\nFrom: $FROM\nBcc: $BCC\nIn-Reply-To: $message_id\nUser-Agent: elida/$ELIDAVERSION\n"; cat $log) | $SENDMAIL -t
        exit 0
    fi
    chmod 664 $ELIDAHOME/source/$pkg/$dsc
    set +e
    su - $USERNAME -c "gpg --homedir $GPGHOMEDIR --no-permission-warning" < $ELIDAHOME/source/$pkg/$dsc > $tmp 2> $sig
    rc=$(grep -c "^gpg: Good signature from " $sig)
    set -e
    if [ $rc -ne 1 ]
    then
        cat $tmp $sig >> $log
        echo >> $log
        $LOGGER -p syslog.crit $BASENAME: gpg error
        (echo -e "Subject: Re: $subject\nTo: $from\nFrom: $FROM\nBcc: $BCC\nIn-Reply-To: $message_id\nUser-Agent: elida/$ELIDAVERSION\n"; cat $tmp $sig) | $SENDMAIL -t
        exit 0
    fi

    files=$(grep -E "^ [0-9a-f]{32} [0-9]+ .*\.(diff|tar)\.gz$" $tmp | sed "s/.* //")
    for file in $files
    do
        if [ $file != ${dsc%-*}.orig.tar.gz ]
        then
            set +e
            wget -nH -N -P $ELIDAHOME/source/$pkg/ $url/$file >> $log 2>&1
            set -e
        else
            if [ ! -e $ELIDAHOME/source/$pkg/$file ]
            then
                if [[ $pkg =~ ^lib ]]
                then
                    sdir=${pkg:0:4}
                else
                    sdir=${pkg:0:1}
                fi
                for mirror in $MIRRORS
                do
                    if [ ! -e $ELIDAHOME/source/$pkg/$file ]
                    then
                        set +e
                        wget -nH -N -P $ELIDAHOME/source/$pkg/ $mirror/pool/main/$sdir/$pkg/$file >> $log 2>&1
                        set -e
                    fi
                done
                if [ ! -e $ELIDAHOME/source/$pkg/$file ]
                then
                    set +e
                    wget -nH -N -P $ELIDAHOME/source/$pkg/ $url/$file >> $log 2>&1
                    set -e
                fi
            fi
        fi
        chmod 664 $ELIDAHOME/source/$pkg/$file
    done
    cat $tmp $sig >> $log
    echo -e '\nMD5s:' >> $log
    (cd $ELIDAHOME/source/$pkg; md5sum $dsc $files) >> $log
    echo >> $log
    sed -e '1,/^Files:/ d' $tmp | cut -d\  -f2,4 | sed -e 's/ /  /' > $md5
    cd $ELIDAHOME/source/$pkg
    set +e
    md5sum --check $md5 >> $log 2>&1
    rc=$?
    set -e
    if [ ! $rc ]
    then
        $LOGGER -p syslog.crit $BASENAME: md5sum error rc=$rc
        exit $rc
    fi

    cat > $ELIDAHOME/pending/$pdv << FIN
from="$from"
message_id="$message_id"
subject="$subject"
reply_to="$reply_to"
FIN
    mv $ELIDAHOME/pending/$pdv $ELIDAHOME/ready/$pdv

    echo >> $log
    set +e
    apt-get --download-only source $pkg >> $log
    set -e

    if [ -e $pdv.diff.gz ]
    then
        echo >> $log
        echo "diffstat $pdv.diff.gz" >> $log
        diffstat $pdv.diff.gz >> $log
    fi

    diffs=$(ls -1rt *.diff.gz | tail -2)
    rc=$(wc -w <<< $diffs)
    if [ $rc -eq 2 ]
    then
        set +e
        interdiff -zp1 $diffs > $dif 2>&1
        set -e
        echo >> $log
        echo interdiff -zp1 $diffs >> $log
        diffstat < $dif >> $log
        echo >> $log
        cat $dif >> $log
    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
fi

exit 0
