#!/bin/sh
#
# This script will update all the modules listed below so that
# common points to master in the common module.
#
# If you have many of the GStreamer modules checked out in a particular
# directory, it's best to run this script from that directory.  For
# example, I check everything out in ~/gst, so this file is
# ~/gst/common/update-common.  To do an update, I do
# 'cd ~/gst ; ./common/update-common'.  This will automatically use
# the refs in your existing checkout when cloning the temporary
# checkout.  Alternatively, you can use the reference variable below.
#

# Set this variable to point to any directory containing existing
# git # checkouts, and git will pull objects from there, decreasing
# network usage.
reference=~/gst

set -e
set -x

modules="gstreamer gst-plugins-base gst-plugins-good gst-plugins-bad \
	gst-plugins-ugly gst-ffmpeg gst-python \
	gnonlin gst-plugins-gl"

topdir=`pwd`
dir=`mktemp -d $topdir/common-update-XXXXXX`

for module in $modules
do
  cd $dir
  if test -e $reference/$module/.git ; then
    git clone --reference $reference/$module/.git --shared ssh://git.freedesktop.org/git/gstreamer/$module
  elif test -e $topdir/$module/.git ; then
    git clone --reference $topdir/$module/.git --shared ssh://git.freedesktop.org/git/gstreamer/$module
  else
    git clone ssh://git.freedesktop.org/git/gstreamer/$module
  fi
  cd $dir/$module
  git submodule init
  git submodule update
  cd $dir/$module/common
  ref_from=`git log --pretty=format:%h -n 1 HEAD`
  git checkout master
  git pull origin
  ref_to=`git log --pretty=format:%h -n 1 HEAD`
  echo updating common from $ref_from to $ref_to
  if [ "$ref_from" != "$ref_to" ] ; then
    cd $dir/$module
    git add common
    git commit -m "Automatic update of common submodule

From $ref_from to $ref_to"
  fi
  cd $dir
done

for module in $modules
do
  cd $dir/$module
  git push origin
done

rm -rf $dir
