#! /bin/sh

# default variable values

PPAURL="http://ppa.launchpad.net/ubuntu-mobile/ubuntu/pool/main"
USERNAME=`whoami`
OUTDIR=`pwd`

print_usage() {
    echo "Usage: ppaprepare [-d outdir] [-u user] project tag"
    echo "Required Arguments:"
    echo "   project: repo name (as full URL or just a project name)"
    echo "   tag: git tag identifying source you want to release"
    echo "Options:"
    echo "   -d: outdir is the directory to place the source tree"
    echo "   -u: user is the login id for the git repo"
    exit 0
}

# argument and option processing

while getopts ":d:u:hf" opt; do
    case $opt in
    d) OUTDIR=$OPTARG;;
    u) USERNAME=$OPTARG;;
    h) print_usage;;
    \?) print_usage;;
    esac
done
shift $(($OPTIND - 1))
if [ $# -lt 2 ]; then
    print_usage
fi

PROJECTURL=""
CHECK1=`echo $1 | sed -n -e "/ssh:\/\/.*\.git/p"`
CHECK2=`echo $1 | sed -n -e "/.*\.git/p" -e "/[\/:]/p"`
if [ -n "$CHECK1" ]; then
    PROJECTURL=$1
    PROJECT=`echo $1 | sed 's/\.git//;s/.*\///'`
elif [ -z "$CHECK2" ]; then
    PROJECT=$1
else
    echo "Malformed project argument, needs to either be a full URL or just a project name"
    exit
fi
TAG=$2

# need a separate directory for the PPA source since
# its tree will have the same name as the git one

PPADIR="${OUTDIR}/ppa-${PROJECT}"
if [ -d $PPADIR ]; then
    rm -rf $PPADIR
fi
mkdir -p $PPADIR

# we want a fresh copy, delete all remnants of previous
# calls to this script

CHECK=`ls -1d ${OUTDIR}/${PROJECT}* 2>/dev/null`
for i in $CHECK; do
    if [ -d $i ]; then
	rm -rf $i
    else
	rm -f $i
    fi
done

# clone the git repo

cd $OUTDIR
if [ -z "$PROJECTURL" ]; then
    git-clone ssh://${USERNAME}@moblin.org/home/repos/projects/${PROJECT}.git $PROJECT
else
    git-clone $PROJECTURL $PROJECT
fi
if [ ! -d ${PROJECT} ]; then
    echo "[ERROR] Failed to clone the repo at ${PROJECT}"
    exit
fi

# switch to the tag the user specified

cd $PROJECT
git-checkout $TAG
if [ $? -ne 0 ]; then 
    echo "Failed to checkout ${TAG}, check if the tag is correct";
    exit;
fi

# determine the git version from the ChangeLog

if [ ! -e ChangeLog ]; then
    echo "[ERROR] No ChangeLog present in ${PROJECT}"
    exit;
fi
GVERSION=`grep -a -o "(.*)" ChangeLog | sed 's/(//;s/)//;2,$d'` 
if [ -z "$GVERSION" ]; then
    echo "Couldn't read the version from the ChangeLog"
    exit
fi

# set the git source directory's name to include the version

cd ..
mv -f $OUTDIR/$PROJECT $OUTDIR/$PROJECT-$GVERSION
GITSRC="${OUTDIR}/${PROJECT}-${GVERSION}"
mv -f $GITSRC/debian $PPADIR/debian-git
mv -f $GITSRC/.git $PPADIR
echo ""
echo "GIT source tree is at ${GITSRC}"

# create a git source tarball minus the debian and .git folders

tar -cvzf $PROJECT-$GVERSION.tar.gz $PROJECT-$GVERSION > /dev/null
md5sum $PROJECT-$GVERSION.tar.gz > $PROJECT-$GVERSION.tar.gz.md5sum

echo "GIT source tarball is ${OUTDIR}/${PROJECT}-${GVERSION}.tar.gz"
echo ""

# find the dsc filename of the current PPA release

TMPFILE=`tempfile`
DSCURL="${PPAURL}/${PROJECT%${PROJECT#?}}/${PROJECT}/"
wget $DSCURL -q -O $TMPFILE
DSCFILE=`grep -a -o "href.*dsc.*</a>" $TMPFILE | sed 's/<.*//;s/.*>//' | sort -rn | head -1`

# extract the PPA version from the dsc filename

PVERSION=`echo $DSCFILE | sed 's/\.dsc//;s/.*_//'`
PVERMAJOR=`echo $PVERSION | sed 's/\(.*\)-.*/\1/'`
rm -f $TMPFILE

if [ "$GVERSION" = "$PVERMAJOR" ]; then
    echo "[ERROR] The GIT version, ${GVERSION}, must be greater than the PPA version, ${PVERMAJOR}"
    echo "Update your ChangeLog with a new version, push the changes, and reapply the tag"
    exit
fi
# download and create the ppa source repo

cd $PPADIR
dget -x $DSCURL/$DSCFILE
PPASRC=$PPADIR/$PROJECT-$PVERMAJOR
echo ""
echo "PPA source tree is at ${PPASRC}"

# run diff for the two source trees and debian dirs

SDIFF=$PPADIR/$PROJECT-ppa$PVERMAJOR-git$GVERSION.diff
DDIFF=$PPADIR/$PROJECT-ppa$PVERMAJOR-git$GVERSION-debian.diff
mv -f $PPASRC/debian $PPADIR/debian-ppa
diff -uNr $PPASRC $GITSRC > $SDIFF
echo "PPA vs GIT source diff is ${SDIFF}"
mv -f $PPADIR/debian-ppa $PPASRC/debian
mv -f $PPADIR/debian-git $GITSRC/debian
mv -f $PPADIR/.git $GITSRC
diff -uNr $PPASRC/debian $GITSRC/debian > $DDIFF
echo "PPA vs GIT debian dir diff is ${DDIFF}"
echo ""

cd $PPASRC
uupdate ${OUTDIR}/${PROJECT}-${GVERSION}.tar.gz
NEWSRC=$PPADIR/$PROJECT-$GVERSION
if [ ! -d $NEWSRC ]; then
    echo "[ERROR] uupdate failed to create ${NEWSRC}"
    exit
elif [ ! -h ${PPADIR}/${PROJECT}_${GVERSION}.orig.tar.gz ]; then
    echo "[ERROR] uupdate failed to create ${PPADIR}/${PROJECT}_${GVERSION}.orig.tar.gz"
    exit
else
    rm -f $PPADIR/$PROJECT_$GVERSION.orig.tar.gz
    cp -f $OUTDIR/$PROJECT-$GVERSION.tar.gz $PPADIR/$PROJECT_$GVERSION.orig.tar.gz
    echo ""
    echo "New PPA src directory at $NEWSRC"
    echo "Make sure $NEWSRC looks ok before uploading"
    echo "When ready, cd to it and run this command:"
    echo "debuild -S -sa -k<gpgkey>"
    echo "Where the gpg key is found via \"gpg --list-keys\":"
    echo "  pub   1024D/<gpgkey> 2008-01-01"
    echo "  uid                  Full Name (My PPA Key) <my.email@mydomain.com>"
    echo "  sub   2048g/58CF328B 2008-01-01"
    echo ""
fi
