#!/usr/bin/perl
############################################################################
# $Id: //depot/qt/2.3/bin/syncqt#6 $
#
# Synchronizes Qt header files - internal Trolltech tool.
#   - Creates symlinks on Unix.
#   - Copies files on Windows.
#
# Copyright (C) 1997-1998 by Trolltech AS.  All rights reserved.
#
############################################################################

die "syncqt: QTDIR not defined" if ! $ENV{"QTDIR"};
$fast=0;

$basedir = $ENV{"QTDIR"};
$includedir = $basedir . "/include";

$showonly = undef;

while ( $#ARGV >= 0 ) {
    if ( $ARGV[0] eq "-fast" ) {
	$fast=1;
    } elsif ( $ARGV[0] eq "-inc" ) {
	shift;
	$includedir = $ARGV[0];
	if ( $includedir =~ /^\// ) {
	    $includedir = `pwd` ."/". $includedir;
	}
    } elsif ( $ARGV[0] eq "-show" ) {
	$showonly++;
    }
    shift;
}

undef $/;

mkdir $includedir, 0777;

opendir SRC, "$basedir/src";
@dirs = map { -d "$basedir/src/$_" ? "src/$_" : () } readdir(SRC);
closedir SRC;
@dirs = ( @dirs, "extensions/xt/src", "extensions/nsplugin/src" );
@dirs = ( @dirs, "src/3rdparty/tools", "src/3rdparty/kernel" );

foreach $p ( @dirs ) {
    if ( -d "$basedir/$p" ) {
	chdir "$basedir/$p";
	@ff = find_files( ".", "^[-a-z0-9]*(?:_[^p].*)?\\.h\$" , 0 );
	foreach ( @ff ) { $_ = "$p/$_"; }
	push @files, @ff;
    }
}

@files = ( @files, "src/3rdparty/kernel/qgifimageformat_p.h" );

if ( $showonly ) {
    foreach ( @files ) {
	print "$_\n";
    }
    exit( 0 );
}

if ( check_unix() ) {
    chdir $includedir;
    foreach $f ( @files ) {
	$h = $f;
	$h =~ s-.*/--g;
	if ( ! -l $h ) {
	    symlink("../" . $f, $h);
	    print "symlink created for $f\n";
	}
    }
} else {
    foreach $f ( @files ) {
	$h = $f;
	$h =~ s-.*/--g;
	sync_files("$basedir/$f", "$includedir/$h", $fast);
    }
}

exit 0;


#
# sync_files(file,ifile)
#
# If ifile does not exist, file is copied to ifile, otherwise
# the newest file is copied over the older file.
#

sub sync_files()
{
    my ($file,$ifile,$fast,$copy,$knowdiff,$filecontents,$ifilecontents) = @_;

    if ( $fast ) {
	# Uni-directional synchronization
	if ( (stat($ifile))[9] < (stat($file))[9] ) {
	    open( I, "< " . $file ) || die "Could not open $file for reading";
	    $filecontents = <I>;
	    close I;
	    $copy = -1;
	    $knowdiff = 1;
	}
    } else {
	# Bi-directional synchronization
	open( I, "< " . $file ) || die "Could not open $file for reading";
	$filecontents = <I>;
	close I;
	if ( open(I, "< " . $ifile) ) {
	    $ifilecontents = <I>;
	    close I;
	    $copy = (stat($ifile))[9] <=> (stat($file))[9];
	    $knowdiff = 0,
	} else {
	    $copy = -1;
	    $knowdiff = 1;
	}
    }

    if ( $knowdiff || ($filecontents ne $ifilecontents) ) {
	if ( $copy > 0 ) {
	    open(O, "> " . $file) || die "Could not open $file for writing";
	    print O $ifilecontents;
	    close O;
	    print "$file written\n";
	} elsif ( $copy < 0 ) {
	    open(O, "> " . $ifile) || die "Could not open $ifile for writing";
	    print O $filecontents;
	    close O;
	    print "$ifile written\n";
	}
    }
}


#
# Finds files.
#
# Examples:
#   find_files("/usr","\.cpp$",1)   - finds .cpp files in /usr and below
#   find_files("/tmp","^#",0)	    - finds #* files in /tmp
#

sub find_files {
    my($dir,$match,$descend) = @_;
    my($file,$p,@files);
    local(*D);
    $dir =~ s=\\=/=g;
    ($dir eq "") && ($dir = ".");
    if ( opendir(D,$dir) ) {
	if ( $dir eq "." ) {
	    $dir = "";
	} else {
	    ($dir =~ /\/$/) || ($dir .= "/");
	}
	foreach $file ( readdir(D) ) {
	    next if ( $file  =~ /^\.\.?$/ );
	    $p = $dir . $file;
	    ($file =~ /$match/) && (push @files, $p);
	    if ( $descend && -d $p && ! -l $p ) {
		push @files, &find_files($p,$match,$descend);
	    }
	}
	closedir(D);
    }
    return @files;
}


#
# check_unix()
#
# Returns 1 if this is a Unix, 0 otherwise.
#

sub check_unix {
    my($r);
    $r = 0;
    if ( -f "/bin/uname" ) {
	$r = 1;
	(-f "\\bin\\uname") && ($r = 0);
    } elsif ( -f "/usr/bin/uname" ) {
        $r = 1;
	(-f "\\usr\\bin\\uname") && ($r = 0);
    }
    return $r;
}
