#!/usr/local/bin/perl

# find the longest common directory prefix of the 2 input paths
# input is:
# $_[0] = path 1
# $_[1] = path 2
#
# output is:
# relative path
sub CTCommonPrefix {
   local( @path1 ) ;
   local( @path2 ) ;
   local( $count ) = 0 ;
   local( $thresh ) ;
   local( $tmp ) = $_[0] ;
   $tmp =~ s/^\/// ;
   @path1 = split ( /\//, $tmp ) ;
   $tmp = $_[1] ;
   $tmp =~ s/^\/// ;
   @path2 = split ( /\//, $tmp ) ;
   if ( $path1[0] eq "view" ) {
      $thresh = 1 ;
   } else {
      $thresh = 0 ;
   }
   while (( $path1[0] eq $path2[0] ) &&
	  ( $#path1 >= 0 ) &&
	  ( $#path2 > 0 )) {
      shift @path1;
      shift @path2;
      $count++ ;
   }
   local( $ret ) ;
   if ( $count > $thresh ) {
      while ( $#path1 >= 0 ) {
	 $ret = $ret . "../" ;
	 shift @path1 ;
      }
      while ( $#path2 >= 0 ) {
	 $ret = $ret . $path2[0] . "/" ;
	 shift @path2 ;
      }
      $ret =~ s/\/$// ;
   } else {
      $ret = $_[1] ;
   }
   $ret ;
}

# $pwd = $ENV{"PWD"} ;
# sometimes the PWD environment variable lies, and getcwd by itself doesn't
# report the view extention.
use Cwd ;
$pwd = getcwd() ;
if ( $pwd =~ /^\/vobs/ ) {
   open( VFILE, "/usr/atria/bin/cleartool pwv -short |" ) ;
   $view = <VFILE> ;
   close( VFILE ) ;
   $view =~ s/\n$// ;
   $pwd = "/view/" . $view . $pwd ;
}
$output = "" ;

foreach $item ( @ARGV ) {
   $common = &CTCommonPrefix( $pwd, $item ) ;
   if ( length( $common ) > 0 ) {
      if ( $output ne "" ) {
	 $output = $output . " " ;
      }
      $output = $output . $common ;
   }
}

print $output . "\n" ;
