#!/bin/bash

##########################################################################
# CONFIGURATION SETTINGS
##########################################################################

# Where to put your _standard_ mahara git repository
MAHARA_GIT_DIR="/usr/src/mahara/mahara"

# Where to put your _language pack_ mahara git repository
LANG_PACK_GIT_DIR="/usr/src/mahara/git-repos/mahara-eu"

# The code of your language pack (es, eu, fr, ...)
LANG_PACK="es"

# The real name of your language pack (only used for the initial commits).
LANG_NAME="Spanish"

# What git branches you are insterested in translating. _YOU NEED TO
# SPECIFY "master" AT LEAST_. At the time of this writing 1.0_STABLE
# is also available for 1.0.x Mahara versions.
GIT_BRANCHES="master 1.0_STABLE"


##########################################################################
#
# NO USER SERVICEABLE PARTS BELOW THIS LINE :-)
#
##########################################################################

# We don't have dirname available on Windows (msysgit), so emulate it
dirname ()
{
	# We do have basename, luckily :-)
	basename=`basename $1`
	echo ${1%/${basename}}
}

check_repo_dir()
{
	if [ -d $2 ]
	then
		echo "$1 ($2) already exits. Stopping now."
		return 1
	fi
	return 0
}

# Use forward slashes in paths, even if we are on Windows. msysgit (ang Cygwin) have
# less trouble with them.
MAHARA_GIT_DIR=`echo $MAHARA_GIT_DIR | tr '\\\\' '/' `
LANG_PACK_GIT_DIR=`echo $LANG_PACK_GIT_DIR | tr '\\\\' '/'`

if [ $MAHARA_GIT_DIR == $LANG_PACK_GIT_DIR ]
then
	echo "MAHARA_GIT_DIR and LANG_PACK_GIT_DIR can't be the same directory"
	exit 1
fi

check_repo_dir "MAHARA_GIT_DIR" $MAHARA_GIT_DIR || exit 1
check_repo_dir "LANG_PACK_GIT_DIR" $LANG_PACK_GIT_DIR || exit 1

mahara_dir_name=`basename $MAHARA_GIT_DIR`
mahara_top_dir=`dirname $MAHARA_GIT_DIR`
mahara_dir=${mahara_top_dir}/${mahara_dir_name}

echo "Cloning the standard mahara repository..."
mkdir -p $mahara_top_dir
cd $mahara_top_dir
# Clone the repository but don't checkout the master head yet.
# This gives us change to set core.autocrlf and core.safecrlf
# below before breaking anything.
git clone -n git://git.mahara.org/git/mahara.git $mahara_dir_name
cd $mahara_dir_name
# Set core.autocrlf and core.safecrlf to false so git on Windows doesn't
# play with line endings on checkout/commit. That breaks havoc!!
git config core.autocrlf false
git config core.safecrlf false
git checkout -q master

echo "Creating the language packe repository..."
mkdir -p $LANG_PACK_GIT_DIR
cd $LANG_PACK_GIT_DIR
git init -q
git config core.autocrlf false
git config core.safecrlf false
git remote add origin git+ssh://git.mahara.org/git/lang/$LANG_PACK.utf8.git

for branch in $GIT_BRANCHES
do
	echo "Creating branch $branch in the language pack repository..."

	cd $LANG_PACK_GIT_DIR
	if [ "$branch" != "master" ]
	then
		git checkout -q -b $branch master
		git rm -q -r .
		# In addition to telling git to remove everything from the repository
		# we need to remove the empty directories git leaves behind (as git
		# only tracks files, not directories). Otherwise the rename
		# commands that we use below put things in the wrong subdirectories.
		find . -name '.' -o -name '..' -o -path './.git' -prune -o -exec rm -r {} ";" 2> /dev/null
	fi
	git config --add remote.origin.push $branch

	cd ${mahara_dir}
	git checkout -q origin/$branch
	cd htdocs
	# Exclude the files under ./lib, as we are not interested
	# and there are several named en.*
	for i in `find . -path ./lib -prune -o -name "en.*" -print`
	do
		mkdir -p $LANG_PACK_GIT_DIR/`dirname $i`
		cp -a $i $LANG_PACK_GIT_DIR/`dirname $i`
	done

	cd $LANG_PACK_GIT_DIR
	for i in `find . -type d -name "en.utf8" -print`
	do
		mv $i ${i%*/en.utf8}/${LANG_PACK}.utf8
	done

	for i in `find . -type f -name "en.*" -print`
	do
		mv $i ${i%*/en.js}/${LANG_PACK}.js
	done

	git add .
	git commit -q -m "Initial commit of ${LANG_NAME} lang pack ($branch branch)."
	
done

cd ${mahara_dir}
git checkout -q master

cd $LANG_PACK_GIT_DIR
git checkout -q master

echo "Done!"
