#!/usr/bin/perl

my @fieldNameArr;
my @fieldTypeArr;
my @promptArr;
my @nomodArr;
my $n=0;
my $depth=0; # are we in a record?

sub printHeader {
    local ($w,$h)=@_;
        print << "EOF"
4 0 1
beginScreenProperties
major 4
minor 0
release 1
x $x
y 175
w $w
h $h
font "helvetica-bold-i-18.0"
fontAlign "center"
ctlFont "courier-bold-r-14.0"
ctlFontAlign "right"
btnFont "EOD"
fgColor index 10
bgColor index 3
textColor index 10
ctlFgColor1 index 25
ctlFgColor2 index 30
ctlBgColor1 index 5
ctlBgColor2 index 10
topShadowColor index 5
botShadowColor index 10
pvType "EPICS"
endScreenProperties
EOF
}

sub printTextMonitor {
local ($x,$y,$w,$h,$fieldName)=@_;

print <<"EOF"
# (Text Monitor)
object activeXTextDspClass:noedit
beginObjectProperties
major 4
minor 1
release 0
x $x
y $y
w $w
h $h
controlPv "\$(PV).$fieldName"
font "helvetica-bold-i-12.0"
#fontAlign "center"
fgColor index 10
bgColor index 5
useDisplayBg
autoHeight
limitsFromDb
nullColor index 30
useHexPrefix
newPos
objType "monitors"
endObjectProperties
EOF
}

sub printMenuButton {
local ($x,$y,$w,$h,$fieldName)=@_;

print <<"EOF"
# (Menu Button)
object activeMenuButtonClass
beginObjectProperties
major 4
minor 0
release 0
x $x
y $y
w $w
h $h
fgColor index 10
bgColor index 5
inconsistentColor index 10
topShadowColor index 5
botShadowColor index 10
controlPv "\$(PV).$fieldName"
font "helvetica-medium-r-10.0"
endObjectProperties
EOF
}

sub printStaticText {
local ($x,$y,$w,$h,$prompt)=@_;

print <<"EOF"
# (Static Text)
object activeXTextClass
beginObjectProperties
major 4
minor 1
release 0
x $x
y $y
w $w
h $h
font "helvetica-bold-r-10.0"
fontAlign "right"
fgColor index 10
bgColor index 5
useDisplayBg
value {
  "$prompt"
}
endObjectProperties
EOF
}

sub printTextControl {
local ($x,$y,$w,$h,$prompt)=@_;

print <<"EOF"
# (Text Control)
object activeXTextDspClass
beginObjectProperties
major 4
minor 1
release 0
x $x
y $y
w $w
h $h
controlPv "\$(PV).$fieldName"
font "helvetica-bold-r-10.0"
fgColor index 25
bgColor index 5
useDisplayBg
editable
motifWidget
limitsFromDb
nullColor index 30
newPos
objType "controls"
endObjectProperties
EOF
}


while (<>)
{

    if ( m/recordtype\((.*)/ )
    {
	$depth=1;
    }
    if ( m/field\((.*),(.*)\)/ )
    {
	$depth++;
	$fieldName=$1;
	$fieldType=$2;
	$prompt="";
	$nomod=0;
    }
    if ( m/prompt\((.*)\)/ )
    {
	$prompt=$1;
	$prompt=~ s/"//g;
    }
    if ( m/special\((.*)\)/ )
    {
	$tmp=$1;
	if ( $tmp =~ m/SPC_NOMOD/ ) { $nomod=1 };
    }
    if ( m/}/ )  # end of the field
    {
	if ( $depth==2 && $fieldType !~ m/DBF_NOACCESS/ )
	{
	    $n=push(@fieldNameArr,$fieldName);
	    push(@fieldTypeArr,$fieldType);
	    push(@promptArr,$prompt);
	    push(@nomodArr,$nomod);
	    $depth--;
	}
    }
}



# Margins
$top=10;
$left=10;

$y=$top;
$h=16; # Widget height
$hh=18; # Widget top-top
$col1=220;
$col2=80;

printHeader(300,$n*$hh+2*$top+20);

for ($index=0;$index<$n;$index++, $y=$y+$hh)
{
    $x=$left;
    $fieldName=$fieldNameArr[$index];

    printStaticText($x,$y,$col1,$h,$promptArr[$index].": ".$fieldName);
    $x=$x+$col1+4;

    if ($nomodArr[$index])
    {
	printTextMonitor($x,$y,$col2,$h,$fieldName);
	$x=$x+$col2+4;
    }
    elsif( $fieldTypeArray[$index] =~ m/DBF_MENU/ )
    {
	printMenuButton($x,$y+1,$col2,$h,$fieldName);
	$x=$x+$col2+4;
    }
    else
    {
	printTextControl($x,$y-1,$col2,$h,$fieldName);
	$x=$x+$col2+4;
    }
}
