#!/bin/bash

die() {
    echo $*
    exit 1
}

default_tag="qtwebkit-4.6-snapshot-20091003"

if [ $# -eq 0 ]; then
    tag="$default_tag"
elif [ $# -eq 1 ]; then
    tag=$1
else
    die "usage: $0 [commit (defaults to $default_tag)]"
fi

repository=`git config qtwebkit.url`
if [ -z "$repository" ]; then
    die "error: cannot locate webkit git repository. please run git config --global qtwebkit.url /path-or-url/to/webkit/repo"
fi

excluded_directories="LayoutTests JavaScriptGlue WebKitLibraries WebKitSite WebKitTools WebCore/platform/cf WebCore/platform/gtk WebCore/platform/chromium"
excluded_directories="$excluded_directories PageLoadTests"
excluded_directories="$excluded_directories BugsSite"
excluded_directories="$excluded_directories PlanetWebKit"
excluded_directories="$excluded_directories SunSpider"
excluded_directories="$excluded_directories WebKitExamplePlugins"

excluded_directories="$excluded_directories autotools"
excluded_directories="$excluded_directories JavaScriptCore/Makefile"
excluded_directories="$excluded_directories Makefile"
excluded_directories="$excluded_directories Makefile.shared"
excluded_directories="$excluded_directories GNUmakefile.am"
excluded_directories="$excluded_directories WebCore/Makefile"
excluded_directories="$excluded_directories WebKit/Makefile"

excluded_directories="$excluded_directories JavaScriptCore/GNUmakefile.am"
excluded_directories="$excluded_directories JavaScriptCore/Configurations"
excluded_directories="$excluded_directories JavaScriptCore/JavaScriptCore.exp"
excluded_directories="$excluded_directories JavaScriptCore/JavaScriptCore.xcodeproj"
excluded_directories="$excluded_directories JavaScriptCore/tests"
excluded_directories="$excluded_directories JavaScriptCore/API/tests"
excluded_directories="$excluded_directories JavaScriptCore/JavaScriptCore.vcproj"
excluded_directories="$excluded_directories JavaScriptCore/JavaScriptCore.gyp"
excluded_directories="$excluded_directories JavaScriptCore/wtf/wx"
excluded_directories="$excluded_directories JavaScriptCore/wtf/gtk"
excluded_directories="$excluded_directories JavaScriptCore/wtf/mac"
excluded_directories="$excluded_directories JavaScriptCore/wtf/win"
excluded_directories="$excluded_directories JavaScriptCore/wtf/chromium"
excluded_directories="$excluded_directories JavaScriptCore/wtf/haiku"

excluded_directories="$excluded_directories WebCore/WebCore.vcproj"
excluded_directories="$excluded_directories WebCore/WebCore.gyp"
excluded_directories="$excluded_directories WebCore/DerivedSources.make"
excluded_directories="$excluded_directories WebCore/GNUmakefile.am"
excluded_directories="$excluded_directories WebCore/WebCore.base.exp"
excluded_directories="$excluded_directories WebCore/WebCore.PluginHostProcess.exp"
excluded_directories="$excluded_directories WebCore/WebCore.OrientationEvents.exp"
excluded_directories="$excluded_directories WebCore/WebCore.xcodeproj"
excluded_directories="$excluded_directories WebCore/Configurations"

excluded_directories="$excluded_directories WebCore/bridge/objc"
excluded_directories="$excluded_directories WebCore/bridge/testbindings.pro"
excluded_directories="$excluded_directories WebCore/bindings/objc"
excluded_directories="$excluded_directories WebCore/bindings/v8"

excluded_directories="$excluded_directories JavaScriptCore/icu"

excluded_directories="$excluded_directories WebCore/icu"
# remove CollatorICU from SOURCES in JavaScriptCore.pri first
#excluded_directories="$excluded_directories JavaScriptCore/wtf/unicode/icu"

excluded_directories="$excluded_directories WebCore/loader/mac"
excluded_directories="$excluded_directories WebCore/loader/win"
excluded_directories="$excluded_directories WebCore/loader/icon/wince"

excluded_directories="$excluded_directories WebCore/page/gtk"
excluded_directories="$excluded_directories WebCore/page/mac"
excluded_directories="$excluded_directories WebCore/page/wx"
excluded_directories="$excluded_directories WebCore/page/chromium"
excluded_directories="$excluded_directories WebCore/page/haiku"
excluded_directories="$excluded_directories WebCore/page/wince"

excluded_directories="$excluded_directories WebCore/history/mac"

excluded_directories="$excluded_directories WebCore/editing/mac"
excluded_directories="$excluded_directories WebCore/editing/wx"
excluded_directories="$excluded_directories WebCore/editing/haiku"

excluded_directories="$excluded_directories WebCore/platform/haiku"

excluded_directories="$excluded_directories WebCore/platform/text/wx"
excluded_directories="$excluded_directories WebCore/platform/text/gtk"
excluded_directories="$excluded_directories WebCore/platform/text/chromium"
excluded_directories="$excluded_directories WebCore/platform/text/haiku"

excluded_directories="$excluded_directories WebCore/platform/sql/chromium"

excluded_directories="$excluded_directories WebCore/manual-tests"

excluded_directories="$excluded_directories WebCore/platform/network/cf"
excluded_directories="$excluded_directories WebCore/platform/network/curl"
excluded_directories="$excluded_directories WebCore/platform/network/mac"
excluded_directories="$excluded_directories WebCore/platform/network/win"
excluded_directories="$excluded_directories WebCore/platform/network/soup"
excluded_directories="$excluded_directories WebCore/platform/network/chromium"

excluded_directories="$excluded_directories WebCore/platform/graphics/cg"
excluded_directories="$excluded_directories WebCore/platform/graphics/cairo"
excluded_directories="$excluded_directories WebCore/platform/graphics/gtk"
excluded_directories="$excluded_directories WebCore/platform/graphics/wx"
excluded_directories="$excluded_directories WebCore/platform/graphics/mac"
excluded_directories="$excluded_directories WebCore/platform/graphics/win"
excluded_directories="$excluded_directories WebCore/platform/graphics/skia"
excluded_directories="$excluded_directories WebCore/platform/graphics/chromium"
excluded_directories="$excluded_directories WebCore/platform/graphics/wince"
excluded_directories="$excluded_directories WebCore/platform/graphics/haiku"

excluded_directories="$excluded_directories WebCore/platform/image-decoders/bmp"
excluded_directories="$excluded_directories WebCore/platform/image-decoders/gif"
excluded_directories="$excluded_directories WebCore/platform/image-decoders/zlib"
excluded_directories="$excluded_directories WebCore/platform/image-decoders/png"
excluded_directories="$excluded_directories WebCore/platform/image-decoders/ico"
excluded_directories="$excluded_directories WebCore/platform/image-decoders/jpeg"
excluded_directories="$excluded_directories WebCore/platform/image-decoders/xbm"
excluded_directories="$excluded_directories WebCore/platform/image-decoders/skia"
excluded_directories="$excluded_directories WebCore/platform/image-decoders/haiku"

excluded_directories="$excluded_directories WebCore/platform/image-encoders/skia"

excluded_directories="$excluded_directories WebCore/plugins/gtk"
excluded_directories="$excluded_directories WebCore/plugins/chromium"

excluded_directories="$excluded_directories WebCore/accessibility/chromium"
excluded_directories="$excluded_directories WebCore/accessibility/gtk"
excluded_directories="$excluded_directories WebCore/accessibility/mac"
excluded_directories="$excluded_directories WebCore/accessibility/win"
excluded_directories="$excluded_directories WebCore/accessibility/wx"

excluded_directories="$excluded_directories WebCore/storage/wince"

excluded_directories="$excluded_directories WebCore/platform/wx"
excluded_directories="$excluded_directories WebCore/platform/wince"

excluded_directories="$excluded_directories WebKit/qt/Plugins"

excluded_directories="$excluded_directories WebKit/gtk"
excluded_directories="$excluded_directories WebKit/win"
excluded_directories="$excluded_directories WebKit/wx"
excluded_directories="$excluded_directories WebKit/cf"
excluded_directories="$excluded_directories WebKit/haiku"
excluded_directories="$excluded_directories WebKit/chromium"

excluded_directories="$excluded_directories WebKit/English.lproj WebKit/WebKit.xcodeproj"
excluded_directories="$excluded_directories WebCore/English.lproj"

exclude_with_exceptions_list="WebCore/platform/win/:WebCore/platform/win/SystemTimeWin.cpp\\|WebCore/platform/win/BitmapInfo.*"

excluded_directories="$excluded_directories WebKit/mac/Carbon"
excluded_directories="$excluded_directories WebKit/mac/ChangeLog"
excluded_directories="$excluded_directories WebKit/mac/ChangeLog-2002-12-03"
excluded_directories="$excluded_directories WebKit/mac/ChangeLog-2006-02-09"
excluded_directories="$excluded_directories WebKit/mac/ChangeLog-2007-10-14"
excluded_directories="$excluded_directories WebKit/mac/DefaultDelegates"
excluded_directories="$excluded_directories WebKit/mac/DOM"
excluded_directories="$excluded_directories WebKit/mac/ForwardingHeaders"
excluded_directories="$excluded_directories WebKit/mac/History"
excluded_directories="$excluded_directories WebKit/mac/icu"
excluded_directories="$excluded_directories WebKit/mac/Info.plist"
excluded_directories="$excluded_directories WebKit/mac/MigrateHeaders.make"
excluded_directories="$excluded_directories WebKit/mac/Misc"
excluded_directories="$excluded_directories WebKit/mac/Panels"
excluded_directories="$excluded_directories WebKit/mac/Plugins"
excluded_directories="$excluded_directories WebKit/mac/PublicHeaderChangesFromTiger.txt"
excluded_directories="$excluded_directories WebKit/mac/Resources"
excluded_directories="$excluded_directories WebKit/mac/Storage"
excluded_directories="$excluded_directories WebKit/mac/WebCoreSupport"
excluded_directories="$excluded_directories WebKit/mac/WebInspector"
excluded_directories="$excluded_directories WebKit/mac/WebKit.exp"
excluded_directories="$excluded_directories WebKit/mac/WebKit.order"
excluded_directories="$excluded_directories WebKit/mac/WebKitPrefix.h"
excluded_directories="$excluded_directories WebKit/mac/WebView"

exclude_with_exceptions_list="$exclude_with_exceptions_list WebKit/mac/Configurations/:WebKit/mac/Configurations/Version.xcconfig"

files_to_remove=""
files_to_remove="$files_to_remove WebKit/qt/Api/qwebnetworkinterface.cpp"
files_to_remove="$files_to_remove WebKit/qt/Api/qwebnetworkinterface.h"
files_to_remove="$files_to_remove WebKit/qt/Api/qwebnetworkinterface_p.h"
files_to_remove="$files_to_remove WebKit/qt/Api/qcookiejar.h"
files_to_remove="$files_to_remove WebKit/qt/Api/qcookiejar.cpp"

files_to_remove="$files_to_remove WebCore/rendering/RenderThemeMac.mm"
files_to_remove="$files_to_remove autogen.sh"
files_to_remove="$files_to_remove configure.ac"

files_to_remove="$files_to_remove WebKit.pro"

files_to_remove="$files_to_remove WebKit/qt/QtLauncher/QtLauncher.pro"
files_to_remove="$files_to_remove WebKit/qt/QtLauncher/main.cpp"

files_to_remove="$files_to_remove WebKit/qt/QGVLauncher/QGVLauncher.pro"
files_to_remove="$files_to_remove WebKit/qt/QGVLauncher/main.cpp"

files_to_remove="$files_to_remove JavaScriptCore/AllInOneFile.cpp"
files_to_remove="$files_to_remove JavaScriptCore/JavaScriptCoreSources.bkl"
files_to_remove="$files_to_remove JavaScriptCore/jscore.bkl"
files_to_remove="$files_to_remove JavaScriptCore/jsc.pro"

files_to_remove="$files_to_remove WebCore/wscript"
files_to_remove="$files_to_remove WebCore/WebCore.ContextMenus.exp"
files_to_remove="$files_to_remove WebCore/WebCore.DragSupport.exp"
files_to_remove="$files_to_remove WebCore/WebCore.Inspector.exp"
files_to_remove="$files_to_remove WebCore/WebCoreSources.bkl"
files_to_remove="$files_to_remove WebCore/webcore-base.bkl"
files_to_remove="$files_to_remove WebCore/webcore-wx.bkl"

require_clean_work_tree() {
    # test if working tree is dirty
    git rev-parse --verify HEAD > /dev/null &&
    git update-index --refresh &&
    git diff-files --quiet &&
    git diff-index --cached --quiet HEAD ||
    die "Working tree is dirty"
}

which qmake >/dev/null 2>/dev/null
if [ "$?" != 0 ]; then
    die "abort: Could not locate qmake in your PATH"
fi

test -z "$(git rev-parse --show-cdup)" || {
       exit=$?
       echo >&2 "You need to run this command from the toplevel of the working tree."
       exit $exit
}

echo "checking working tree"
require_clean_work_tree

revCount=`git ls-remote $repository | grep $tag | awk '{print $1}' | wc -l`
if [ "$revCount" != 1 ]; then
    die "Cannot parse $tag into a revision. It seems ambiguous".
fi

rev=`git ls-remote $repository | grep -E "^.+$tag$" | awk '{print $1}'`

tarball=`mktemp /tmp/webkit-snapshot.tar.XXXXXX` || exit 1
echo "creating $tarball"

echo "archiving webkit from $repository $tag ( $rev )"

git archive --remote=$repository $rev > $tarball || exit 1

echo "removing unwanted files and directories"
for dir in $excluded_directories; do
    echo "    removing $dir"
    tar --delete --file=$tarball $dir
done

for item in $exclude_with_exceptions_list; do
    dir=`echo $item | awk -F : '{print $1}'`
    include=`echo $item | awk -F : '{print $2}'`
    echo "    removing $dir except $include"
    files=`tar --list --file=$tarball $dir | grep -v -E "^$dir\$" | grep -v $include`
    tar --delete --file=$tarball $files
done

for file in $files_to_remove; do
    echo "    removing $file"
    tar --delete --file=$tarball $file
done

echo "done!"

srcdir=src/3rdparty/webkit
absSrcDir=$PWD/$srcdir
localDiff=
lastImportRevison=

echo "replacing $srcdir"
if [  -d $srcdir ]; then
    lastImportRevison=`tail -n 1 src/3rdparty/webkit/VERSION`
    git ls-files $srcdir | xargs rm
    git ls-files -z src/3rdparty/webkit | git update-index --force-remove -z --stdin
    lastImport=`git rev-list --max-count=1 HEAD -- src/3rdparty/webkit/VERSION`
    changes=`git rev-list --no-merges --reverse $lastImport.. -- src/3rdparty/webkit`
    localDiff=/tmp/webkit_patch
    echo -n>$localDiff
    for change in $changes; do
        echo "Saving commit $change"
        git show -p --stat "--pretty=format:%nFrom %H Mon Sep 17 00:00:00 2001%nFrom: %an <%ae>%nDate: %ad%nSubject: Re-apply change $change by %an%n%n%s%n%b%n" $change -- src/3rdparty/webkit >> $localDiff
        echo "-- " >> $localDiff
        echo "1.2.3" >> $localDiff
        echo >> $localDiff
    done
    if [ -s $localDiff ]; then
        echo "Saved locally applied patches to $localDiff"
    else
        localDiff=""
    fi
else
    mkdir -p $srcdir
fi

(cd $srcdir && tar xf $tarball)
git add $srcdir

echo "generating extra sources"
(
  for proj in JavaScriptCore WebCore; do
      cd $absSrcDir/$proj &&
      rm -rf tmp &&
      mkdir tmp &&
      cd tmp &&
      mkdir -p ../generated &&
      qmake -o Makefile CONFIG-=QTDIR_build QT_CONFIG+=phonon GENERATED_SOURCES_DIR=`pwd`/../generated  OUTPUT_DIR=`pwd` ../$proj.pro &&
      make generated_files &&
      perl -pi -e "s,$absSrcDir/,,g" ../generated/*.cpp ../generated/*.h &&
      git add ../generated &&
      cd .. &&
      rm -rf tmp &&
      cd ..
  done
)
rm -rf $srcdir/WebKitBuild

cat >$srcdir/VERSION <<EOT
This is a snapshot of the Qt port of WebKit from

        git://gitorious.org/qtwebkit/qtwebkit.git

The commit imported was from the

        $tag branch/tag

and has the sha1 checksum

        $rev
EOT
git add $srcdir/VERSION

git diff-files --name-only -z | git update-index --remove -z --stdin

echo "removing $tarball"
rm -f $tarball

cat >commitlog.txt <<EOT
Updated WebKit from $repository to $tag ( $rev )
EOT

if [ -d "$repository/.git" -a -n "$lastImportRevison" ]; then
    echo >>commitlog.txt
    echo "Changes in WebKit/qt since the last update:" >>commitlog.txt
    echo >>commitlog.txt
    git --git-dir=$repository/.git diff $lastImportRevison $rev -- WebKit/qt/ChangeLog | sed -n -e "s,^\+\(.*\),\1,p" >>commitlog.txt
fi

echo "Changes:"
echo
git --no-pager diff --name-status --cached $srcdir

echo
echo "Wrote commitlog.txt. Use with"
echo
echo "    git commit -e -F commitlog.txt"
echo
echo "to commit your changes"

if [ -n "$localDiff" ]; then
    echo
    echo "The changes that were locally stored in Perforce are now stored as a git patch in $localDiff"
    echo "You may want to appy them with"
    echo
    echo "    git am -3 $localDiff"
    echo
fi
