#!/bin/csh -f

#directory with contacts 
set contacts=/home/jiri/mycontacts/b77f94ce-cd6c-ee95-1e4c-dd9172f217df

set dofail=1
if ( x$1 == x-n ) then
set dofail=0
shift
endif

#find contact
set isnumber=`echo $1|awk '{if($1 ~ "^[+0-9][+0-9]*$") print 1; else print 0;}'`
if ($isnumber == 1) then
	set number=$1
else
	set found=`grep -i -l '^N:.*'$1'.*;;;' $contacts/*.vcf`
	if ( ${#found} == 0 ) then
		if ( $dofail == 0 ) exit 0
		echor -r name $1 not found
		exit 1
	endif
	if ( ${#found} > 1 ) then
		set alternatives=`grep '^N:.*;;;' $found | awk -F: '{print $3}' |tr ';' ' '`
                echor -r name $1 not unique, choose between: $alternatives
		if ( $dofail == 0 ) then
			echo $found
			exit 0
		endif
                exit 1
        endif
	set number=`grep 'TEL.*:' $found|awk -F: '{print $2}'|head -1|sed -e 's/ //g'`
endif

echo $number $found
