#!/bin/perl
#
#-----------------------------------------------------------------------
#  Script Name:		test_label_name.pl Clearcase trigger script to check label naming convention
#
#  Function:		This script will trigger before a label is created
#			and format the label according to required project conventions
#			Modified from IBM script at http://www.ibm.com/developerworks/rational/library/4311.html
#
#  Author:		Lantrix (techdebug.com)
#
#  Creation Date:	18/09/2007
#
#  Version:		1.00
#
#  Input Variables:	$CLEARCASE_LBTYPE
#			$CLEARCASE_USER
#
#  Output Files:	N/A
#
#  Error Messages/Error Codes:
#			N/A
#
#-----------------------------------------------------------------------
#
# Configuration Variables
#
my $USERPREFIX = 'USER';
my $DEFECTPREFIX = 'DEFECT';
my $PROJ1_PREFIX = 'PROJECTA';
my $PROJ2_PREFIX = 'PROJECTB';
my $REL_PREFIX = 'REL';
my $BASEPREFIX = 'BASELINE';

#######################################
#
# function definition
#

sub checkUserName {
  my $user = shift(@_);

  my $currentUser = uc($ENV{CLEARCASE_USER});

 if ($user !~ m/^($currentUser)/) {
    `clearprompt proceed -mask proceed -type error -prompt \"Can't create label for this user\"`;
    exit 1;
  }
  exit 0;
}


####################################################
#
# main section
#

{
  my $label = $ENV{CLEARCASE_LBTYPE};

  # check, if label is in uppercase format
  if ($label ne uc($label)) {
    `clearprompt proceed -mask proceed -type error -prompt \"CC Error: Label not in uppercase\"`;
    exit 1;
  }

  # decompose label
  if ($label =~ m/^($USERPREFIX)_(.*)_(.*)$/) {
    &checkUserName($2);
  } elsif ($label =~ m/^($DEFECTPREFIX)_(.*)$/) {
    exit 0;
  } elsif ($label =~ m/^($PROJ1_PREFIX)_(.*)$/) {
    exit 0;
  } elsif ($label =~ m/^($PROJ2_PREFIX)_(.*)$/) {
    exit 0;
  } elsif ($label =~ m/^($REL_PREFIX)_(.*)$/) {
    exit 0;
  } elsif ($label =~ m/^($BASEPREFIX)_(.*)$/) {
    exit 0;
  } else {
    # no valid prefix found
    `clearprompt proceed -mask proceed -type error -prompt \"CC Error: No valid Label specified - please read project guidelines\"`;
    exit 1;
  }
}
