#!/usr/local/bin/perl
$FILE_OP = 0;
$DIR_OP = 0;

sub usage {

    print "Usage: dosFix [-f <file> | -d <dir>]\n";
    exit(0);
}

# Simple function to retrieve all of our arguments.
sub getArgs {

    if (@ARGV[0] eq "") {
	&usage;
    }

    local($arg) = "";
    while($arg = shift(@ARGV)) {
	
	if ($arg eq "-f") {
	    $FILE_OP = 1;
	    $FILE_NAME = shift (@ARGV);
	    if (!$FIX_FILE) {
		&usage;
	    }
	} elsif ($arg eq "-d") {
	    $DIR_OP = 1;
	    $DIR_NAME = shift (@ARGV);
	    if (!$DIR_NAME) {
		&usage;
	    }
	} else {
	    &usage;
	}
    }

}

sub fixFile {
    system("dos2unix $FILE_NAME $FILE_NAME");
}

sub fixDir {

    if (!(-d $DIR_NAME)) {
	print $DIR_NAME," is not a directory.";
	exit(1);
    }
    @FILE_LIST = `find $DIR_NAME -type f`;

    while ($_ = shift (@FILE_LIST)) {
	chop $_;
	print "Fixing $_\n";
	system("dos2unix $_ $_");
	sleep(1);
    }
}


&getArgs;
if ($FILE_OP) {
    &fixFile;
}

if ($DIR_OP) {
    &fixDir;
}
