#!/usr/bin/perl

# acceptable forms:
#    ctquery            - list all attached projects and flavors
#    ctquery project    - list the attached flavor of the named project
#    ctquery - flavor   - list all attached projects who are attached with a
#                         given flavor

$projs = $ENV{"CTPROJS"} ;
@projlist = split( / +/, $projs ) ;

if ( $#ARGV == -1 ) {
   # list all projects and flavors
   print "Currently attached projects (and flavors):\n" ;
   foreach $pair ( @projlist ) {
      @pairlist = split( /:/, $pair ) ;
      ( $pairtmp = $pairlist[0] ) =~ tr/A-Z/a-z/ ;
      print "   $pairtmp ($pairlist[1])\n" ;
   }
} elsif (( $#ARGV == 0 ) && !($ARGV[0] =~ /^\-/)) {
   # list the attached flavor of the named project
   foreach $pair ( @projlist ) {
      @pairlist = split( /:/, $pair ) ;
      ( $pairtmp = $pairlist[0] ) =~ tr/A-Z/a-z/ ;
      if ( $pairtmp eq $ARGV[0] ) {
	 print "$pairlist[1]\n" ;
      }
   }
} elsif (( $#ARGV == 1 ) && ( $ARGV[0] eq "-" )){
   # list all attached projects who are attached with a given flavor
   foreach $pair ( @projlist ) {
      @pairlist = split( /:/, $pair ) ;
      if ( $pairlist[1] eq $ARGV[1] ) {
	 $pairlist[0] =~ tr/A-Z/a-z/ ;
	 print "$pairlist[0]\n" ;
      }
   }
} else {
   print "Usage: ctquery [project]  -or-\n" ;
   print "       ctquery - flavor\n" ;
   exit ;
}
