#!/usr/local/bin/perl -w

use strict;
use Getopt::Std;
use DeployUtils::Logger;
use Clearcase::LsCheckoutCmd;
use Clearcase::undoCheckoutCmd;

use vars qw ( $opt_h $opt_d $opt_p $opt_k $opt_r );

#==============================================================================
#   Usage
#==============================================================================
sub Usage
{
    my $msg = shift;

    my $useStr = 
"Usage:
$0 [-h] [-d N] [-i] [-n|-c \"comment\"] [-p]
where:
    -h    : This help page
    -d N  : Sets Debugging level to N
    -r    : Remove current file after undo checkout (default)
    -k    : Keep current file as file.keep
    -p    : Executes a Press Enter to Continue on termination of script

This script does a recursive undo checkout on all checked out files find below pwd.

It will run a lscheckout command to get a list of files that are checked out.
It then runs the uncheckout command on these files to checkout the files.
";
    print "$msg \n" if ( defined($msg) );
    print $useStr;
    exit 0;
}   # Usage

getopts ('hd:pkr');

Usage() if ( defined($opt_h) );

Usage("Must only use one of -k or -r") if ( defined($opt_k) && defined($opt_r) );

# default to r if none provided
$opt_r = 1 if ( !defined($opt_k) && ! defined($opt_r) );

# dont want to make debug less than 3
setLogLevel($opt_d) if ( defined($opt_d) && $opt_d > 3);

my $coFiles = Clearcase::LsCheckoutCmd->new();

LogNorm("Checking for Checked out Files in " . $coFiles->CCcwd());

if ( $coFiles->CCexecute() == 0 )
{
    my ( $undoCo );
    my ( $numCom, $numNoCom ) = $coFiles->countFiles();

    $coFiles->printResult();
    LogNorm("");

    if ( $numCom + $numNoCom > 0 )
    {
        $undoCo = Clearcase::UndoCheckoutCmd->new();
        $undoCo->CCaddArgs("-rm") if ( defined($opt_r) );
        $undoCo->CCaddArgs("-keep") if ( defined($opt_k) );

        $undoCo->addFiles(@{$coFiles->getFileList()});

        my $answer = GetYesNoQuit("Do you want to undo check outs the files above");
        if ( $answer eq "y" )
        {
            $undoCo->CCexecute();
        }
    }
}
else
{
    LogError("Failed to execute LsCheckout Command");
}

# regardless of how or where this program end this will run (unless user Ctrl-C etc)
# so if -p supplied we put a wait for enter
END
{
    if ( $opt_p )
    {
        LogNorm("Press Enter To Continue");
        getc();
    }
}

# -------------------------------------------------------------------------
sub GetYesNoQuit
#
# -------------------------------------------------------------------------
{
    LogNorm("-n", "$_[0] (default: y) [y,n,q=quit]: ");

    while ( <STDIN> )
    {
        chomp;

        tr /A-Z/a-z/;
        
        return "y"  if ( /^$/ );
        return "$1" if ( /^ *([yn]{1})$/ );
        exit 1      if ( /^ *(q{1})/ );
        LogNorm("-n", "Please re-enter response? (default: y) [y,n,q]: ");
    }
}

