#!/bin/sh
# Script to upgrade to a new Qt.
# Apply VERY carefully, step by step.
#
# David Faure <faure@kde.org>
#

SNAPSHOT=qt-x11-free-3.0.4rc1.tar.gz

if [ ! -f $SNAPSHOT ]; then echo $SNAPSHOT not found ; fi

TOPLEVEL=`tar tzf $SNAPSHOT | head -1 | sed -e 's,/$,,' `

echo Found TOPLEVEL=$TOPLEVEL

echo -n Cleaning qt-copy...
# Do a make cvs-clean first
if [ ! -d qt-copy ]; then echo You should have a qt-copy directory under here ; fi
cd qt-copy
common=""
if [ -f ../kdelibs/admin/Makefile.common ] ; then common=../kdelibs/admin/Makefile.common; fi
if [ -f ../../kdelibs/admin/Makefile.common ] ; then common=../../kdelibs/admin/Makefile.common; fi
if [ -z "$common" ]; then echo Makefile.common not found ; fi
make -f $common cvs-clean
cd ..
echo done

# List of new stuff
echo -n Creating to_add and to_remove...
tar tzf $SNAPSHOT | sort > qtlist     || exit 1
perl -pi -e "s/$TOPLEVEL/qt-copy/" qtlist

# Plus stuff we added ourselves
echo qt-copy/README.qt-copy >> qtlist
echo qt-copy/Makefile.cvs >> qtlist
echo qt-copy/upgrade_script.qt >> qtlist

# Prepare list of what's new (not includes, they're links)
cp -f /dev/null to_add
cat qtlist | grep -v include | while read a ; do
  if [ ! -e "$a" ]; then
     echo "$a" >> to_add
  fi
done

# And what we don't want to see in our cvs
perl -pi -e 'if (/^qt-copy\/src\/tools\/qmodules.h/) {$_ = "";}' to_add

# Remove old stuff
cp -f /dev/null to_remove
find qt-copy | grep -v CVS | grep -v .cvsignore | grep -v debian | grep -v rpm | grep -v objprelink | while read a ; do
  if ! grep -q -F $a qtlist; then
    echo $a >> to_remove
  fi
done
echo done

echo Press a key to continue.....
read key

if [ ! -d qt-copy ]; then echo You should have a qt-copy directory under here ; fi # Better very safe than very sorry...
cd qt-copy

#perl -e ' $/ = undef; $files = ""; if ( open( I, "../to_remove" ) ) { $files = <I>; $files =~ s/\s+/ /gs; $files =~ s/qt-copy\///g; close I; } if ( $files && open( C, "cvs remove -f $files |" ) ) { $res = <C>; } print $res;'
perl -pi -e "s/qt-copy\///" ../to_remove

cat ../to_remove | while read a; do
  echo Removing $a
  cvs rm -f $a
done

cd ..

# Untar
echo -n Untarring snapshot over qt-copy...
rm -rf $TOPLEVEL
mv qt-copy $TOPLEVEL
tar xzf $SNAPSHOT
mv $TOPLEVEL qt-copy
echo done

cd qt-copy

# add new stuff
#perl -e '$/ = undef;files = "";if ( open( I, "../to_add" ) ) {    $files = <I>;    $files =~ s/\s+/ /gs;    $files =~ s/qt-copy\///g;    close I;}if ( $files && open( C, "cvs add $files |" ) ) {    $res = <C>;}print $res;'
perl -pi -e "s/qt-copy\///" ../to_add
perl -pi -e "s/.\/qt-copy\///" ../to_add

cat ../to_add | while read a ; do
     echo Adding $a
     cvs add $a
done

echo -n Updating from CVS...
# Run an update - this is because tar x touches all files, even the ones
# than haven't changed
cvs up -A
echo done

# Now commit !
# Better do it by hand (to choose a log, possibly silent)
# and subdir by subdir (otherwise it takes ages)
echo "Don't forget to commit, with CVS_SILENT and subdir by subdir (otherwise it takes ages)"


