#!/bin/sh

##############################################################################
#
# Script to convert a series of arch branch continuations into a
# monothreaded mercurial dev line
#
# Copyright(C) 2005 Edouard Gomez <ed.gomez@free.fr>
#
# License: public domain
#
# How to use: ./$0
#
# Then you have a freshly xvidcore mainline mercurial repo ready to use in
# ${REPOSITORY_DIR}
#
# Limitations:
#  - single line of development support
#  - doesn't extract the from: information i tried to keep in xvidcore commits
#  - could maybe badly track renames as mercurial is not very clear about how
#    it supports renames, and its semantics
#
# Nice stuff about this script:
#  - should preserve all log comments
#  - should preserve commit dates, thus the history is not time shifted and
#    compacted to the script running timelife 
##############################################################################

export REPOSITORY_DIR=xvidcore.hgrepo

rm -rf "${REPOSITORY_DIR}"

function initial_import
{
	INITIAL_VERSION=$1

	baz get "${INITIAL_VERSION}" "${REPOSITORY_DIR}" >/dev/null 2>&1
	cd "${REPOSITORY_DIR}"
	hg init

 	baz inventory -s | xargs hg add

	PATCH_VERSION=`baz parse-package-name -l ${INITIAL_VERSION}`
	baz log -f -r ${PATCH_VERSION}:${PATCH_VERSION} > .bazlog
	COMMIT_DATE=`cat .bazlog |grep 'Standard-date:' |sed s,'Standard-date: ','',g`
	COMMIT_AUTHOR=`cat .bazlog |grep 'Creator:' | sed s,'Creator: ','',g`
	rm -f .bazlog

	echo "== Import ${INITIAL_VERSION}"
	hg commit -u "${COMMIT_AUTHOR}" -m "Initial import" -d `date -d "${COMMIT_DATE}" '+%s'`
	echo
}

function commit_first_branch_version
{
	VERSION=$1
	echo "== Swithing branch to ${VERSION}"
	baz replay ${VERSION}--base-0 >/dev/null 2>&1
	baz tree-version ${VERSION} >/dev/null 2>&1
	echo
}

function prepare_commit
{
	REVISION=$1
	PATCH_LEVEL=`baz parse-package-name -l $REVISION`

	baz replay ${REVISION} 2>/dev/null | grep -v '{arch}' | grep -v '.arch-ids' | grep -v "^*" >.replay

	ADDED_FILES=`grep '^A[^/]' .replay |awk '{print $2}'`
	MODIFIED_FILES=`grep '^M' .replay |awk '{print $2}'`
	DELETED_FILES=`grep '^D[^/]' .replay |awk '{print $2}'`
	RENAMED_FILES=`grep '^=>' .replay |awk '{print $2 ":" $3;}'`

	for i in ${RENAMED_FILES} ; do
		ORG_FILE=`echo $i | cut -d ':' -f 1`
		DST_FILE=`echo $i | cut -d ':' -f 2`
		echo "R ${ORG_FILE} => ${DST_FILE}"
		hg copy ${ORG_FILE} ${DST_FILE}
		hg remove ${ORG_FILE}
	done

	for i in ${ADDED_FILES} ; do
		hg add ${i}
		echo "A ${i}"
	done

	for i in ${DELETED_FILES} ; do
		hg remove ${i}
		echo "D ${i}"
	done

	for i in ${MODIFIED_FILES} ; do
		echo "M ${i}"
	done

	baz log -f -r ${PATCH_LEVEL}:${PATCH_LEVEL} > .bazlog
	COMMIT_SUMMARY=`cat .bazlog |grep ^Summary: | sed s,'^Summary:[[:space:]]*\(.*\)$','\1',`
	COMMIT_LOG=`cat .bazlog | awk 'BEGIN{start=0} /^$/ {start=1;} // { if (start != 0) print;}' | grep -v '==========='`
	COMMIT_DATE=`cat .bazlog |grep 'Standard-date:' |sed s,'Standard-date: ','',g`
	COMMIT_AUTHOR=`cat .bazlog |grep 'Creator:' | sed s,'Creator: ','',g`
	rm -f .bazlog

	# Commit text forging
	echo "${COMMIT_SUMMARY}" > .log
	echo >> .log
	echo -n "${COMMIT_LOG}" >> .log

	# Commit file list
	echo -n "${MODIFIED_FILES} "> .files
	echo -n "${ADDED_FILES} " >> .files
	echo -n "${DELETED_FILES} " >> .files
	echo -n "${RENAMED_FILES} " | sed s,':',' ',g >> .files
	
	# Commit author
	echo -n "${COMMIT_AUTHOR}" > .author

	# Date commit
	echo -n `date -d "${COMMIT_DATE}" '+%s'` > .date
}

function commit_all_branch_versions
{
	BRANCH=$1
	num_patches=`baz abrowse "${BRANCH}" | grep patch- | sed s,'.*patch-\([0-9]*\)','\1',`
	for i in `seq 1 ${num_patches}` ; do
	        REVISION="${BRANCH}--patch-${i}"
		echo "== Commit ${REVISION} (rev ${global_counter})"
		prepare_commit ${REVISION}
		hg commit -u "`cat .author`" -l .log -d `cat .date` `cat .files`
		global_counter=`expr ${global_counter} '+' 1`
		echo
	done
}

# We have to do the first import explicitly
initial_import "ed.gomez@free.fr--main/xvidcore--devapi4--1.0--base-0"

# Then we will iterate on all continuation branches to switch to the said
# branch and then commit all changesets up to the next branch
BRANCHES="ed.gomez@free.fr--main/xvidcore--devapi4--1.0"
BRANCHES="${BRANCHES} ed.gomez@free.fr--2003-1/xvidcore--devapi4--1.0"
BRANCHES="${BRANCHES} ed.gomez@free.fr--2004-1/xvidcore--devapi4--1.0"
BRANCHES="${BRANCHES} ed.gomez@free.fr--2004-1/xvidcore--head--0.0"
BRANCHES="${BRANCHES} ed.gomez@free.fr--2005-1/xvidcore--head--0.0"

# We use that counter to track the revision number... no use but keeps me
# awaken during full history import
export global_counter=1
for branch in ${BRANCHES} ; do
	commit_first_branch_version "${branch}"
	commit_all_branch_versions "${branch}"
done

