#!/bin/bash
#
# Author:  Carsten Grohmann
#
# Version: 0.1
#
# License: GPL
#
# Date:    4th May 2009
#
# Usage:
#       Convert a list of CVS modules or a whole CVS repository in separate
#       Mercurial repositories. Default CVS directories will be skipped.
#
# Syntax:
#  cvs2hg.sh <Module1 [Module2 [...]]>
#
# Example:
#  # Create temporary directory first
#  > mkdir convert && cd convert
#
#  # Checkout whole CVS repository
#  # cvs -d:pserver:<USER>@<CVS-Server>:<absolute path of repository>
#  > cvs -d:pserver:carsten@localhost:/home/cvs/projects/repository
#  > checkout .
#
#  # Write a file named "authors" with the mapping of usernames used in
#  # the CVS repository to Mercurial usernames.
#  # Example:
#  > cat authors
#  carsten=Carsten Grohmann
#
#  # Convert all modules into seprate Mercurial repositiories
#  > cvs2hg.sh *
#

#set -x
#set -v

# General options
HG_CONVERT_OPTS="--datesort --authors=authors"
HG_BIN="hg"

# convert a CVS repository with all modules inside to Mercurial
for module in $@; do

  echo " "

  # skip non-directories
  if [ ! -d $module ]; then
    echo "Skipping $module because it's not a directory"
    continue
  fi
 
  # skip CVSROOT directory
  if [ $(basename "$module") = "CVSROOT" ]; then
    echo "Skipping CVSROOT directory"
    continue
  fi

  # skip CVS directory
  if [ $(basename "$module") = "CVS" ]; then
    echo "Skipping CVS directory"
    continue
  fi

  # convert module
  echo " "
  echo "Starting conversion of $module"
  $HG_BIN convert $HG_CONVERT_OPTS $module

  # check for success
  res=$?
  if [ $res -ne 0 ]; then
    echo " "
    echo "Converting of CVS module $module to Mercurial failed!"
    echo "Press Enter to continue or Ctrl-C to abort"
    read dummy
  else
    echo " "
    echo "Converting of CVS module $module to Mercurial successful!"
  fi
done
