#-------------------------------------------------------------------------------
# demo.bat: AFP2web demo batch program
#
# Author  : Fa. Maas
# Date    : 2009-04-10
# Version : 1.0.1
#
# $V100   2008-07-10	Initial Release
# $V101   2009-04-10    Extended to support PNG Input
#
#-------------------------------------------------------------------------------

#----- Function, Used to get the Output Format from the User.
FETCH_OUTPUT_FORMAT(){
#-------------------------------------------------------------------------------
# Fetch output format
#-------------------------------------------------------------------------------
echo
echo -n "ENTER OUTPUT FORMAT :$ALLOWED_OUTPUTS : " 
read OUTPUT

#---- Convert the Output format into uppercase

OUTPUT=`echo $OUTPUT | tr a-z A-Z`

#---- Assert output format, if not use default (PDF)
if [ "$OUTPUT" == "" ]
then
    OUTPUT=$DEFAULT_OUTPUT
    OUTPUTTEXT=$DEFAULT_OUTPUT
fi

if [ "$OUTPUT" == "PDFA" ] 
then
    OUTPUT="PDFA"
    OUTPUTTEXT="PDF/A (Level B)"

#---- Special handling for PDF/A output
#     a. Let input sample be insure.afp
    SAMPLE="samples/insure.afp"
#     b. Add "-strict" command line option
    AFP2WEB="$AFP2WEB "-strict""
fi
    
if [ "$OUTPUT" == "ASCII" ]
then    OUTPUTTEXT="ASCII"
	OUTPUT="ASCII"

#---- Special handling for ASCII output
#     a. Let input sample be insure.afp
    SAMPLE=samples/insure.afp
fi

#---- Make sure OUTPUT & OUTPUTTEXT values are uppercase
if [ "$OUTPUT" == "PDF" ]
then
	OUTPUT="PDF"
	OUTPUTTEXT="PDF"
fi

if [ "$OUTPUT" == "TIFF" ]
then
        OUTPUTTEXT="TIFF"
	OUTPUT="TIFF"
fi

if [ $OUTPUT == "AFP" ] 
then
    OUTPUTTEXT="AFP"
    OUTPUT="AFP"
fi

if [ "$OUTPUT" == "JPEG" ]
then
    OUTPUTTEXT="JPEG"
    OUTPUT="JPEG"
fi

if [ "$OUTPUT" == "PNG" ]
then
    OUTPUTTEXT="PNG"
    OUTPUT="PNG"
fi
}

#------- Function, Used to display the Error message, when the user gives improper output format
FORMAT_NOT_ALLOWED(){
    echo
    echo $OUTPUT is not allowed for $INPUT Format
    echo Please Select one output format from $ALLOWED_OUTPUTS
    #------ Function Invoked to get output format
    FETCH_OUTPUT_FORMAT
    echo
}

#----- Function, Used to Get the Script File Name
FETCH_SCRIPT_FILENAME(){
    SCRIPTFILENAMETMP=$SCRIPTFILENAME
    echo
    echo -n "Enter Script FileName [$SCRIPTFILENAME] : "
    read SCRIPTFILENAME
    #----Assert the Script Filename
    if [ "$SCRIPTFILENAME" == "" ]
    then
        SCRIPTFILENAME=$SCRIPTFILENAMETMP
    fi
}

clear
echo "*******************************************************************************"
echo "                                AFP2web Demo"              
echo "*******************************************************************************"

#-------------------------------------------------------------------------------
# Declare and Initialize varaibles
#-------------------------------------------------------------------------------
AFP2WEB="./afp2web -q -c"

CONVERTMODE=
SCRIPTFILENAME=afp2web.pm

SAMPLE="samples/medform.afp"
INPUT=
# $V101 Begin 
ALLOWED_INPUTS="AFP,PDF,TIFF,JPEG,BMP,GIF,PCX,PNG [AFP]"
# $V101 End

OUTPUT=
OUTPUTTEXT=
DEFAULT_OUTPUT="PDF"
ALLOWED_OUTPUTS="AFP,PDF,PDFA,TIFF,JPEG,ASCII [PDF]"

#-------------------------------------------------------------------------------
# Fetch The Conversion Mode
#-------------------------------------------------------------------------------
echo
echo -n Enter Conversion Mode ALL, DOCINDEX, SCRIPTINGFACILITY,[ALL] : 
read CONVERTMODE

#-----Assert the Convert Mode
if [ "$CONVERTMODE" == "" ]
then
    CONVERTMODE="ALL"
fi

#-----------Convert the CONVERTMODE into uppercase

CONVERTMODE=`echo $CONVERTMODE | tr a-z A-Z`

if [ "$CONVERTMODE" == "ALL" ]
then
    ALLOWED_INPUTS="AFP, PDF, TIFF, JPEG, BMP, GIF, PCX, PNG [AFP]"
fi

if [ "$CONVERTMODE" == "DOCINDEX" ]
then
    ALLOWED_INPUTS="AFP [AFP]"
    SAMPLE="samples/insure.afp"
    AFP2WEB="$AFP2WEB "-doc_index""
fi

if [ "$CONVERTMODE" == "SCRIPTINGFACILITY" ]
then
    ALLOWED_INPUTS="AFP, PDF, [AFP]"
    SAMPLE="samples/insure.afp"    
    FETCH_SCRIPT_FILENAME
    AFP2WEB="$AFP2WEB "-doc_cold" "-sp:"$SCRIPTFILENAME"
fi


#-------------------------------------------------------------------------------
# Fetch input format
#-------------------------------------------------------------------------------
echo
echo -n "ENTER INPUT FORMAT "$ALLOWED_INPUTS" : "
read INPUT


#---- Assert input format
if [ "$INPUT"  == " " ]
then 
     INPUT="AFP"
fi

#---- Convert the input format into uppercase

INPUT=`echo $INPUT | tr a-z A-Z`

#---- Evaluate sample filename, allowed output formats, 
#---- default output format based on given input format
if [ "$INPUT" == "AFP" ]
then
    SAMPLE="samples/insure.afp"
    INPUT="AFP"
    DEFAULT_OUTPUT="PDF"
    ALLOWED_OUTPUTS="PDF,PDFA TIFF,JPEG,ASCII [PDF]"
    
fi

if [ "$INPUT" == "PDF" ] 
then
    SAMPLE="samples/insure.pdf"
    INPUT="PDF"
    DEFAULT_OUTPUT="AFP"
    ALLOWED_OUTPUTS="AFP [AFP]"
fi

if [ "$INPUT" == "TIFF" ]
then 
    SAMPLE="samples/medform.tif"
    INPUT="TIFF"
    ALLOWED_OUTPUTS="AFP,PDF,TIFF,JPEG,ASCII [PDF]"
fi

if [ "$INPUT" == "JPEG" ]
then
    SAMPLE="samples/medform.jpg"
    INPUT="JPEG"
    ALLOWED_OUTPUTS="AFP,PDF,TIFF,JPEG,ASCII [PDF]"
fi

if [ "$INPUT" == "BMP" ]
then
    SAMPLE="samples/bmpsample.bmp"
    INPUT="BMP"
    ALLOWED_OUTPUTS="AFP,PDF,TIFF,JPEG,ASCII [PDF]"
fi

if [ "$INPUT" == "GIF" ]
then 
    SAMPLE="samples/gifsample.gif"
    INPUT="GIF"
    ALLOWED_OUTPUTS="AFP,PDF,TIFF,JPEG,ASCII [PDF]"
fi

if [ "$INPUT" == "PCX" ] 
then
     SAMPLE="samples/pcxsample.pcx"
     INPUT="PCX"
     ALLOWED_OUTPUTS="AFP,PDF,TIFF,JPEG,ASCII [PDF]"
fi

# $V101 Begin
if [ "$INPUT" == "PNG" ] 
then
     SAMPLE="samples/pngsample.png"
     INPUT="PNG"
     ALLOWED_OUTPUTS="AFP,PDF,TIFF,JPEG [PDF]"
fi
# $V101 End

#---- Function invoking to get Ouput format from the User
FETCH_OUTPUT_FORMAT

#---- Assert output format against allowed formats
echo $ALLOWED_OUTPUTS | grep $OUTPUT > /dev/null
if [ $? -eq 1 ] 
then  
#----Function invoking to get display the error message
      FORMAT_NOT_ALLOWED
fi

#---- Fetch sample filename
SAMPLETMP=$SAMPLE
echo
echo -n "ENTER SAMPLE FILENAME [ "$SAMPLE" ]:"
read SAMPLE

#---Assert the SAMPLE 
if [ "$SAMPLE" == "" ]
then
    SAMPLE=$SAMPLETMP
fi


#-------------------------------------------------------------------------------
# Call AFP2web
#-------------------------------------------------------------------------------

#---- Build AFP2web calling string
AFP2WEB="$AFP2WEB -$OUTPUT $SAMPLE"

if [ "$CONVERTMODE" == "DOCINDEX" ]
then
    echo "Document Index Conversion Sample"
fi

if [ "$CONVERTMODE" == "SCRIPTINGFACILITY" ]
then
    echo "Scripting Facility Conversion Sample"
fi

#---- Calling the AFP2web
echo
echo
echo Converts $SAMPLE from $INPUT to $OUTPUTTEXT
echo $OUTPUT Output will be stored in the pdf directory
echo
echo $AFP2WEB
$AFP2WEB
echo AFP2web returned $?
echo
exit