#-------------------------------------------------------------------------------
# demo    : AFP2web demo batch shell script
#
# Author  : Fa.OXSEED
# Date    : 2011-02-17
# Version : 1.0.6
#
# $V100   2008-07-10	Initial Release
# $V101   2009-04-10    Extended to support PNG Input
# $V102   2009-07-20    Added support for Coded and Non-Coded outputs
#                       Extended support for PDF TO TIFF,JPEG conversions
# $V103   2009-09-09    Extended to support PNG Output
# $V104   2010-07-30    Extended to support LPD and MMD Input
# $V105   2010-11-01    Extended to support "any2html" and "afp2xml" Conversions
# $V106   2011-02-17    Extended to set library path for 64-bit Linux 
#
#-------------------------------------------------------------------------------

#----- Function, Used to get the Conversion mode from the User.
FETCH_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 Output format into uppercase
    OUTPUT=`echo $CONVERTMODE | tr a-z A-Z`

    #-----------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
        # $V104 Begin
        ALLOWED_INPUTS="AFP, PDF, LPD, MMD [AFP]"
        # $V104 End
        SAMPLE="samples/insure.afp"        
    fi
}

# $V106 Begin
#-------------------------------------------------------------------------------
# Function, Used to display the Error message, when the user gives improper Input format
#-------------------------------------------------------------------------------
CONVERSION_NOT_ALLOWED(){
    echo
    echo $CONVERTMODE is not allowed Conversion
    echo Please Select one input format from $ALLOWED_CONVERSIONS
    #------ Function Invoked to get output format
    FETCH_CONVERSION_MODE
    echo
}
# $V106 End 

#----- Function, Used to get the Input Format from the User.
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="AFP,PDF,PDFA TIFF,JPEG,ASCII,PNG [PDF]"    
    fi

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

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

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

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

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

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

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

    # $V104 Begin
    if [ "$INPUT" == "LPD" ] 
    then
         SAMPLE="samples/lpdsample.lpd"
         INPUT="LPD"
         ALLOWED_OUTPUTS="PDF,TIFF,JPEG,PNG,ASCII [PDF]"
         AFP2WEB="$AFP2WEB "-lpdin" "-sft:ASA_EBCDIC_FIXED_148""
    fi
    if [ "$INPUT" == "MMD" ] 
    then
         SAMPLE="samples/mmdsample.mmd"
         INPUT="MMD"
         ALLOWED_OUTPUTS="PDF,TIFF,JPEG,PNG,ASCII [PDF]"
         AFP2WEB="$AFP2WEB "-mmdin" "-sft:ASA_EBCDIC_FIXED_133" "-fd:f1a2w" "-pd:p1a2w""
    fi
}

#----- 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" == "AFP" ] 
then
    OUTPUTTEXT="AFP"
    OUTPUT="AFP"
fi

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

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

if [ "$OUTPUT" == "PNG" ]
then
    OUTPUTTEXT="PNG"
    OUTPUT="PNG"
fi
}
# $V104 Begin
#------- Function, Used to display the Error message, when the user gives improper input format
INPUT_FORMAT_NOT_ALLOWED(){
    echo
    echo $INPUT is not allowed for $CONVERTMODE
    echo Please Select one input format from $ALLOWED_INPUTS
    #------ Function Invoked to get input format
    FETCH_INPUT_FORMAT
    echo
}
# $V104 End

#------- 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(){
    # $V105 Begin
    echo
    SCRIPT_FILENAME_TEMP=$DEF_SCRIPT_FILENAME
    echo -n "Enter Script FileName $ALLOWED_SF_MODULES [$DEF_SCRIPT_FILENAME] : "
    # $V105 End
    read SCRIPTFILENAME
    #----Assert the Script Filename
    if [ "$SCRIPTFILENAME" == "" ]
    then
        SCRIPTFILENAME=$SCRIPT_FILENAME_TEMP
    fi
}

# $V102 Begin
#----- Function, Used to Set Output Type
SET_OUTPUT_TYPE(){
    echo   
    echo -n "NEED CODED OUTPUT? $CODED_OUTPUT_OPTIONS [Yes] : "
    read CODED_OUTPUT
    
    #---- Convert the Coded output type into uppercase
    CODED_OUTPUT=`echo $CODED_OUTPUT | tr a-z A-Z`

    #---- Assert Output Type
    if [ "$CODED_OUTPUT" == "" ] 
    then
        CODED_OUTPUT="Yes"
    fi
    if [ "$CODED_OUTPUT" == "NO" ] 
    then
        AFP2WEB="$AFP2WEB -nco"
    fi
}
# $V102 End

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

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

CONVERTMODE=
# $V105 Begin
DEF_SCRIPT_FILENAME=afp2web.pm
# $V105 End

SAMPLE="samples/medform.afp"
INPUT=
# $V104 Begin
ALLOWED_INPUTS="AFP,PDF,TIFF,JPEG,BMP,GIF,PCX,PNG,LPD,MMD [AFP]"
ALLOWED_CONVERSIONS="ALL, DOCINDEX, SCRIPTINGFACILITY"
# $V104 End

OUTPUT=
OUTPUTTEXT=
DEFAULT_OUTPUT="PDF"
# $V103 Begin
ALLOWED_OUTPUTS="AFP,PDF,PDFA,TIFF,JPEG,ASCII,PNG [PDF]"
# $V103 End
# $V102 Begin
CODED_OUTPUT="ON"
CODED_OUTPUT_OPTIONS="Yes, No"
# $V102 End
DIRECTORY=
SFMODULE=
# $V105 Begin
ALLOWED_SF_MODULES=$DEF_SCRIPT_FILENAME
# $V105 End

# $V106 Begin
#-------------------------------------------------------------------------------
# Set the Library Path
#-------------------------------------------------------------------------------
  
OSNAME=`echo $OSTYPE | tr [A-Z] [a-z]`;

case "$OSNAME" in

*aix* | *AIX*)
    #-----------Export the Library Path
    export LIBPATH=.:$LIBPATH    
    ;;
*)
    #-----------Export the Library Path
    export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
    ;;
esac
# $V106 End

#-------------------------------------------------------------------------------
# Fetch The Conversion Mode
#-------------------------------------------------------------------------------
# $V104 Begin
FETCH_CONVERSION_MODE

#---- Assert Conversion mode format against allowed Conversions
echo $ALLOWED_CONVERSIONS | grep $CONVERTMODE > /dev/null
if [ $? -eq 1 ] 
then  
#----Function invoking to get display the error message
      CONVERSION_NOT_ALLOWED
fi

#-------------------------------------------------------------------------------
# Fetch input format
#-------------------------------------------------------------------------------
FETCH_INPUT_FORMAT

#---- Assert input format against allowed formats
echo $ALLOWED_INPUTS | grep $INPUT > /dev/null
if [ $? -eq 1 ] 
then  
#----Function invoking to get display the error message
      INPUT_FORMAT_NOT_ALLOWED
fi

if [ "$CONVERTMODE" == "SCRIPTINGFACILITY" ]
then

    # $V105 Begin
    case $INPUT in
        "LPD")
            ALLOWED_SF_MODULES="sfsamples/afp2xml.pm, sfsamples/any2html.pm, sfsamples/eyecatcher_lpd.pm"
	    DEF_SCRIPT_FILENAME="sfsamples/eyecatcher_lpd.pm"
            ;;
        "MMD")
            ALLOWED_SF_MODULES="sfsamples/afp2xml.pm, sfsamples/any2html.pm, sfsamples/eyecatcher_mmd.pm"
            DEF_SCRIPT_FILENAME="sfsamples/eyecatcher_mmd.pm"
            ;;
        *)
            ALLOWED_SF_MODULES="afp2web.pm"
	    DEF_SCRIPT_FILENAME="afp2web.pm"
            ;;
    esac
    # $V105 End
    
    #---- Fetch the script filename
    FETCH_SCRIPT_FILENAME
	    
    #--------------Set the Script filename
    AFP2WEB="$AFP2WEB "-doc_cold" "-sp:"$SCRIPTFILENAME"
fi
# $V104 End

# $V105 Begin
if [ "$SCRIPTFILENAME" != "afp2web.pm" ]
then
    SF_MODULE=`echo ${SCRIPTFILENAME:10}`
else
    SF_MODULE=$DEF_SCRIPT_FILENAME      
fi

if [ "$SF_MODULE" == "any2html.pm" ]
then
    DEFAULT_OUTPUT="PNG"
    ALLOWED_OUTPUTS="PNG [PNG]"
else
    if [ "$INPUT" == "PDF" ]
    then
        SET_OUTPUT_TYPE
    fi
fi
# $V105 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
# $V104 Begin
if [ "$INPUT" != "LPD" ] &&  [ "$INPUT" != "MMD" ]
then
    echo -n "ENTER SAMPLE FILENAME [ "$SAMPLE" ]:"  
    read SAMPLE
fi
# $V104 End

#---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