#!/usr/local/bin/tclsh

proc find_missing {first_path second_path current_dir current_dir2 mf}  {
	global ls_args
	upvar $mf missing_files

	set save_dir [pwd]
	cd $first_path/$current_dir
	set files {}
	foreach i $ls_args {
		catch {set files [concat $files [glob $i]]} dd
	}
	if { [llength $files]==0 }  {
		cd $save_dir
		return
	}
	cd $second_path/$current_dir2
	foreach i $files {
		if [catch {eval exec ls $i} res2] {
			lappend missing_files $current_dir/$i
		} 
	}
	cd $save_dir
}

proc find_differing { first_path second_path current_dir current_dir2 df}  {
	global ls_args dfile1 dfile2
	upvar $df differing_files

	set save_dir [pwd]
	cd $first_path/$current_dir
	set files {}
	foreach i $ls_args {
		catch {set files [concat $files [glob $i]]} dd
	}
	if { [llength $files]==0}  {
		cd $save_dir
		return
	}
	cd $second_path/$current_dir2
	foreach i $files {
		if {[string compare [file type $first_path/$current_dir/$i] \
				"file"] != 0} {
			return
		}
		if ![catch {eval exec ls $i} res2] {
		   set dfile1 $first_path/$current_dir/$i  
		   set dfile2 $second_path/$current_dir2/$i
		   if [catch {exec diff $dfile1 $dfile2 >/dev/null} \
						res3] {
				lappend differing_files $i
			}
		}
	}
	cd $save_dir
}

proc print_missing {} {
	global missing_files1 missing_files2 path1 path2

	set missing_files1 [lrange $missing_files1 1 [llength $missing_files1] ]
	set missing_files2 [lrange $missing_files2 1 [llength $missing_files2] ]
	if { [llength $missing_files1] > 0} {
		puts "       -------------------------- "
		puts "       Files missing from $path2: "
		puts "       -------------------------- "
		foreach i $missing_files1 {
			puts "       $i"
		}
	}
	if { [llength $missing_files2] > 0} {
		puts "       -------------------------- "
		puts "       Files missing from $path1: "
		puts "       -------------------------- "
		foreach i $missing_files2 {
			puts "      $i"
		}
	}
}

proc print_differing differing_files {
	set len [llength $differing_files]
	set differing_files [lrange $differing_files 1 \
				     [llength $differing_files] ]
	if { [llength $differing_files] > 0} {
		puts "       -------------------------- "
		puts "       Files Differing "
		puts "       -------------------------- "
		foreach i $differing_files {
			puts "       $i"
		}
	}
}

proc process_dir { current_dir  current_dir2} {
	global path1 path2 missing_files1 missing_files2

	puts "-----------------------"
	puts "searching: $current_dir"
	puts "-----------------------"
	set missing_files1 {a}
	set missing_files2 {a}
	set differing_files {a}
	find_missing $path1 $path2 $current_dir $current_dir2 missing_files1 
	find_missing $path2 $path1 $current_dir2 $current_dir missing_files2 
	find_differing $path2 $path1 $current_dir2 $current_dir differing_files
	print_missing 
	print_differing $differing_files
}

proc search_dir_driver { subdir subdir2} {
	global path1 path2
	#puts "in sdd: $path1/$subdir" 
	#puts "      : $path2/$subdir2" 
	if {![file isdirectory $path1/$subdir] || \
	    ![file isdirectory $path2/$subdir2]} {
		return
	}
	if {[string compare [file type $path1/$subdir] link] == 0}  {
		puts "warning: not searching link: $path1/$subdir";  return
	}
	process_dir $subdir $subdir2
	set save_dir $path1/$subdir
	cd $path1/$subdir
	if ![catch {glob */} res] {
		foreach  i $res  {
			set i [split $i /]
			set i [lrange $i 0 0]
			search_dir_driver $subdir/$i $subdir2/$i
		}
	}
	cd $save_dir
}

proc get_dirname { d } {
	upvar $d dir
	if ![catch {glob $dir} res] {
		set dir [glob $dir]
	} else {
		puts "file or directory $dir does not exist"
		return
	}
	set dir [split $dir /]
	if { [lsearch $dir {}] != 0 } {
		set dir [concat [split [exec pwd] /] $dir] 
	}
	if { [lsearch $dir {}] == [expr [llength $dir] - 1] } {
		set dir [lrange $dir 0 [expr [llength $dir] - 2]]
	}
	set dir [join $dir /]
}

proc search_dir {dir {dir2 1}} {
	global path1 path2 missing_files1 missing_files2
	get_dirname dir
	if {$dir2 == 1} {
		set dir2 $dir
	} else {
		get_dirname dir2
	}
	set path1 [file dirname $dir]
	set file1 [file tail $dir]
	set path2 [file dirname $dir2]
	set file2 [file tail $dir2]
	set missing_files1 {a}
	set missing_files2 {a}

	set save_dir [pwd]
	search_dir_driver $file1 $file2
	cd $save_dir
}


if {$argc < 3} {
  puts "usage:  lcompare <dir1> <dir2> \"<ls arguments>\""
  puts "ex: lcompare  ~/link/lanl_link/link ~/link/sb_link/link.03Nov94 \"*.cpp *.h Mak*\""
  return
}

#set d1 /lunar/u1/berryj/link/lanl_link/link
#set d2 /lunar/u1/berryj/link/sb_link/link.03Nov94
#set ls_args {*.cpp *.h Mak*}

set d1 [lindex $argv 0]
set d2 [lindex $argv 1]
set ls_args [split [lindex $argv 2]]


search_dir $d1 $d2
