#!/bin/bash


SCHED_FILE="auto_bs_schedule.edl"
ACTIVE_FILE="auto_bs_active.edl"

if [ ! -d "bs_panels" ];then
    mkdir "bs_panels"
fi
PWD=$(pwd)/"bs_panels"
TEMP_DIR="/tmp"  #$PWD/DispTemp
#mkdir $TEMP_DIR 

pv_list=('LINE_DESC' 'VES_REC_P' 'VES_HOLD_P' 'VES_MAX_P' 'TUBE_REC_P' 'TUBE_HOLD_P' 'TUBE_MAX_P' 'WIN_REC_P' 'WIN_HOLD_P' 'WIN_MAX_P' 'OVEN_TEMP' 'OVEN_RAMP' 'OVEN_TEMP_CONT' 'FIL_AMPS' 'FIL_RAMP' 'MIN_TIME_NEXT' 'MAX_TIME_NEXT' 'VES_COOLING' 'MAN_HEAT_TAPE' 'MAX_TIME_ACTION' 'OP_INTERVENTION' 'COMMENT') # 'INTLK_MODE' 'KLYS_SERIAL' 'KLYS_TYPE' 'REV'

station='BS:B44:$(BS)'

function screen_props {
    echo -e '4 0 1' > "$temp"
    echo -e 'beginScreenProperties' >> "$temp"
    echo -e 'major 4' >> "$temp"
    echo -e 'minor 0' >> "$temp"
    echo -e 'release 1' >> "$temp"
    echo -e 'x 8' >> "$temp"
    echo -e 'y 83' >> "$temp"
    echo -e "w $disp_width" >> "$temp"
    echo -e "h $disp_height" >> "$temp"
    echo -e 'font "helvetica-medium-r-18.0"' >> "$temp"
    echo -e 'ctlFont "helvetica-medium-r-18.0"' >> "$temp"
    echo -e 'btnFont "helvetica-medium-r-18.0"' >> "$temp"
    echo -e 'fgColor index 14' >> "$temp"
    echo -e 'bgColor index 0' >> "$temp"
    echo -e 'textColor index 14' >> "$temp"
    echo -e 'ctlFgColor1 index 14' >> "$temp"
    echo -e 'ctlFgColor2 index 0' >> "$temp"
    echo -e 'ctlBgColor1 index 0' >> "$temp"
    echo -e 'ctlBgColor2 index 14' >> "$temp"
    echo -e 'topShadowColor index 0' >> "$temp"
    echo -e 'botShadowColor index 14' >> "$temp"
    echo -e 'endScreenProperties\n' >> "$temp"
}

function mux {
    echo -e '# (Menu Mux)' >> "$temp"
    echo -e 'object menuMuxClass' >> "$temp"
    echo -e 'beginObjectProperties' >> "$temp"
    echo -e 'major 4' >> "$temp"
    echo -e 'minor 1' >> "$temp"
    echo -e 'release 0' >> "$temp"
    echo -e "x $xpos" >> "$temp"
    echo -e "y $ypos" >> "$temp"
    echo -e 'w 115' >> "$temp"
    echo -e 'h 30' >> "$temp"
    echo -e 'fgColor index 14' >> "$temp"
    echo -e 'bgColor index 0' >> "$temp"
    echo -e 'topShadowColor index 0' >> "$temp"
    echo -e 'botShadowColor index 14' >> "$temp"
    echo -e 'font "helvetica-medium-r-18.0"' >> "$temp"
    echo -e 'initialState "0"' >> "$temp"
    echo -e 'numItems 2' >> "$temp"
    echo -e 'symbolTag {' >> "$temp"
    echo -e '  0 "Config 0"' >> "$temp"
    echo -e '  1 "Config 1"' >> "$temp"
    echo -e '  2 "Config 2"' >> "$temp"
    echo -e '  3 "Config 3"' >> "$temp"
    echo -e '  4 "Config 4"' >> "$temp"
    echo -e '  5 "Config 5"' >> "$temp"
    echo -e '  6 "Config 6"' >> "$temp"
    echo -e '  7 "Config 7"' >> "$temp"
    echo -e '  8 "Config 8"' >> "$temp"
    echo -e '  9 "Config 9"' >> "$temp"
    echo -e '  10 "Config 10"' >> "$temp"
    echo -e '}' >> "$temp"
    echo -e 'symbol0 {' >> "$temp"
    echo -e '  0 "C"' >> "$temp"
    echo -e '  1 "C"' >> "$temp"
    echo -e '  2 "C"' >> "$temp"
    echo -e '  3 "C"' >> "$temp"
    echo -e '  4 "C"' >> "$temp"
    echo -e '  5 "C"' >> "$temp"
    echo -e '  6 "C"' >> "$temp"
    echo -e '  7 "C"' >> "$temp"
    echo -e '  8 "C"' >> "$temp"
    echo -e '  9 "C"' >> "$temp"
    echo -e '  10 "C"' >> "$temp"
    echo -e '}' >> "$temp"
    echo -e 'value0 {' >> "$temp"
    echo -e '  0 "0"' >> "$temp"
    echo -e '  1 "1"' >> "$temp"
    echo -e '  2 "2"' >> "$temp"
    echo -e '  3 "3"' >> "$temp"
    echo -e '  4 "4"' >> "$temp"
    echo -e '  5 "5"' >> "$temp"
    echo -e '  6 "6"' >> "$temp"
    echo -e '  7 "7"' >> "$temp"
    echo -e '  8 "8"' >> "$temp"
    echo -e '  9 "9"' >> "$temp"
    echo -e '  10 "10"' >> "$temp"
    echo -e '}' >> "$temp"
    echo -e 'endObjectProperties\n' >> "$temp"
}

function static_text {
    echo -e '# (Static Text)' >> "$temp"
    echo -e 'object activeXTextClass' >> "$temp"
    echo -e 'beginObjectProperties' >> "$temp"
    echo -e 'major 4' >> "$temp"
    echo -e 'minor 1' >> "$temp"
    echo -e 'release 1' >> "$temp"
    echo -e "x $xpos" >> "$temp"
    echo -e "y $ypos" >> "$temp"
    echo -e "w $width" >> "$temp"
    echo -e 'h 15' >> "$temp"
    echo -e "font \"helvetica-bold-r-14.0\"" >> "$temp"
    echo -e "fontAlign \"$align\"" >> "$temp"
    if [ $alarm -eq 1 ]; then
	echo -e 'fgColor index 88' >> "$temp"
	echo -e "alarmPv \"CALC\\\\\{A==$line}($station:ACTIVE_LINE)\"" >> "$temp"
    else
	echo -e 'fgColor index 14' >> "$temp"
    fi
    echo -e 'bgColor index 0' >> "$temp"
    echo -e 'useDisplayBg' >> "$temp"
    echo -e 'value {' >> "$temp"
    echo -e "  \"$text\"" >> "$temp"
    echo -e '}' >> "$temp"
    echo -e 'autoSize' >> "$temp"
    echo -e 'endObjectProperties\n' >> "$temp" 
}

function skip_to_line {
    # Shell script which launches zenity interface.  Kludge due to read
    echo -e '# (Shell skip_to_line)' >> "$temp" 
    echo -e 'object shellCmdClass' >> "$temp" 
    echo -e 'beginObjectProperties' >> "$temp" 
    echo -e 'major 4' >> "$temp" 
    echo -e 'minor 3' >> "$temp" 
    echo -e 'release 0' >> "$temp" 
    echo -e "x $xpos" >> "$temp" 
    echo -e "y $ypos" >> "$temp" 
    echo -e "w $width" >> "$temp" 
    echo -e 'h 15' >> "$temp" 
    echo -e 'fgColor index 14' >> "$temp" 
    echo -e 'bgColor index 0' >> "$temp" 
    echo -e 'topShadowColor index 0' >> "$temp" 
    echo -e 'botShadowColor index 14' >> "$temp" 
    echo -e "font \"helvetica-medium-r-$fsize\"" >> "$temp" 
    echo -e 'invisible' >> "$temp" 
    echo -e 'numCmds 1' >> "$temp" 
    echo -e 'command {' >> "$temp" 
    echo -e "  0 \"zenity --question && caput $station:ACTIVE_LINE_REQ $text && caput $station:ACTIVE_LINE_CMD 1\"" >> "$temp"
    echo -e '}' >> "$temp" 
    echo -e 'endObjectProperties\n' >> "$temp" 
    echo -e '# (Shell skip_to_line end)' >> "$temp"
}

function text_control_zen {
    # Shell script which launches zenity interface.  Kludge due to read
    echo -e '# (Shell Command)' >> "$temp" 
    echo -e 'object shellCmdClass' >> "$temp" 
    echo -e 'beginObjectProperties' >> "$temp" 
    echo -e 'major 4' >> "$temp" 
    echo -e 'minor 3' >> "$temp" 
    echo -e 'release 0' >> "$temp" 
    echo -e "x $xpos" >> "$temp" 
    echo -e "y $ypos" >> "$temp" 
    echo -e "w $width" >> "$temp" 
    echo -e 'h 15' >> "$temp" 
    echo -e 'fgColor index 14' >> "$temp" 
    echo -e 'bgColor index 0' >> "$temp" 
    echo -e 'topShadowColor index 0' >> "$temp" 
    echo -e 'botShadowColor index 14' >> "$temp" 
    echo -e "font \"helvetica-medium-r-$fsize\"" >> "$temp" 
    echo -e 'invisible' >> "$temp" 
    echo -e 'numCmds 1' >> "$temp" 
    echo -e 'command {' >> "$temp" 
    echo -e "  0 \"caput $reqpv \`zenity --text=\'New Value:\' --entry\`\"" >> "$temp" 
    echo -e '}' >> "$temp" 
    echo -e 'endObjectProperties\n' >> "$temp" 
}

function text_monitor {
    # Text monitor widget for PV readback
    echo -e '# (Text Monitor)' >> "$temp"
    echo -e 'object activeXTextDspClass:noedit' >> "$temp"
    echo -e 'beginObjectProperties' >> "$temp"
    echo -e 'major 4' >> "$temp"
    echo -e 'minor 7' >> "$temp"
    echo -e 'release 0' >> "$temp"
    echo -e "x $xpos" >> "$temp"
    echo -e "y $ypos" >> "$temp"
    echo -e "w $width" >> "$temp"
    echo -e 'h 15' >> "$temp"
    echo -e "controlPv \"$pv\"" >> "$temp"
    echo -e "font \"helvetica-medium-r-$fsize\"" >> "$temp"
    echo -e "fontAlign \"$align\"" >> "$temp"
    echo -e 'fgColor index 14' >> "$temp"
    echo -e 'bgColor index 0' >> "$temp"
    echo -e 'useDisplayBg' >> "$temp"
    echo -e 'autoHeight' >> "$temp"
    echo -e 'limitsFromDb' >> "$temp"
    echo -e 'nullColor index 0' >> "$temp"
    echo -e 'useHexPrefix' >> "$temp"
    echo -e 'newPos' >> "$temp"
    echo -e 'objType "monitors"' >> "$temp"
    echo -e 'endObjectProperties\n' >> "$temp"
}

function text_control {
    echo -e '# (Text Control)' >> "$temp"
    echo -e 'object activeXTextDspClass' >> "$temp"
    echo -e 'beginObjectProperties' >> "$temp"
    echo -e 'major 4' >> "$temp"
    echo -e 'minor 7' >> "$temp"
    echo -e 'release 0' >> "$temp"
    echo -e "x $xpos" >> "$temp"
    echo -e "y $ypos" >> "$temp"
    echo -e "w $width" >> "$temp"
    echo -e 'h 15' >> "$temp"
    echo -e "controlPv \"$pv\"" >> "$temp"
    echo -e "font \"helvetica-medium-r-$fsize\"" >> "$temp"
    echo -e "fontAlign \"$align\"" >> "$temp"
    echo -e 'fgColor index 14' >> "$temp"
    echo -e 'bgColor index 0' >> "$temp"
    echo -e 'useDisplayBg' >> "$temp"
    echo -e 'editable' >> "$temp"
    if [ $exponential -eq 1 ]; then 
	echo -e "format \"exponential\"" >> "$temp"
    fi
    echo -e 'precision 2' >> "$temp"
    echo -e 'autoHeight' >> "$temp"
    #echo -e 'limitsFromDb' >> "$temp"
    echo -e 'nullColor index 0' >> "$temp"
    echo -e 'useHexPrefix' >> "$temp"
    echo -e 'newPos' >> "$temp"
    echo -e 'smartRefresh' >> "$temp"
    echo -e 'objType "controls"' >> "$temp"
    echo -e 'endObjectProperties\n' >> "$temp"
}

function button {
    echo -e '# (Message Button)' >> "$temp"
    echo -e 'object activeMessageButtonClass' >> "$temp"
    echo -e 'beginObjectProperties' >> "$temp"
    echo -e 'major 4' >> "$temp"
    echo -e 'minor 1' >> "$temp"
    echo -e 'release 0' >> "$temp"
    echo -e "x $xpos" >> "$temp"
    echo -e "y $ypos" >> "$temp"
    echo -e "w $width" >> "$temp"
    echo -e 'h 30' >> "$temp"
    echo -e 'fgColor index 14' >> "$temp"
    echo -e 'onColor index 0' >> "$temp"
    echo -e 'offColor index 0' >> "$temp"
    echo -e 'topShadowColor index 0' >> "$temp"
    echo -e 'botShadowColor index 14' >> "$temp"
    echo -e "controlPv \"$pv\"" >> "$temp"
    echo -e 'pressValue "1"' >> "$temp"
    echo -e 'releaseValue "0"' >> "$temp"
    echo -e "onLabel $text" >> "$temp"
    echo -e "offLabel $text" >> "$temp"
    echo -e '3d' >> "$temp"
    echo -e 'useEnumNumeric' >> "$temp"
    echo -e 'font "helvetica-medium-r-18.0"' >> "$temp"
    echo -e 'endObjectProperties\n' >> "$temp"
}

function rectangle {
    # Just a rectangle
    echo -e '# (Rectangle)' >> "$temp"
    echo -e 'object activeRectangleClass' >> "$temp"
    echo -e 'beginObjectProperties' >> "$temp"
    echo -e 'major 4' >> "$temp"
    echo -e 'minor 0' >> "$temp"
    echo -e 'release 0' >> "$temp"
    echo -e "x $rect_x" >> "$temp"
    echo -e "y $rect_y" >> "$temp"
    echo -e "w $rect_width" >> "$temp"
    echo -e "h $rect_height" >> "$temp"
    echo -e 'lineColor index 14' >> "$temp"
    echo -e 'fill' >> "$temp"
    echo -e "fillColor index $rect_color" >> "$temp"
    echo -e 'lineWidth 0' >> "$temp"
    echo -e 'endObjectProperties' >> "$temp"
}

function header {
    # Creates Header row for table
    fsize='14.0'
    for pv in ${pv_list[@]}; do
        ypos=$y_base
    
        if [ "$pv" == "LINE_DESC" ] || [ "$pv" == "COMMENT" ]; then
    	    width=120
        elif [ "$pv" == "VES_COOLING" ]; then
	    width=37
        elif [ "$pv" == "MAX_TIME_ACTION" ] || [ "$pv" == "OP_INTERVENTION" ]; then
	    width=45
        #elif [ "$pv" == "KLYS_SERIAL" ]; then
	#    width=60
        else
	    width=50
        fi
        
        let 'xpos += (width + x_spacing)'
	if [ "$pv" == "OP_INTERVENTION" ];then
	    text="OP\nINTER\nREQ"
	else
            text=$(echo "$pv" | sed 's/_/\\n/g')
        fi
        lines=$(echo -e "$text" | wc -l)
        let 'ypos -= (lines * 5)'
	
        static_text
        if [ "$pv" == "LINE_DESC" ] || [ "$pv" == "COMMENT" ]; then
	    let 'xpos += 75'
        elif [ "$pv" == "MAX_TIME_ACTION" ]; then
            let 'xpos += 10'
        elif [ "$pv" == "VES_COOLING" ]; then
	    let 'xpos += 13'
        #elif [ "$pv" == "KLYS_SERIAL" ]; then
	#    let 'xpos += 20'
        fi
    done
}


pv=''
xpos=5
ypos=10
width=50
alarm=0
x_spacing=15
y_spacing=30
disp_width=1890
disp_height=1350
exponential=0

echo "Generating schedule display files..."
temp="$PWD/$SCHED_FILE"
> $temp

# Setup basic display settings
screen_props

rect_width=$disp_width
rect_height=35
rect_color=6
rect_x=0
rect_y=0
rectangle

# Create config selection menu
#mux
#let 'xpos += 130'
#let 'ypos += 8'
#let 'width = disp_width - 40'
width=50
align='right'
fsize='18.0'
text='Name:'
static_text
let 'xpos += 50'
width=500
align='left'
pv="$station:BUFFER_NAME"
text_control

let 'xpos += 500'
width=50
align='right'
text='Rev:'
static_text
let 'xpos += 50'
width=50
align='left'
pv="$station:BUFFER_REV"
text_control

let 'xpos += 50'
width=50
align='right'
text='Type:'
static_text
let 'xpos += 50'
width=50
align='left'
pv="$station:BUFFER_KLYS_TYPE"
text_control

let 'xpos += 50'
width=50
align='right'
text='Serial:'
static_text
let 'xpos += 50'
width=70
align='left'
pv="$station:BUFFER_KLYS_SERIAL"
text_control

let 'xpos += 200'
let 'ypos -= 8'
width=70
align='center'
pv="$station:SAVE_SCHEDULE"
text='SAVE'
button

# Create table header
y_base=60
xpos=20
align='center'
header

# Create table
base="$station:Stored_"
ypos=70
for line in {0..39};do
    alarm=1
    xpos=10
    let 'ypos += y_spacing'
    width=50
    
    temp="$TEMP_DIR/BakeSchedule$line"
    #echo "$temp"
    > "$temp"

    
    if [ $line -eq 20 ];then
	xpos=20
        let 'y_base = ypos + 10'
	align='center'
        header
	xpos=10
	let 'ypos = (y_base + y_spacing + 10)'
	width=50
    fi

    #echo "Starting line $line..."
    if [ $((line%2)) -eq 0 ];then
	rect_height=25
	let 'rect_width = disp_width - 40'
	let 'rect_y = ypos - 5'
	let 'rect_x = xpos - 5'
	rect_color=1
	rectangle
    fi
    
    text="$line"
    skip_to_line
    text="Line $line"
    fsize='14.0'
    static_text
    #echo "Line $line: $xpos\, $width"
    for pv in ${pv_list[@]}; do
	let 'xpos += (width + x_spacing)'
	#echo "$pv: $xpos"
	if [ "$pv" == "LINE_DESC" ] || [ "$pv" == "COMMENT" ]; then
	    width=200
	    align='left'
	#elif [ "$pv" == "KLYS_SERIAL" ]; then
	#    width=80
	else
	    width=50
            align='center'
	fi
	
	if [[ $pv == *"_P" ]]; then
	    exponential=1
	else
	    exponential=0
	fi

	pv=$base$pv$line"_REQ"	
	#reqpv=$pv"_REQ"
        
	fsize='12.0'
	text_control
	#text_monitor
    done
    text="Line $line"
    let 'xpos += (width + x_spacing)'
    width=50
    fsize='14.0'
    static_text
    echo -e "\n" >> "$temp"    
done

cat $TEMP_DIR/BakeSchedule* >> "$PWD/$SCHED_FILE"

# Use Schedule file to create Active display file
cp "$PWD/$SCHED_FILE" "$PWD/$ACTIVE_FILE"
sed -i -e '/^#.*skip_to_line/,/^#.*skip_to_line end/d' -e '/^alarmPv.*/d' -e 's/fgColor index 88/fgColor index 14/g' "$PWD/$SCHED_FILE"

sed -i -e 's/Stored/Active/g' -e 's/BUFFER/ACTIVE/g' -e 's/SAVE_SCHEDULE/PUSH_ACTIVE_CONFIG/g' "$PWD/$ACTIVE_FILE"

#echo "Cleaning up temp files..."
#rm -f $TEMP_DIR/BakeSchedule*
#rmdir $TEMP_DIR
echo "Done."

exit 0
