#!/bin/bash

# Source makepkg.conf; fail if it is not found
if [ -r "/etc/makepkg.conf" ]; then
	source "/etc/makepkg.conf"
else
    echo "/etc/makepkg.conf not found!"
    exit 1
fi

# Source user-specific makepkg.conf overrides
if [ -r ~/.makepkg.conf ]; then
	source ~/.makepkg.conf
fi

cmd=`basename $0`

if [ ! -f PKGBUILD ]; then
    echo "No PKGBUILD file"
    exit 1
fi

# define tags and staging areas based on architecture
if [ "$CARCH" = "i686" ]; then
    currenttag='CURRENT'
    testingtag='TESTING'
    suffix=''
elif [ "$CARCH" = "x86_64" ]; then
    currenttag='CURRENT-64'
    testingtag='TESTING-64'
    suffix='64'
else
    echo "CARCH must be set to a recognized value!"
    exit 1
fi

source PKGBUILD
pkgfile=${pkgname}-${pkgver}-${pkgrel}-${CARCH}.pkg.tar.gz
oldstylepkgfile=${pkgname}-${pkgver}-${pkgrel}.pkg.tar.gz

if [ ! -f $pkgfile ]; then
    if [ -f $PKGDEST/$pkgfile ]; then
        pkgfile=$PKGDEST/$pkgfile
        oldstylepkgfile=$PKGDEST/$oldstylepkgfile
    elif [ -f $oldstylepkgfile ]; then
        pkgfile=$oldstylepkgfile
    elif [ -f $PKGDEST/$oldstylepkgfile ]; then
        pkgfile=$PKGDEST/$oldstylepkgfile
    else
        echo "File $pkgfile doesn't exist"
        exit 1
    fi
fi

if [ "$cmd" == "extrapkg" ]; then
    repo="extra"
    tag="$currenttag"
elif [ "$cmd" == "corepkg" ]; then
    repo="core"
    tag="$currenttag"
elif [ "$cmd" == "testingpkg" ]; then
    repo="testing"
    tag="$testingtag"
elif [ "$cmd" == "unstablepkg" ]; then
    repo="unstable"
    tag="$currenttag"
elif [ "$cmd" == "communitypkg" ]; then
    repo="community"
    tag="$currenttag"
fi

# see if any limit options were passed, we'll send them to SCP
unset scpopts
if [ "$1" = "-l" ]; then
    scpopts="$1 $2"
    shift 2
fi

if [ "$repo" != "community" ]; then
    # combine what we know into a variable (suffix defined based on $CARCH)
    uploadto="staging/${repo}${suffix}/add/$(basename ${pkgfile})"
    scp ${scpopts} "${pkgfile}" "archlinux.org:${uploadto}"
    if [ "$(md5sum "${pkgfile}" | cut -d' ' -f1)" != "$(ssh archlinux.org md5sum "${uploadto}" | cut -d' ' -f1)" ]; then
        echo "File got corrupted during upload, cancelled."
        exit 1
    else
        echo "File integrity okay."
    fi
else
    if [ ! -f ~/.tupkg ]; then
        echo "Must configure tupkg via ~/.tupkg, cancelled"
        exit 1
    fi
    if [ "$(basename $pkgfile)" != "$(basename $oldstylepkgfile)" ]; then
        echo "Renaming makepkg3 package for compatability"
        mv $pkgfile $oldstylepkgfile
        pkgfile=$oldstylepkgfile
    fi
    tupkg $pkgfile
fi
if [ $? -ne 0 ]; then
    echo "Cancelled"
    exit 1
fi
echo "===> Uploaded $pkgfile"

if [ "$1" != "" ]; then
    cvs commit -m "upgpkg: $pkgname $pkgver-$pkgrel
    $1" > /dev/null
    if [ $? -ne 0 ]; then
        echo "Cancelled"
        exit 1
    fi
    echo "===> Commited with \"upgpkg: $pkgname $pkgver-$pkgrel
    $1\" message"
else
    cvs commit -m "upgpkg: $pkgname $pkgver-$pkgrel" > /dev/null
    if [ $? -ne 0 ]; then
        echo "Cancelled"
        exit 1
    fi
    echo "===> Commited with \"upgpkg: $pkgname $pkgver-$pkgrel\" message"
fi

cvs tag -c -F -R $tag > /dev/null
if [ $? -ne 0 ]; then
    echo "Cancelled"
    exit 1
fi
echo "===> Tagged as $tag"

# vim:ft=sh:ts=4:sw=4:et: