-#/bin/sh
+#/bin/bash
+## package-update.sh -- Create a package archive from the elpa repository
-/bin/rm /home/elpa/update-log
+## Usage: ./package-update.sh DEST [FULL-UPDATE]
+##
+## This creates a package archive beginning in DEST.
+##
+## The package archive itself is created in DEST/packages. This dir
+## contains the package files.
+##
+## If a second argument FULL-UPDATE is specified (whatever its value),
+## also create the following:
+## - the archive admin scripts in DEST/admin
+## - a tarball containing the entire archive in
+## DEST/packages/emacs-packages-latest.tgz
+## - the Org mode daily package
-## Create emacs-packages-latest.tgz
-/usr/bin/bzr export --format=tgz --root=emacs-24.1-packages-`/bin/date +'%F'` /home/elpa/emacs-packages-latest.tgz /home/elpa/package-repo
+PATH="/bin:/usr/bin:/usr/local/bin:${PATH}"
+## Remove any trailing slash from DEST
+DEST=${1%/}
+FULL=$2
-## Create the world-facing copy
-rm -rf /home/elpa/packages-new
-/usr/bin/bzr export /home/elpa/packages-new /home/elpa/package-repo
+EMACS=emacs
+BZR=bzr
+TAR=tar
+REPO_PACKAGES=packages
-# Copy the org daily package from orgmode.org
-/home/elpa/bin/org-synch.sh
+## Parse arguments
+if [ -z $DEST ]; then
+ echo "Syntax: $0 DEST [fetch-extras-boolean]"
+ exit 1
+elif [ -d $DEST ]; then
+ cd $DEST
+ DEST_FULL=$(pwd)
+ PKGROOT=$DEST_FULL/packages
+ TMP_PKGROOT=$DEST_FULL/packages-new
+ echo "Installing into '$DEST_FULL'"
+ if [ -z $FULL ]; then
+ echo "Base archive update only (pass second arg for full update)."
+ else
+ echo "Performing full archive update."
+ TARBALL=$PKGROOT/emacs-packages-latest.tgz
+ TARBALL_ROOT="emacs-24.1-packages-`/bin/date +'%F'`"
+ ADMINROOT=$DEST_FULL/admin
+ REPO_ADMIN=admin
+ fi
+else
+ echo "Sorry but $DEST is not a directory, aborting."
+ exit 1
+fi
-/bin/mv /home/elpa/packages /home/elpa/packages-old
-/bin/mv /home/elpa/packages-new /home/elpa/packages
-/bin/rm -rf /home/elpa/packages-old
+## Change to the bzr root directory
+REPO_ROOT_DIR=`$BZR root`;
+if [ -z $REPO_ROOT_DIR ]; then
+ "This script should be run from a bzr repository, aborting."
+ exit 1
+else
+ cd $REPO_ROOT_DIR;
+fi
-/bin/chmod -R a+rX /home/elpa/packages
+## Create the working directory that will be the world-facing copy of
+## the package archive base.
+echo "Exporting packages to temporary working directory $TMP_PKGROOT"
+rm -rf $TMP_PKGROOT
+$BZR export $TMP_PKGROOT $REPO_PACKAGES
-/bin/echo "Update complete at" `/bin/date` >> /home/elpa/update-log
+## If second arg is provided, copy in the admin directory.
+if [ -z $FULL ]; then
+ echo "Skipping admin directory"
+else
+ echo "Exporting admin scripts to $ADMINROOT"
+ rm -rf $ADMINROOT
+ $BZR export $ADMINROOT $REPO_ADMIN
+fi
+
+
+cd $TMP_PKGROOT
+
+
+## If second arg is provided, grab the Org daily
+if [ -z $FULL ]; then
+ echo "Not fetching Org daily from orgmode.org"
+else
+ echo "Fetching Org daily from orgmode.org"
+ pkgname=`curl -s http://orgmode.org/pkg/daily/|perl -ne 'push @f, $1 if m/(org-\d{8})\.tar/; END { @f = sort @f; print "$f[-1]\n"}'`
+ wget -q http://orgmode.org/pkg/daily/${pkgname}.tar -O ${pkgname}.tar
+ if [ -f ${pkgname}.tar ]; then
+ tar xf ${pkgname}.tar
+ rm -f ${pkgname}.tar
+ mv ${pkgname} org
+ fi
+fi
+
+## Call `batch-make-archive' to generate archive-contents, the readme
+## files, etc.
+$EMACS -batch -l $REPO_ROOT_DIR/admin/archive-contents.el -f batch-make-archive
+
+## Tar up the multi-file packages.
+echo "Creating multi-file package tarballs in $TMP_PKGROOT"
+for pt in *; do
+ if [ -d $pt ]; then
+ echo "Creating tarball $TMP_PKGROOT/$pt.tar"
+ tar -cf $pt.tar $pt --remove-files
+ fi
+done
+
+## Move the working directory to its final location
+cd ..
+rm -rf $PKGROOT-old
+if [ -d $PKGROOT ]; then
+ mv $PKGROOT $PKGROOT-old
+fi
+mv $TMP_PKGROOT $PKGROOT
+rm -rf $PKGROOT-old
+
+## If doing a full update, make a tarball of the entire archive.
+if [ -z $FULL ]; then
+ echo "Skipping archive tarball"
+else
+ echo "Exporting packages into $TARBALL (root = $TARBALL_ROOT)"
+ cd $REPO_ROOT_DIR
+ $BZR export --format=tgz --root=$TARBALL_ROOT $TARBALL $REPO_PACKAGES
+fi
+
+chmod -R a+rX $PKGROOT
+echo "Update complete at" `/bin/date`