#!/bin/sh

BASEDIR=/pkgs/bzr
CHROOTDIR=$BASEDIR/chroot

# Location of the repository
REPO=$CHROOTDIR/repos/packages

# Location of embargo repo
EMBARGO=$CHROOTDIR/repos/embargo

# Location of the repository we're going to pull in.
OLDREPO=$BASEDIR/cvsextras/devel

# Sample package data.

# Location of the main working branch from which our other branches will be
# created.  In Fedora cvs, this is devel which maps to rawhide.  Here, we
# attempt to call it trunk in order to produce the mentality that it is the
# branch that should be worked on first.  If this is undesirable we can rename
# it to devel to go back to the old mode of thinking.
TRUNK=trunk

# Temporary location.  We are going to create our toplevel branches and make
# checkouts here that we commit to the repository.
TMPDIR=/pkgs/bzr/tmp


echo "Creating $REPO as a repository"
mkdir -p $REPO
cd $REPO
bzr init-repo .

# Setup common as our initial repository branch.  Instead of having a module
# as in cvs, we setup common as a project from which everything else in the
# repository will branch.  Individual packages are not supposed to modify
# the common files as local changes will be overwritten with the next update
# of the common files.  These updates are handled by the Fedora admin scripts.
# bzr and other DRCS's don't have a concept of push to all branches (as
# branches will occur on computers outside of the parent's control) so our
# admin scripts will run a pull from each subproject's branch.
#
echo "Adding the common files to the repo"
# When we have a Smart Server, this can be a directory instead.
bzr init-repo comon
pushd common
bzr init $TRUNK
pushd $TMPDIR
bzr checkout $REPO/common/$TRUNK common
cp -rp $OLDREPO/common common
rm -rf common/CVS
cp -p $OLDREPO/quilt/Makefile common/
pushd common
bzr add
bzr commit -m "Import common files"
popd
rm -rf common
popd
popd

echo "Branching for all packages"
cd $REPO
for pkg in `ls $OLDREPO`; do
	if [ x$pkg != "xcommon" ]; then
		# When we have a Smart Server, this can be a directory instead.
		bzr init-repo $pkg
		pushd $pkg
		bzr branch $REPO/common/$TRUNK $TRUNK
		pushd $TMPDIR
		bzr checkout $REPO/$pkg/$TRUNK $pkg
		cp -p $OLDREPO/$pkg/* $pkg/
		pushd $pkg
		bzr add
		bzr commit -m "Initial import of $pkg to the new repository"
		popd
		rm -rf $pkg
		popd
		popd
	fi
done

echo "Branching packages for a Fedora release"
cd $REPO
for pkg in `ls $REPO`; do
	pushd $pkg
	bzr branch $TRUNK FC-5
	popd
done

echo "Branching embargoed branches for select packages: qa-assistant"
mkdir -p $EMBARGO
setfacl --set-file acls/embargo $EMBARGO
setfacl -d --set-file acls/embargo.default $EMBARGO
cd $EMBARGO
for pkg in qa-assistant; do
	bzr branch $REPO/$pkg/$TRUNK $pkg-1234
done
