#!/bin/perl -w #+ # inventory -- convert between guiname, barcode, and blueprint ID # # Purpose: # Given a guiname, barcode number, or blueprint ID number, print # out the corresponding guiname, barcode number, or blueprint ID # number. # # Usage: # inventory [ -n | -p ] [ ] # # Arguments: # -p = prime; update inventory file # -n = noprime; do not update inventory file [default] # type = [ f(ilter) | s(litmask) | g(rating) ] # from = [ g(uiname) | b(arcode) | i(d)] # value = guiname, barcode, or id value to convert # to = [ g(uiname) | b(arcode) | i(d)] # # Output: # to STDOUT # # Restrictions: # none # # Exit values: # 0 = normal completion # 255 = wrong number of arguments # -1 = no match for specified value # # Example: # 1. Print a list of all slitmasks in database: # deimos@polo> inventory s # guiname=s4 barcode=186 id=165 # guiname=s6 barcode=183 id=167 # guiname=s8 barcode=182 id=168 # guiname=sa22 barcode=181 id=169 # guiname=w42 barcode=176 id=175 # guiname=w72 barcode=177 id=174 # ... # # 2. Obtain barcode for mask with guiname "sa22": # deimos@polo> inventory s g sa22 b # 181 # # 3. Obtain guiname for mask with barcode "181": # deimos@polo> inventory s b 181 g # sa22 # # 4. Obtain guiname for mask with blueprint ID "169": # deimos@polo> inventory s i 169 g # sa22 # # 4. Obtain guiname for mask with blueprint ID "169", # first updating the list to ensure most recent data: # deimos@polo> inventory -p s i 169 g # sa22 #- # Modification history: # 2002-Oct-30 GDW Original version # 2004-Jan-15 GDW Added dremeldo=readDB to update inventory # 2004-Feb-17 GDW Added "-noprime" # 2004-Mar-19 GDW Added "-prime" and converted to Getopt # 2009-Apr-18 GDW Support multiple matches # 2009-Jun-17 GDW Fixing priming not working (changed # option parsing) #----------------------------------------------------------------------- use Getopt::Std; use strict; my( $INVENTORY) = "$ENV{KROOT}/data/deimot/dyna/inventory"; my( $usage) = "Usage: $0 [ -n | -p ] [ ]\n"; my( $guiname, $barcode, $id); my( $mode) = 0; my( $stat) = 0; my( $from, $value, $to); my( $prime) = 0; my( @result); my( $key); my( %guiname, %barcode, %id, %option); # flag check... getopts("np", \%option); if ( $option{"n"} ) { $prime = 0 } if ( $option{"p"} ) { $prime = 1; print "priming\n"; } # check args... die "$usage" unless ( $#ARGV == 0 or $#ARGV == 3 ); # parse args... my( $type) = uc(substr(shift,0,1)); if ( $#ARGV >= 0 ){ $mode = 1; $from = uc(substr(shift,0,1)); $value = uc(shift); $to = uc(substr(shift,0,1)); } # validate args... if ( $type ne "S" and $type ne "F" and $type ne "G" ) { die "$usage where 'type=$type' must be one of: S=slitmask F=filter G=grating" } if ( $mode == 1 and $from ne "G" and $from ne "B" and $from ne "I" ) { die "$usage where 'from=$from' must be one of: G=guiname B=barcode I=blueprint ID" } if ( $mode == 1 and $to ne "G" and $to ne "B" and $to ne "I" ) { die "$usage where 'to=$to' must be one of: G=guiname B=barcode I=blueprint ID" } if ( $mode == 1 and $to eq "I" and $type ne "S" ) { die "$usage where 'to=$to' can only be used with slitmasks (type=S)" } # update inventory file if the "-p" flag was specified... if ( $prime) { print( "priming...\n"); my( @args) = qw(modify -s deimot silent dremeldo=readDB); system( @args) == 0 or die; } # parse file... open INVENTORY, "<$INVENTORY" or die "Can't find file $INVENTORY"; while( ){ # chuck lines for other elements... chomp; next unless m/^$type/; # parse input line... if ( $type eq "S" ) { m/^.\s+(\S+)\s+(\S+)\s+\((\S+)\)/; $guiname = $1; $barcode = $2; $id = $3; } else { m/^.\s+(\w+)\s+(\w+)/; $guiname = $1; $barcode = $2; } # print-only mode? if ( $mode == 0 ){ printf "guiname=%-8s", $guiname; printf " barcode=%-8s", $barcode; printf " id=%-8s", $id if defined($id); printf "\n"; next; } # select key field... if ( $from eq "G"){ $key = $guiname } elsif ( $from eq "B"){ $key = $barcode } elsif ( $from eq "I"){ $key = $id } else { die "Invalid 'from' type '$from'" } # build hash... $key = uc($key); # start or append, depending on whether this hash element is already # defined... if (exists $guiname{$key}){ push @{$guiname{$key}}, $guiname; push @{$barcode{$key}}, $barcode; push @{$id{$key}}, $id; } else { $guiname{$key} = [$guiname]; $barcode{$key} = [$barcode]; $id{$key} = [$id]; } } close INVENTORY; # find match only if received input... if( $mode == 1){ # select output... if( $to eq "G"){ @result = @{$guiname{$value}} } elsif ( $to eq "B" ){ @result = @{$barcode{$value}} } elsif ( $to eq "I" ){ @result = @{$id{$value}} } else { die "Invalid 'to' type '$to'" } # print result... if( not @result){ @result = qw("INDEF"); $stat = -1; } print "@result\n"; } exit $stat;