#!/bin/sh


####
# Script to checkout/update base sources from both trunk (ToT) and the current
# release tag (as determined by the base/config/RELEASE_URL file) and a ports
# tree from trunk (ToT), and then export and sync all of them to the
# ${RSYNCROOT} location, wherefrom the rsync modules are fed to the `sync'
# and `selfupdate' routines in port(1). Read the base/portmgr/rsync.repos
# file for more information on both the necessary rsync modules and filesystem
# level paths, which this script bootstraps.
#
# Whatever server uses this script to mirror the MacPorts rsync repositories
# should simply adapt the ${RSYNCROOT} path variable as necessary (keeping it
# in sync with the equally named variable in base/portmgr/rsync.repos) and
# install it on cron/launchd with a suitable periodicity, previously discussed
# with the portmgr@ team (macports-mgr@lists.macosforge.org). Repositories
# themselves are detailed & served by base/portmgr/rsync.repos, as stated above
# (that is, no manual intervention what-so-ever is needed, other than installing
# this script and adding the repositories detailed in base/portmgr/rsync.repos
# to a local rsyncd.conf file).
#
# Lastly, it is required of every 3rd party mirrors to keep track of this script
# and the base/portmgr/rsync.repos file and always maintain local copies in as
# close sync as possible. 
#
#
# Created by fkr@opendarwin.org, jberry@macports.org and yeled@macports.org,
# Updated by jmpp@macports.org
# $Id: mprsyncup 38495 2008-07-22 18:09:32Z wsiegrist@apple.com $
####


set -e

# Commands we need:
SVN="/opt/local/bin/svn -q --non-interactive"
RSYNC="/usr/bin/rsync -q"
RM="/bin/rm"
MKDIR="/bin/mkdir"
LN="/bin/ln"

# Paths we'll work on:
SVNROOT=/var/tmp/macports
TBASE=${SVNROOT}/trunk/base
RBASE=${SVNROOT}/release/base
PORTS=${SVNROOT}/release/ports
RSYNCROOT=/Volumes/data/rsync/macports

# Sources information:
SVNURL=http://svn.macports.org/repository/macports
RELEASE_URL_FILE=config/RELEASE_URL


# Update/checkout trunk's base, export and rsync it to the rsync repos location (${RSYNCROOT}):
if [ -d ${TBASE}/.svn ]; then
    ${SVN} update ${TBASE}
else
    ${SVN} checkout ${SVNURL}/trunk/base ${TBASE}
fi

# Delete old exports if they exist
if [ -d ${TBASE}-export ]; then
    $RM -rf ${TBASE}-export;
fi
${SVN} export ${TBASE} ${TBASE}-export

if [ ! -d ${RSYNCROOT}/trunk/base ]; then
    ${MKDIR} -p ${RSYNCROOT}/trunk
fi
${RSYNC} -a -I --delete ${TBASE}-export/ ${RSYNCROOT}/trunk/base && ${RM} -rf ${TBASE}-export


# Read what tag we're releasing from, switch to/checkout a copy, export and rsync it to ${RSYNCROOT}/release/base:
read RELEASE_URL < ${TBASE}/${RELEASE_URL_FILE}
if [ ! -n ${RELEASE_URL} ]; then
    echo "no RELEASE_URL specified in svn trunk, baling out!"
    exit 1
fi
if [ -d ${RBASE}/.svn ]; then
    ${SVN} switch ${RELEASE_URL} ${RBASE}
else
    ${SVN} checkout ${RELEASE_URL} ${RBASE}
fi

# Delete old exports if they exist
if [ -d ${RBASE}-export ]; then
    $RM -rf ${RBASE}-export;
fi
${SVN} export ${RBASE} ${RBASE}-export

if [ ! -d ${RSYNCROOT}/release/base ]; then
    ${MKDIR} -p ${RSYNCROOT}/release/base
fi
${RSYNC} -a -I --delete ${RBASE}-export/ ${RSYNCROOT}/release/base && ${RM} -rf ${RBASE}-export


# Update/checkout the ports tree, export it and rsync it to ${RSYNCROOT}/release/ports:
if [ -d ${PORTS}/.svn ]; then
  ${SVN} update ${PORTS}  
else
    ${SVN} checkout ${SVNURL}/trunk/dports ${PORTS}
fi

# Delete old exports if they exist
if [ -d ${PORTS}-export ]; then
    $RM -rf ${PORTS}-export;
fi
${SVN} export ${PORTS} ${PORTS}-export

if [ ! -d ${RSYNCROOT}/release/ports ]; then
    ${MKDIR} -p ${RSYNCROOT}/release/ports
fi
${RSYNC} -a -I --delete ${PORTS}-export/ ${RSYNCROOT}/release/ports && ${RM} -rf ${PORTS}-export


# Compatibility checks:
cd ${RSYNCROOT}
if [ ! -h dpupdate ]; then
    ${RM} -rf dpupdate && ${LN} -s trunk dpupdate
fi
if [ ! -h dpupdate1 ]; then
    ${RM} -rf dpupdate1 && ${LN} -s release dpupdate1
fi
if [ ! -h trunk/dports ]; then
    cd trunk
    ${RM} -rf dports && ${LN} -s ../release/ports dports
fi
