#!/pro/local/bin/bash

export MODS
MODS="i00f000 i99f000"
MODS="$MODS include fnc"
MODS="$MODS l00m000 l01f000 l02f000 l03f000 l06f000 l90z000"
MODS="$MODS rpt"

export MSG="Merge all 4GL modules into one repo"

echo "Re-initialize GIT repo"
rm -rf .git commit-message
git init

echo "Recovering original module repo's"
tar xzf lep-git.tgz

for mod in $MODS ; do
    echo "Fetching for $mod"
    git fetch $mod refs/heads/*:refs/remotes/$mod/*
    done

echo "Removing module repo's"
for mod in $MODS ; do
    rm -rf $mod/.git
    done

echo "Adding modules"
git add $MODS >/dev/null

echo "Commit"
git commit -m "$MSG" >/dev/null

echo "Merge"
export MODMASTER
for mod in $MODS ; do
    MODMASTER="$MODMASTER $mod/master"
    done

git merge --no-commit -s ours $MODMASTER

export PMODMASTER
for mod in $MODS ; do
    PMODMASTER="$PMODMASTER -p $mod/master"
    done

echo "Commit"
echo $MSG >commit-message
commit=$(git commit-tree HEAD: $PMODMASTER < commit-message)

echo =========
echo $commit
echo =========

echo "Update"
git update-ref HEAD $commit
