#!/usr/bin/perl

#
# cvslist.pl v1.0
# Copyright (C) 2001 by Darko K. <darkok@hermes.si>
#
# Distributed under the GNU GPL
#
# For every file in current dir and its subdirs print:
# filename, revision number (and tag if apropriate), status
#

use Cwd;

if (!open(cvs,"cvs -Q status -Rv|")) {
    die("can't starts cvs\n");
}

$dirprefix = "";
$curdir = getcwd();
@dirscwd = split('/', $curdir);

printf("%-45s %-30s %s\n", "Filename", "Revision", "St");
printf("===============================================================================\n");

while (<cvs>) {
    chop;
    if (/^\s*Working revision:\s*([0-9.]+)\s*(.*)/) {
        $revision = $1;
    }
    if (/^\s*Repository revision:\s*([0-9.]+)\s*(.*)\/.*,v/) {
        $filepath = $2;
        $printit = 1;
        # determine common element in path by stripping on the right side
        @dirsrep = split('/', $filepath);
        $repdcnt = 1;
        $curdcnt = 1;
        while (1) {
            if ($curdcnt gt $#dirscwd) {
                last;
            }
            if ($repdcnt gt $#dirsrep) {
                $repdcnt = 1;
                $curdcnt++;
            }
            if ($dirsrep[$repdcnt] eq $dirscwd[$curdcnt]) {
		# for sourceforge
                if ($dirsrep[$repdcnt] eq $dirsrep[$repdcnt+1]) {
		    $repdcnt++;
		}
                while ($dirsrep[$repdcnt] eq $dirscwd[$curdcnt]) {
                    if (($curdcnt gt $#dirscwd) || ($repdcnt gt $#dirsrep)) {
                        last;
                    }
                    $repdcnt++;
                    $curdcnt++;
		}
                if ($dirsrep[$repdcnt] ne $dirscwd[$curdcnt]) {
                    $repdcnt--;
                    $curdcnt--;
                }
                if ($curdcnt gt $#dirscwd) {
                    $curdcnt--;
		}
                if ($repdcnt gt $#dirsrep) {
                    $repdcnt--;
                }
                last;
            }
            $repdcnt++;
        }
        if ($repdcnt le $#dirsrep) {
	    $newfilepath = "";
            for ($i = $repdcnt+1; $i <= $#dirsrep; $i++) {
	        if ($newfilepath ne "") {
	            $newfilepath = sprintf("%s/", $newfilepath);
		}
	        $newfilepath = sprintf("%s%s", $newfilepath, $dirsrep[$i]);
	    }
	    if ($newfilepath ne "") {
                $file = sprintf("%s/%s", $newfilepath, $file);
            }
        }
    }
    if (/^\s+((\w|[._-])+)\s*\(revision: $revision\)/) {
        if ($tag eq "") {
            $tag = $1;
            $revision = sprintf("%s (%s)", $revision, $tag);
        }
    }
    if (/^File:(\s+)((\w|[.-])+)(\s*)Status:\s*([A-Z])/) {
        if ($printit > 0) {
            printf("%-45s %-30s %s\n", $file, $revision, $status);
        }
        $file = $2;
        $status = $5;
        $tag = "";
        $printit = 0;
        # Locally Modified
        if ($status eq "L") {
            $status = "M";
        }
    }
}

if ($printit > 0) {
    printf("%-45s %-30s %s\n", $file, $revision, $status);
}

close(cvs);
