#/bin/sh

ONAPPVERSION=
MODE=
DIR=
MASK=
OUT_FILE=
SQL_UPDATE=0
TMPLVERSION_OLD=
COMPUTE_MD5=1
DELIMITER=
CHECK_VZ=1
CDN=0
CDN_LABEL='OnApp CDN Appliance'
LBVA=0
LBVA_LABEL='Load Balancer Virtual Appliance'
ASVA=0
ASVA_LABEL='Application Server Appliance'
CHECK_SMART=1
NOT_SMART="ubuntu-8.04-x64 endian-2.5-x86"
NOT_RESIZE="cpanel"
CHECK_BAREMETAL=1
NOT_BAREMETAL="centos-7.0-x64 rhel-7.0-x64"
VER_TO_LABEL=0
QUIET=0
TABLENAME='templates'

LIVE_MIGRATE_DISABLE="openvz cloudlinux"

usage()
{
    echo >&2 "Usage: $0 -v 2.0|2.1|2.2|2.3|3.0.0|3.0.1|3.0.2|3.0.3|3.0.4|3.0.5|3.0.6|3.0.7|3.0.8|3.1|3.2|3.3|3.4|3.5|4.0|4.1|4.2|4.3|5.0|5.1|5.2|5.3|5.4}5.5|5.6|5.7|5.8|5.9|5.10|6.0|6.1|6.2|6.3|6.4|6.5|6.6|6.7|6.8 -m SQL|CSV|TXT -d TMPL_DIR -t TMPL_MASK [-5] [-p PARAMS -l DELIMITER] [-o OUT_FILE] [-u WHERE_STATEMENT] [-D TABLE_NAME] [-z] [-c|-b|-w] [-s] [-r] [-q] [-h]
                           -v ONAPP_VERSION : OnApp Control Panel version:
                                              2.0       - for OnApp 2.0.x versions
                                              2.1 = 2.2 - for OnApp 2.1.x and 2.2.0 and 2.2.0.x versions
                                              2.3       - for OnApp 2.3.x versions
                                              3.0.0     - for OnApp 3.0.0 version
                                              3.0.1     - for OnApp 3.0.1 version
                                              3.0.2     - for OnApp 3.0.2 version
                                              3.0.3     - for OnApp 3.0.3 version
                                              3.0.4     - for OnApp 3.0.4 version
                                              3.0.5     - for OnApp 3.0.5 version
                                              3.0.6     - for OnApp 3.0.6 version
                                              3.0.7     - for OnApp 3.0.7 version
                                              3.0.8     - for OnApp 3.0.8 version
                                              3.1     - for OnApp 3.1.x versions
                                              3.2     - for OnApp 3.2.x versions
                                              3.3     - for OnApp 3.3.x versions
                                              3.4     - for OnApp 3.4.x versions
                                              3.5     - for OnApp 3.5.x versions
                                              4.0     - for OnApp 4.0.x versions
                                              4.1     - for OnApp 4.1.x versions
                                              4.2     - for OnApp 4.2.x versions
                                              4.3     - for OnApp 4.3.x versions
                                              5.0     - for OnApp 5.0.x versions
                                              5.1     - for OnApp 5.1.x versions
                                              5.2     - for OnApp 5.2.x versions
                                              5.3     - for OnApp 5.3.x versions
                                              5.4     - for OnApp 5.4.x versions
                                              5.5     - for OnApp 5.5.x versions
                                              5.6     - for OnApp 5.6.x versions
                                              5.7     - for OnApp 5.7.x versions
                                              5.8     - for OnApp 5.8.x versions
                                              5.9     - for OnApp 5.9.x versions
                                              5.10    - for OnApp 5.10.x versions
                                              6.0     - for OnApp 6.0.x versions
                                              6.1     - for OnApp 6.1.x versions
                                              6.2     - for OnApp 6.2.x versions
                                              6.3     - for OnApp 6.3.x versions
                                              6.4     - for OnApp 6.4.x versions
                                              6.5     - for OnApp 6.5.x versions
                                              6.6     - for OnApp 6.6.x versions
                                              6.7     - for OnApp 6.7.x versions
                                              6.8     - for OnApp 6.8.x versions
                           -m SQL|CSV|TXT : output format:
                                            SQL - INSERT to add new templates, or UPDATE to update templates (with -u option only)
                                            CSV - CSV format with 'Title;URL_Title;Categories' included
                                            TXT - any other required params reporting. Used with -p option only
                           -d TMPL_DIR : directory where template's files are located
                           -t TMPL_MASK : templates which need to be processed mask.
                                          Note, that you should slash the following like: '\?' or '\*'
                           -o OUT_FILE : Out to file. Not mandatory option.
                           -u WHERE_STATEMENT: Generate update SQL statement 'UPDATE templates set ... <WHERE_STATEMENT>;'
                                               Note the option works with '-m SQL' only.
                           -p PARAMS : Used with '-m TXT' mode only
                                      PARAMS are:
                                                 LABEL
                                                 TMPLVERSION
                                                 OS
                                                 DISTRO
                                                 VIRTUALIZATION
                                                 DISKSIZE
                                                 MIN_MEMORY_SIZE
                                                 RESIZE
                                                 MIGRATE
                                                 ONAPP_VERSION_MIN_REQ
						 CREATED
                           -l DELIMITER : Used with '-m TXT' mode only
                                          Sets delimiter for output fields in TXT mode.
                                          Default delimeter is ';'
                           -D TABLE_NAME : custom teble name in the database
                                           Default is 'templates'
                           -5 : Don't compute MD5 message digest
                           -z : Don't check for supported virtualization
                           -c : Use 'OnApp CDN Appliance' settings for the template(s).
                                Can't be used if -b or -w
                           -b : Use 'Load Balancer Virtual Appliance' settings for the template(s)
                                Can't be used if -c or -w
                           -w : Use 'Application Server Virtual Appliance' settings for the template(s)
                                Can't be used if -c or -b
                           -s : Do not check whether the template is \"smart\" or \"baremetal\"
                           -r : Include template version to the Label string
                           -q : Be quiet
                           -h : Print this info"
}

while getopts v:m:d:t:o:p:l:D:hz5cbwsrqu: opts
do      case "$opts" in
        h)      usage; exit 0;;
        v)      ONAPPVERSION="$OPTARG";;
        D)      TABLENAME="$OPTARG";;
        5)      COMPUTE_MD5=0;;
        z)      CHECK_VZ=0;;
        r)      VER_TO_LABEL=1;;
        s)      CHECK_SMART=0; CHECK_BAREMETAL=0;;
        m)      MODE="$OPTARG";;
        d)      DIR="$OPTARG";;
        t)      MASK="$OPTARG";;
        o)      OUT_FILE="$OPTARG";;
        p)      PARAMS="$OPTARG";;
        l)      DELIMITER="$OPTARG";;
        q)      QUIET=1;;
        u)      SQL_UPDATE=1; WHERE_STATEMENT="$OPTARG";;
        c)      CDN=1; [ $LBVA -eq 1 -o $ASVA -eq 1 ] && echo -e "\n-c and -b or -w options can't be used togetger !\n" >&2 && usage && exit 1;;
        b)      LBVA=1; [ $CDN -eq 1 -o $ASVA -eq 1 ] && echo -e "\n-b and -c or -w options can't be used togetger !\n" >&2 && usage && exit 1;;
        w)      ASVA=1; [ $CDN -eq 1 -o $LBVA -eq 1 ] && echo -e "\n-w and -b or -c options can't be used togetger !\n" >&2 && usage && exit 1;;
        esac
done

if [ "x$ONAPPVERSION" = "x" ]; then
    usage
    exit 0
fi

case $ONAPPVERSION in
    2.0)       # OnApp 2.0.x versions
        ;;
    2.1|2.2)   # OnApp 2.1.x and 2.2.0 and 2.2.0.x versions
        ;;
    2.3)       # OnApp 2.3.x versions
        ;;
    3.0.0)     # OnApp 3.0.0 version
        ;;
    3.0.1)     # OnApp 3.0.1 version
        ;;
    3.0.2)     # OnApp 3.0.2 version
        ;;
    3.0.3)     # OnApp 3.0.3 version
        ;;
    3.0.4)     # OnApp 3.0.4 version
        ;;
    3.0.5)     # OnApp 3.0.5 version
        ;;
    3.0.6)     # OnApp 3.0.6 version
        ;;
    3.0.7)     # OnApp 3.0.7 version
        ;;
    3.0.8)     # OnApp 3.0.8 version
        ;;
    3.1)     # OnApp 3.1 version
        ;;
    3.2)     # OnApp 3.2 version
        ;;
    3.3)     # OnApp 3.3 version
        ;;
    3.4)     # OnApp 3.4 version
        ;;
    3.5)     # OnApp 3.5 version
        ;;
    4.0)     # OnApp 4.0 version
        ;;
    4.1)     # OnApp 4.1 version
        ;;
    4.2)     # OnApp 4.2 version
        ;;
    4.3)     # OnApp 4.3 version
        ;;
    5.0)     # OnApp 5.0 version
        ;;
    5.1)     # OnApp 5.1 version
        ;;
    5.2)     # OnApp 5.2 version
        ;;
    5.3)     # OnApp 5.3 version
        ;;
    5.4)     # OnApp 5.4 version
        ;;
    5.5)     # OnApp 5.5 version
        ;;
    5.6)     # OnApp 5.6 version
        ;;
    5.7)     # OnApp 5.7 version
        ;;
    5.8)     # OnApp 5.8 version
        ;;
    5.9)     # OnApp 5.9 version
        ;;
    5.10)     # OnApp 5.10 version
        ;;
    6.0)     # OnApp 6.0 version
        ;;
    6.1)     # OnApp 6.1 version
        ;;
    6.2)     # OnApp 6.2 version
        ;;
    6.3)     # OnApp 6.3 version
        ;;
    6.4)     # OnApp 6.4 version
        ;;
    6.5)     # OnApp 6.5 version
        ;;
    6.6)     # OnApp 6.6 version
        ;;
    6.7)     # OnApp 6.7 version
        ;;
    6.8)     # OnApp 6.8 version
        ;;
      *) echo; echo "The OnApp version $ONAPPVERSION isn't supported"; echo;
        usage;
        exit 0;;
esac

if [ "x$MODE" != "xCSV" -a "x$MODE" != "xTXT" ]; then
    MODE="SQL"
fi

if [ ! -e $DIR ]; then
    echo "Directory not found: $DIR" >&2
    usage
    exit 1
fi

if [ "x$MASK" = "x" ]; then
    echo "Please provide mask for templates" >&2
    usage
    exit 1
fi

if [ ! "x$OUT_FILE" = "x" ]; then
    if [ -e $OUT_FILE ]; then
        true
        #rm -f $OUT_FILE || exit 1
    fi
fi

if [ $SQL_UPDATE -gt 0 -a "x$WHERE_STATEMENT" = "x" ]; then
    echo "Please put WHERE_STATEMENT to generate 'UPDATE' SQL string" >&2
    usage
    exit 1
fi

cd $DIR || exit 1

if [ "x$MODE" = "xCSV" ]; then
    HEADER='Title;URL_Title;Categories'
    if [ ! "x$OUT_FILE" = "x" ]; then
        echo $HEADER > $OUT_FILE
    else
        echo $HEADER
    fi
fi
if [ "x$MODE" = "xTXT" -a "x${PARAMS}" != "x" ]; then
    for PARAM in $PARAMS; do
        HEADER="${HEADER}${DELIMITER}${PARAM}"
    done
    if [ ! "x$OUT_FILE" = "x" ]; then
        echo $HEADER > $OUT_FILE
    else
        echo $HEADER
    fi
fi

for FILE in `find . -name "$MASK" | sort`; do
    if [ ! "x$OUT_FILE" = "x" ]; then
        if [ $QUIET -eq 0 ]; then echo "Processing $FILE ..."; fi
    fi
    FILE_PATH=$FILE
    FILE=`basename $FILE_PATH`
    OS='linux'
    DISTROEDITION='NULL'
    DISTROTAIL='NULL'
    LABEL=
    TMPLVERSION=
    RESIZE='0'
    MIGRATE='0'
    DISTRO='ubuntu'
    DISKSIZE=5
    SWAP=1
    DISK_TARGET_DEVICE='---\nxen: sda\nkvm: hd\n'
    VIRTUALIZATION='xen,kvm'
    FILE_NAME=$FILE
    if `echo $FILE|grep '.tar.gz' >/dev/null 2>&1`; then
        FULLNAME=`echo $FILE|sed "s/-[[:digit:]]\.[[:digit:]]\.tar\.gz$//"`
    elif `echo $FILE|grep '.ova' >/dev/null 2>&1`; then
        FULLNAME=`echo $FILE|sed "s/-[[:digit:]]\.[[:digit:]]\.ova$//"`
    fi
    MIN_MEMORY_SIZE=128
    ONAPP_VERSION_MIN_REQ='2.2.0'
    EXT4=0
    CREATED=$( stat --format=%y $FILE_PATH | awk '{print $1}' 2>/dev/null )

# *** JumpBox ***

    if echo $FILE|grep jumpbox >/dev/null 2>&1; then
        if `echo $FILE|grep '.tar.gz' >/dev/null 2>&1`; then
            DATA=`echo $FILE|sed 's/^jumpbox-\(.*\)\.tar\.gz$/\1/'`
        elif `echo $FILE|grep '.ova' >/dev/null 2>&1`; then
            DATA=`echo $FILE|sed 's/^jumpbox-\(.*\)\.ova$/\1/'`
        fi
        ARCH=`echo $DATA|cut -d- -f1`
        APPNAME=`echo $DATA|cut -d- -f2`
        APPVERSION=`echo $DATA|cut -d- -f3`
        TMPLVERSION=`echo $DATA|cut -d- -f4`
        VIRTUALIZATION=`echo $DATA|cut -d- -f5`
        if [ "x${VIRTUALIZATION}" = "x" ]; then
            VIRTUALIZATION='xen,kvm'
        else
            VIRTUALIZATION=`echo ${VIRTUALIZATION}| sed 's/\./,/g' 2>/dev/null`
        fi
        if [ $VER_TO_LABEL -eq 1 ]; then
            LABEL="Jumpbox $APPNAME $APPVERSION $ARCH $TMPLVERSION"
        else
            LABEL="Jumpbox $APPNAME $APPVERSION $ARCH"
        fi
        DISKSIZE=9
        MIN_MEMORY_SIZE=512
        EXT4=1
        RESIZE='0'

# *** Windows ***

    elif echo $FILE|egrep -e '^win' >/dev/null 2>&1; then

        if ! `echo $FILE|egrep -e '-ver.\..' >/dev/null 2>&1`; then
            echo "" >&2
            echo "    Windows template name $FILE_PATH is incorrect. it must contain '-verX.X' !" >&2
            echo "" >&2
            continue
        fi
        DATA=
        VIRTUALIZATION=
        if `echo $FILE|grep '.tar.gz' >/dev/null 2>&1`; then
            DATA=`echo $FILE|sed 's/^win\(.*\)\.tar\.gz$/\1/'`
            FILE_NAME=`echo $FILE|sed 's/\.tar\.gz$//'`".img"
        elif `echo $FILE|grep '.ova' >/dev/null 2>&1`; then
            DATA=`echo $FILE|sed 's/^win\(.*\)\.ova$/\1/'`
            FILE_NAME=`echo $FILE|sed 's/\.ova$//'`".img"
        elif `echo $FILE|grep '.img' >/dev/null 2>&1`; then
            DATA=`echo $FILE|sed 's/^win\(.*\)\.img$/\1/'`
            FILE_NAME=$FILE
        else
            continue
        fi
        TMPL_DATA=`echo $DATA|cut -d- -f1`
        APP_DATA=`echo $DATA|cut -d- -f2`
        TMPLVERSION_DATA=`echo $DATA|cut -d- -f3`
        VIRTUALIZATION=`echo $DATA|cut -d- -f4`
        if [ "x$VIRTUALIZATION" = "x" ]; then
            VIRTUALIZATION='xen'
        else
            VIRTUALIZATION=`echo ${VIRTUALIZATION}| sed 's/\./,/g' 2>/dev/null`
        fi
        if [ "x$TMPLVERSION_DATA" = "x" -o "x$TMPLVERSION_DATA" = "xkvm" -o "x$TMPLVERSION_DATA" = "xxen" -o "x$TMPLVERSION_DATA" = "xkvm_virtio" -o "x$TMPLVERSION_DATA" = "xbaremetal" -o "x$TMPLVERSION_DATA" = "xvmware" ]; then
            VIRTUALIZATION=`echo $TMPLVERSION_DATA | sed 's/\./,/g' 2>/dev/null`
            if [ "x$VIRTUALIZATION" = "x" ]; then
                VIRTUALIZATION='xen'
            fi
            TMPLVERSION_DATA=$APP_DATA
            APP_DATA=
        fi

        if echo $VIRTUALIZATION 2>/dev/null | grep virtio >/dev/null 2>&1; then
            VIRTUALIZATION='kvm,kvm_virtio'
        fi

        OS='windows'
        SWAP=0
        DISK_TARGET_DEVICE='---\nxen: hd\nkvm: hd\n'
        DISTRONAME='Windows'
        DISTROVERSION=`echo $TMPL_DATA|cut -d_ -f1`
        ARCH=`echo $TMPL_DATA|cut -d_ -f2`
        DISTROEDITION=`echo $TMPL_DATA|cut -d_ -f3`
        DISTROTAIL=`echo $TMPL_DATA|cut -d_ -f4`
        case $DISTROVERSION in
            03) DISTROVERSION='2003'
                DISKSIZE=10
                MIN_MEMORY_SIZE=384
            ;;
            08) DISTROVERSION='2008'
                DISKSIZE=20
                MIN_MEMORY_SIZE=1024
                if [ "x${VIRTUALIZATION}" = "xxen" ]; then
                    MIGRATE=1
                fi
            ;;
            7)  DISTROVERSION='7'
                DISKSIZE=20
                MIN_MEMORY_SIZE=1024
                if [ "x${VIRTUALIZATION}" = "xxen" ]; then
                    MIGRATE=1
                fi
            ;;
            xp) DISTROVERSION='XP'
                DISKSIZE=10
            ;;
            12) DISTROVERSION='2012'
                DISKSIZE=20
                MIN_MEMORY_SIZE=1024
                if [ "x${VIRTUALIZATION}" = "xxen" ]; then
                    MIGRATE=1
                fi
            ;;
             8) DISTROVERSION='8'
                DISKSIZE=20
                MIN_MEMORY_SIZE=1024
                if [ "x${VIRTUALIZATION}" = "xxen" ]; then
                    MIGRATE=1
                fi
            ;;
             10) DISTROVERSION='10'
                DISKSIZE=20
                MIN_MEMORY_SIZE=1024
            ;;
             16) DISTROVERSION='2016'
                DISKSIZE=20
                MIN_MEMORY_SIZE=1024
            ;;
             19) DISTROVERSION='2019'
                DISKSIZE=20
                MIN_MEMORY_SIZE=1024
            ;;
             vista) DISTROVERSION='Vista'
                DISKSIZE=20
                MIN_MEMORY_SIZE=1024
                if [ "x${VIRTUALIZATION}" = "xxen" ]; then
                    MIGRATE=1
                fi
            ;;
             "8.1") DISTROVERSION='8.1'
                DISKSIZE=20
                MIN_MEMORY_SIZE=1024
                if [ "x${VIRTUALIZATION}" = "xxen" ]; then
                    MIGRATE=1
                fi
            ;;
             22) DISTROVERSION='2022'
                DISKSIZE=30
                MIN_MEMORY_SIZE=1024
                MIGRATE=1
            ;;
             *) continue
            ;;
        esac
        DISTRO=$DISTROVERSION
        case $DISTROEDITION in
            ent) DISTROEDITION="'ENT'"
                 DISTROEDITION_FULL='Enterprise Edition'
             ;;
            std) DISTROEDITION="'STD'"
                 DISTROEDITION_FULL='Standard Edition'
             ;;
             dc) DISTROEDITION="'DC'"
                 DISTROEDITION_FULL='Datacenter Edition'
             ;;
           web) DISTROEDITION="'WEB'"
                DISTROEDITION_FULL='Web Edition'
             ;;
           prof) DISTROEDITION="'PRO'"
                DISTROEDITION_FULL='Professional Edition'
             ;;
           bsn) DISTROEDITION="'BSN'"
                DISTROEDITION_FULL='Business Edition'
             ;;
	   bus) DISTROEDITION="'BSN'"
		DISTROEDITION_FULL='Business Edition'
	     ;;
             *)
               continue
             ;;
        esac
        case $DISTROTAIL in
             r2) DISTROTAIL="'R2'"
                 DISTROTAIL_FULL='R2'
             ;;
              *) DISTROTAIL="''"
                 DISTROTAIL_FULL=''
             ;;
        esac
        TMPLVERSION=`echo $TMPLVERSION_DATA|sed 's/ver//'`
        if [ "x$TMPLVERSION" = "x" ]; then
            continue
        fi
        if [ $VER_TO_LABEL -eq 1 ]; then
            LABEL="Windows $DISTROVERSION $DISTROEDITION_FULL $DISTROTAIL_FULL $APP_DATA $ARCH $TMPLVERSION"
        else
            LABEL="Windows $DISTROVERSION $DISTROEDITION_FULL $DISTROTAIL_FULL $APP_DATA $ARCH"
        fi

        if echo $VIRTUALIZATION 2>/dev/null | grep virtio >/dev/null 2>&1; then
            LABEL="${LABEL} kvm_virtio"
        fi

        if echo $APP_DATA 2>/dev/null | grep sql >/dev/null 2>&1; then
        case $DISTROVERSION in
            2012) DISKSIZE=30
            ;;
            2008) DISKSIZE=30
            ;;
             *) true
            ;;
        esac
        fi
        if echo $TMPLVERSION | egrep -e '^4\.' >/dev/null 2>&1; then
            DISKSIZE=30
            if echo $APP_DATA 2>/dev/null | grep sql >/dev/null 2>&1; then
                DISKSIZE=40
            fi
        fi
    else

# *** Linux ***

        MASK1=
        DISTRONAME=
        if echo $FILE|grep centos >/dev/null 2>&1; then
            MASK1='centos'
            DISTRONAME="CentOS"
            MIGRATE='1'
            RESIZE='1'
            DISTRO='rhel'
            MIN_MEMORY_SIZE=256
            if echo $FILE | grep '\-6\.' >/dev/null 2>&1 || echo $FILE | grep '\-7\.' >/dev/null 2>&1 || echo $FILE | grep '\-8\.' >/dev/null 2>&1; then
                if ! echo $FILE | grep '\-6\.0' >/dev/null 2>&1; then
                    DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
                fi
                MIN_MEMORY_SIZE=384
                if echo $FILE | grep '\-7\.9' >/dev/null 2>&1; then
                    MIN_MEMORY_SIZE=640
                fi
                EXT4=1
            fi
            if echo $FILE | grep '\-7\-' >/dev/null 2>&1 || echo $FILE | grep '\-8\.4' >/dev/null 2>&1; then
                MIN_MEMORY_SIZE=512
            fi
            if echo $FILE|grep lbva >/dev/null 2>&1; then
                MASK1='centos'
                DISTRONAME=$LBVA_LABEL
                DISTRO='lbva'
                LBVA=1
            fi
            if echo $FILE | grep -i 'minimal' >/dev/null 2>&1; then
                MIN_MEMORY_SIZE=128
            fi
            if echo $FILE | grep -i 'IS_benchmark' >/dev/null 2>&1; then
                MIN_MEMORY_SIZE=4096
                DISKSIZE=40
            fi
            if echo $FILE | grep -i 'onapp' >/dev/null 2>&1; then
                MIN_MEMORY_SIZE=2048
                DISKSIZE=10
            fi
        elif echo $FILE|grep xenserver >/dev/null 2>&1; then
            MASK1='xenserver'
            DISTRONAME="XenServer"
            DISTRO='rhel'
            MIN_MEMORY_SIZE=2048
        elif echo $FILE|grep fedora >/dev/null 2>&1; then
            MASK1='fedora'
            DISTRONAME="Fedora"
            MIGRATE='1'
            RESIZE='1'
            DISTRO='rhel'
            MIN_MEMORY_SIZE=256
            if echo $FILE | grep '\-15' >/dev/null 2>&1; then
                MIGRATE='0'
            fi
            if echo $FILE | grep '\-3' >/dev/null 2>&1; then
                MIN_MEMORY_SIZE=512
            fi
            EXT4=1
        elif echo $FILE|grep amazonlinux >/dev/null 2>&1; then
            MASK1='amazonlinux'
            DISTRONAME="Amazon Linux"
            MIGRATE='1'
            RESIZE='1'
            DISTRO='rhel'
            MIN_MEMORY_SIZE=512
            DISKSIZE=6
            EXT4=1
        elif echo $FILE|grep clearos >/dev/null 2>&1; then
            MASK1='clearos'
            DISTRONAME="ClearOS"
            MIGRATE='1'
            RESIZE='1'
            DISTRO='rhel'
            MIN_MEMORY_SIZE=1024
            DISKSIZE=10
            EXT4=1
            if echo $FILE | grep '\-6\.' >/dev/null 2>&1; then
                DISTRONAME="ClearOS Community"
                if ! echo $FILE | grep '\-6\.0' >/dev/null 2>&1; then
                    DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
                fi
            fi
        elif echo $FILE|grep smeserver >/dev/null 2>&1; then
            MASK1='smeserver'
            DISTRONAME="SME Server"
            MIGRATE='1'
            DISTRO='rhel'
        elif echo $FILE|grep cloudlinux >/dev/null 2>&1; then
            MASK1='cloudlinux'
            DISTRONAME="CloudLinux Server"
            RESIZE='1'
            MIGRATE='1'
            DISTRO='rhel'
            MIN_MEMORY_SIZE=512
            if echo $FILE | grep '\-7\.9' >/dev/null 2>&1; then
                 MIN_MEMORY_SIZE=640
            fi
            if echo $FILE | grep '\-6\.' >/dev/null 2>&1; then
                EXT4=1
            fi
            if ! echo $FILE | grep '\-5\.' >/dev/null 2>&1 && ! echo $FILE | grep '\-6\.1' >/dev/null 2>&1 && ! echo $FILE | grep '\-6\.2' >/dev/null 2>&1; then
                DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
            fi
        elif echo $FILE|grep ubuntu >/dev/null 2>&1; then
            MASK1='ubuntu'
            DISTRONAME="Ubuntu"
            MIGRATE='1'
            RESIZE='1'
            if ! echo $FILE | grep '\-8\.04' >/dev/null 2>&1; then
                EXT4=1
            fi
            if echo $FILE | grep '\-12\.' >/dev/null 2>&1 || \
               echo $FILE | grep '\-13\.' >/dev/null 2>&1 || \
               echo $FILE | grep '\-14\.' >/dev/null 2>&1 || \
               echo $FILE | grep '\-15\.' >/dev/null 2>&1; then
                MIN_MEMORY_SIZE=256
            fi
            if echo $FILE | grep '\-16\.' >/dev/null 2>&1 || \
               echo $FILE | grep '\-17\.' >/dev/null 2>&1 || \
               echo $FILE | grep '\-18\.' >/dev/null 2>&1 || \
               echo $FILE | grep '\-19\.' >/dev/null 2>&1 || \
               echo $FILE | grep '\-20\.' >/dev/null 2>&1 || \
               echo $FILE | grep '\-21\.' >/dev/null 2>&1 || \
               echo $FILE | grep '\-22\.' >/dev/null 2>&1; then
                MIN_MEMORY_SIZE=512
            fi
            if echo $FILE | grep '\-23\.' >/dev/null 2>&1; then
                MIN_MEMORY_SIZE=768
            fi
            if echo $FILE | grep -i 'IS_benchmark' >/dev/null 2>&1; then
                MIN_MEMORY_SIZE=4096
                DISKSIZE=40
            fi
        elif echo $FILE|grep debian >/dev/null 2>&1; then
            MASK1='debian'
            DISTRONAME="Debian"
            RESIZE='1'
            if echo $FILE | grep '\-6\.' >/dev/null 2>&1; then
                if echo $FILE | grep -i 'Vyatta' >/dev/null 2>&1; then
                    EXT4=1
                    MIGRATE='1'
                    MIN_MEMORY_SIZE=256
                    BAREMETAL=0
                fi
            fi
            if echo $FILE | grep '\-7\.' >/dev/null 2>&1 || echo $FILE | grep '\-8\.' >/dev/null 2>&1 || echo $FILE | grep '\-9\.' >/dev/null 2>&1 || echo $FILE | grep '\-10\.' >/dev/null 2>&1 || echo $FILE | grep '\-11\.' >/dev/null 2>&1 || echo $FILE | grep '\-12\.' >/dev/null 2>&1; then
                EXT4=1
                MIGRATE='1'
            fi
            if echo $FILE | grep '\-10\.' >/dev/null 2>&1 || echo $FILE | grep '\-11\.' >/dev/null 2>&1 || echo $FILE | grep '\-12\.' >/dev/null 2>&1; then
                MIN_MEMORY_SIZE=384
            fi
        elif echo $FILE|grep alpinelinux >/dev/null 2>&1; then
            MASK1='alpinelinux'
            DISTRONAME="Alpine Linux"
            RESIZE='1'
            EXT4=1
            MIGRATE='1'
            MIN_MEMORY_SIZE=256
        elif echo $FILE|grep yunohost >/dev/null 2>&1; then
            MASK1='yunohost'
            DISTRONAME="YunoHost"
            RESIZE='1'
            EXT4=1
            MIGRATE='1'
            MIN_MEMORY_SIZE=512
        elif echo $FILE|grep elastix >/dev/null 2>&1; then
            MASK1='elastix'
            DISTRONAME="Elastix"
            MIGRATE='1'
            RESIZE='1'
            DISTRO='rhel'
            MIN_MEMORY_SIZE=512
            if echo $FILE | grep '\-2\.4' >/dev/null 2>&1; then
                DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
            fi
        elif echo $FILE|grep redflag >/dev/null 2>&1; then
            MASK1='redflag'
            DISTRONAME="Red Flag Desktop"
            MIGRATE='0'
            DISTRO='rhel'
            MIN_MEMORY_SIZE=512
            DISKSIZE=20
        elif echo $FILE|grep mageia >/dev/null 2>&1; then
            MASK1='mageia'
            DISTRONAME="Mageia"
            MIGRATE='1'
            DISTRO='rhel'
            MIN_MEMORY_SIZE=512
        elif echo $FILE|grep slackware >/dev/null 2>&1; then
            MASK1='slackware'
            DISTRONAME="Slackware"
            MIGRATE='1'
            DISTRO='slackware'
            MIN_MEMORY_SIZE=256
	    if echo $FILE|grep '15\.' >/dev/null 2>&1; then
                MIN_MEMORY_SIZE=384
            fi
        elif echo $FILE|grep gentoo >/dev/null 2>&1; then
            MASK1='gentoo'
            DISTRONAME="Gentoo"
            DISTRO='gentoo'
            RESIZE='1'
            if ! echo $FILE|grep '2\.6' >/dev/null 2>&1 && ! echo $FILE|grep '11\.2' >/dev/null 2>&1 && ! echo $FILE|grep '12\.0' >/dev/null 2>&1; then
                MIGRATE='1'
            fi
            MIN_MEMORY_SIZE=256
            if echo $FILE|grep '11\.2' >/dev/null 2>&1 || echo $FILE|grep '12\.' >/dev/null 2>&1 || echo $FILE|grep '13\.' >/dev/null 2>&1 || echo $FILE|egrep -e '201[[:digit:]]\.' >/dev/null 2>&1 || echo $FILE|egrep -e '202[[:digit:]]\.' >/dev/null 2>&1; then
                EXT4=1
            fi
	    if echo $FILE|grep '2019\.' >/dev/null 2>&1 || echo $FILE|grep '2020\.' >/dev/null 2>&1; then
                DISKSIZE=7
            fi
	    if echo $FILE|grep '2021\.' >/dev/null 2>&1; then
                DISKSIZE=10
                MIN_MEMORY_SIZE=512
            fi
        elif echo $FILE|grep pbxware >/dev/null 2>&1; then
            MASK1='pbxware'
            DISTRONAME="PBXware"
            DISTRO='gentoo'
        elif echo $FILE|grep sabayon >/dev/null 2>&1; then
            MASK1='sabayon'
            DISTRONAME="Sabayon Linux"
            DISTRO='gentoo'
            RESIZE='1'
            MIGRATE='1'
        elif echo $FILE|grep rhel >/dev/null 2>&1; then
            MASK1='rhel'
            DISTRONAME="Red Hat Enterprise Linux"
            RESIZE='1'
            MIN_MEMORY_SIZE=256
            MIGRATE='1'
            if echo $FILE | grep '\-6\.' >/dev/null 2>&1 \
	    || echo $FILE | grep '\-7\.' >/dev/null 2>&1 \
	    || echo $FILE | grep '\-8\.' >/dev/null 2>&1 \
	    || echo $FILE | grep '\-9\.' >/dev/null 2>&1; then
                if echo $FILE | grep '\-6\.0' >/dev/null 2>&1; then
                    MIGRATE='0'
                else
                    DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
                fi
                if ! echo $FILE | grep 'OELS' >/dev/null 2>&1; then
                    EXT4=1
                fi
                MIN_MEMORY_SIZE=384
                if echo $FILE | grep '\-6\.7' >/dev/null 2>&1 \
		|| echo $FILE | grep '\-6\.8' >/dev/null 2>&1 \
		|| echo $FILE | grep '\-6\.9' >/dev/null 2>&1 \
		|| echo $FILE | grep '\-6\.10' >/dev/null 2>&1 \
		|| echo $FILE | grep '\-6\.11' >/dev/null 2>&1 \
		|| echo $FILE | grep '\-7\.' >/dev/null 2>&1 \
		|| echo $FILE | grep '\-8\.' >/dev/null 2>&1 \
		|| echo $FILE | grep '\-9\.' >/dev/null 2>&1; then
                    MIN_MEMORY_SIZE=512
                fi
                if echo $FILE | grep '\-7\.9' >/dev/null 2>&1; then
                    MIN_MEMORY_SIZE=640
                fi
            else
                if echo $FILE | grep '\-5\.7' >/dev/null 2>&1 || echo $FILE | grep '\-5\.8' >/dev/null 2>&1; then
                    DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
                fi
            fi
            DISTRO='rhel'
            DISKSIZE=6
        elif echo $FILE|grep oraclelinux >/dev/null 2>&1; then
            MASK1='oraclelinux'
            DISTRONAME="Oracle Linux Server"
            DISTRO='rhel'
            RESIZE='1'
            DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
            EXT4=1
            MIN_MEMORY_SIZE=384
            if echo $FILE | grep '\-6\.7' >/dev/null 2>&1 || echo $FILE | grep '\-6\.8' >/dev/null 2>&1 || echo $FILE | grep '\-6\.9' >/dev/null 2>&1 || echo $FILE | grep '\-6\.10' >/dev/null 2>&1 || echo $FILE | grep '\-7\.' >/dev/null 2>&1; then
                MIN_MEMORY_SIZE=512
            fi
            if echo $FILE | grep '\-7\.9' >/dev/null 2>&1 || echo $FILE | grep '\-8\.' >/dev/null 2>&1; then
                MIN_MEMORY_SIZE=640
            fi
        elif echo $FILE|grep almalinux >/dev/null 2>&1; then
            MASK1='almalinux'
            DISTRONAME="AlmaLinux"
            DISTRO='rhel'
            RESIZE='1'
            DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
            EXT4=1
            MIN_MEMORY_SIZE=640
        elif echo $FILE|grep springdalelinux >/dev/null 2>&1; then
            MASK1='springdalelinux'
            DISTRONAME="Springdale Linux"
            DISTRO='rhel'
            RESIZE='1'
            DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
            EXT4=1
            MIN_MEMORY_SIZE=640
        elif echo $FILE|grep kalilinux >/dev/null 2>&1; then
            MASK1='kalilinux'
            DISTRONAME="Kali Linux"
            DISTRO='ubuntu'
            RESIZE='1'
            MIGRATE='1'
            DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
            EXT4=1
            MIN_MEMORY_SIZE=384
        elif echo $FILE|grep rockylinux >/dev/null 2>&1; then
            MASK1='rockylinux'
            DISTRONAME="Rocky Linux"
            DISTRO='rhel'
            RESIZE='1'
            DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
            EXT4=1
            MIN_MEMORY_SIZE=640
        elif echo $FILE|grep vzlinux >/dev/null 2>&1; then
            MASK1='vzlinux'
            DISTRONAME="VzLinux"
            DISTRO='rhel'
            RESIZE='1'
            DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
            EXT4=1
            MIN_MEMORY_SIZE=640
        elif echo $FILE|grep freepbx >/dev/null 2>&1; then
            MASK1='freepbx'
            DISTRONAME="FreePBX"
            DISTRO='rhel'
            RESIZE='1'
            DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
            EXT4=1
            MIN_MEMORY_SIZE=1024
            DISKSIZE=10
        elif echo $FILE|grep openfileresa >/dev/null 2>&1; then
            MASK1='openfileresa'
            DISTRONAME="Openfiler ESA"
            RESIZE='1'
            MIGRATE='1'
            DISTRO='rhel'
            DISKSIZE=6
        elif echo $FILE|grep scientificlinux >/dev/null 2>&1; then
            MASK1='scientificlinux'
            DISTRONAME="Scientific Linux"
            RESIZE='1'
            MIGRATE='1'
            MIN_MEMORY_SIZE=256
            EXT4=1
            DISTRO='rhel'
            if echo $FILE | grep '\-6\.' >/dev/null 2>&1; then
                if ! echo $FILE | grep '\-6\.0' >/dev/null 2>&1; then
                    DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
                    MIGRATE='0'
                fi
                MIN_MEMORY_SIZE=384
            else
                if echo $FILE | grep '\-5\.7' >/dev/null 2>&1; then
                    DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
                fi
            fi
            if echo $FILE | grep '\-7\.' >/dev/null 2>&1; then
                MIN_MEMORY_SIZE=512
                if echo $FILE | grep '\-7\.9' >/dev/null 2>&1; then
                    MIN_MEMORY_SIZE=640
                fi
            fi
        elif echo $FILE|grep endian >/dev/null 2>&1; then
            MASK1='endian'
            DISTRONAME="Endian Firewall Appliance"
            RESIZE='0'
            MIGRATE='0'
            DISTRO='rhel'
            MIN_MEMORY_SIZE=512

# *** FreeBSD ***

        elif echo $FILE|grep freebsd >/dev/null 2>&1; then
            MASK1='freebsd'
            DISTRONAME="FreeBSD"
            RESIZE='0'
            MIGRATE='0'
            DISTRO='freebsd'
            OS='freebsd'
            VIRTUALIZATION='xen,kvm'
            DISK_TARGET_DEVICE='---\nxen: hd\nkvm: hd\n'
            MIN_MEMORY_SIZE=512
        elif echo $FILE|grep spamtitan >/dev/null 2>&1; then
            MASK1='spamtitan'
            DISTRONAME="SpamTitan"
            RESIZE='0'
            MIGRATE='0'
            DISTRO='freebsd'
            OS='freebsd'
            VIRTUALIZATION='xen,kvm'
            DISK_TARGET_DEVICE='---\nxen: hd\nkvm: hd\n'
            MIN_MEMORY_SIZE=512
        elif echo $FILE|grep webtitan >/dev/null 2>&1; then
            MASK1='webtitan'
            DISTRONAME="WebTitan"
            RESIZE='0'
            MIGRATE='0'
            DISTRO='freebsd'
            OS='freebsd'
            DISK_TARGET_DEVICE='---\nxen: hd\nkvm: hd\n'
            MIN_MEMORY_SIZE=512
            if echo $FILE | grep '\-5\.' >/dev/null 2>&1; then
                ONAPP_VERSION_MIN_REQ=3.3.0
            fi
        elif echo $FILE|grep pfsense >/dev/null 2>&1; then
            MASK1='pfsense'
            DISTRONAME="pfSense"
            RESIZE='0'
            MIGRATE='0'
            DISTRO='freebsd'
            OS='freebsd'
            VIRTUALIZATION='kvm'
            DISK_TARGET_DEVICE='---\nxen: hd\nkvm: hd\n'
            MIN_MEMORY_SIZE=512
        elif echo $FILE|grep opnsense >/dev/null 2>&1; then
            MASK1='opnsense'
            DISTRONAME="OPNsense"
            RESIZE='0'
            MIGRATE='0'
            DISTRO='freebsd'
            OS='freebsd'
            VIRTUALIZATION='kvm'
            DISK_TARGET_DEVICE='---\nxen: hd\nkvm: hd\n'
            MIN_MEMORY_SIZE=512
        elif echo $FILE|grep freenas >/dev/null 2>&1; then
            MASK1='freenas'
            DISTRONAME="FreeNAS"
            RESIZE='0'
            MIGRATE='0'
            DISTRO='freebsd'
            OS='freebsd'
            VIRTUALIZATION='kvm'
            DISK_TARGET_DEVICE='---\nxen: hd\nkvm: hd\n'
            MIN_MEMORY_SIZE=512
        elif echo $FILE|grep archlinux >/dev/null 2>&1; then
            MASK1='archlinux'
            DISTRONAME="Arch Linux"
            DISTRO='archlinux'
            MIGRATE='1'
            DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
            MIN_MEMORY_SIZE=256
            EXT4=1
            RESIZE='1'
            if echo $FILE | grep '\-x86\-' >/dev/null 2>&1; then
                RESIZE='0'
            fi
        elif echo $FILE|grep opensuse >/dev/null 2>&1; then
            MASK1='opensuse'
            DISTRONAME="openSUSE"
            RESIZE='1'
            DISTRO='suse'
            DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
            if echo $FILE | grep '\-12\.' >/dev/null 2>&1; then
                MIGRATE='1'
            fi
        elif echo $FILE|grep sles >/dev/null 2>&1; then
            MASK1='sles'
            DISTRONAME="SUSE Linux Enterprise Server"
            RESIZE='1'
            DISTRO='suse'
            DISK_TARGET_DEVICE='---\nxen: xvda\nkvm: hd\n'
            if echo $FILE | grep '\-12\.' >/dev/null 2>&1; then
                MIGRATE='1'
            fi
            MIN_MEMORY_SIZE=512
        elif echo $FILE|grep coreos >/dev/null 2>&1; then
            MASK1='coreos'
            DISTRONAME="CoreOS"
            DISTRO='coreos'
            MIN_MEMORY_SIZE=512
            RESIZE='1'
            MIGRATE='1'
            EXT4=1
            DISK_TARGET_DEVICE='---\nxen: xvd\nkvm: vd\n'
        else

# *** Other ***

            if echo $FILE|grep cdp >/dev/null 2>&1; then
                # CDP R1SOFT
                MASK1='cdp'
                DISTRONAME="CDP"
                RESIZE='1'
                MIGRATE='1'
                DISTRO='rhel'
            fi
        fi

# *** Big disks ***

        if echo $FILE|grep cPanel >/dev/null 2>&1 || echo $FILE|grep ISPmanager >/dev/null 2>&1; then
            DISKSIZE=9
        fi
        if echo $FILE|grep -i xelion >/dev/null 2>&1; then
            DISKSIZE=10
        fi
        if `echo $FILE|grep '.tar.gz' >/dev/null 2>&1`; then
            DATA=`echo $FILE|sed "s/^$MASK1-\(.*\)\.tar\.gz$/\1/"`
        elif `echo $FILE|grep '.ova' >/dev/null 2>&1`; then
            DATA=`echo $FILE|sed "s/^$MASK1-\(.*\)\.ova$/\1/"`
        fi
        DATA1=`echo $DATA|cut -d- -f1`
        DATA2=`echo $DATA|cut -d- -f2`
        DATA3=`echo $DATA|cut -d- -f3`
        DATA4=
        DATA4=`echo $DATA|cut -d- -f4 2>/dev/null`
        DATA5=
        DATA5=`echo $DATA|cut -d- -f5 2>/dev/null`

        DISTROVERSION=$DATA1
        if [ "x${DATA2}" = "xx64" -o "x${DATA2}" = "xx86" ]; then
            APPNAME=
            ARCH=$DATA2
            TMPLVERSION=$DATA3
            if [ "x${DATA4}" != "x" ]; then
               VIRTUALIZATION=`echo ${DATA4}| sed 's/\./,/g' 2>/dev/null`
               if `echo $ONAPPVERSION | grep '2\.' >/dev/null 2>&1` || `echo $ONAPPVERSION | grep '3\.0\.0' >/dev/null 2>&1` || `echo $ONAPPVERSION | grep '3\.0\.1' >/dev/null 2>&1`; then
                   if [ "x${DISTRO}" = "xfreebsd" ]; then
                       VIRTUALIZATION='xen,kvm'
                   fi
               fi
            elif [ $CHECK_VZ -eq 1 ]; then
               if ! tar ztf $FILE_PATH ./etc/fstab.kvm >/dev/null 2>&1; then
                   if [ "x${DISTRO}" != "xfreebsd" ]; then
                       VIRTUALIZATION='xen'
                   fi
               fi
            fi
        else
            APPNAME=$DATA2
            ARCH=$DATA3
            TMPLVERSION=$DATA4
            if [ "x${DATA5}" != "x" ]; then
               VIRTUALIZATION=`echo ${DATA5}| sed 's/\./,/g' 2>/dev/null`
               if `echo $ONAPPVERSION | grep '2\.' >/dev/null 2>&1` || `echo $ONAPPVERSION | grep '3\.0\.0' >/dev/null 2>&1` || `echo $ONAPPVERSION | grep '3\.0\.1' >/dev/null 2>&1`; then
                   if [ "x${DISTRO}" = "xfreebsd" ]; then
                       VIRTUALIZATION='xen,kvm'
                   fi
               fi
            elif [ $CHECK_VZ -eq 1 ]; then
               if ! tar ztf $FILE_PATH ./etc/fstab.kvm >/dev/null 2>&1; then
                   if [ "x${DISTRO}" != "xfreebsd" ]; then
                       VIRTUALIZATION='xen'
                   fi
               fi
            fi
        fi
        # Disable KVM+Virtio for Fedora 18 and OnApp < 3.0
        if [ "x${MASK1}" = "xfedora" ] && echo $ONAPPVERSION 2>/dev/null | egrep -e '^2\.' >/dev/null 2>&1 && echo $FILE | grep '\-18' >/dev/null 2>&1; then
            VIRTUALIZATION=`echo $VIRTUALIZATION 2>/dev/null | sed 's/,kvm_virtio//g'`
        fi
        if [ $VER_TO_LABEL -eq 1 ]; then
            LABEL="$DISTRONAME $DISTROVERSION $APPNAME $ARCH $TMPLVERSION"
        else
            LABEL="$DISTRONAME $DISTROVERSION $APPNAME $ARCH"
        fi

    fi

    ADD_SQL_COLUMNS=
    ADD_SQL_VALUES=

    case $DISTRO in
        freebsd)
            ONAPP_VERSION_MIN_REQ='2.2.0'
            ;;
        suse)
            ONAPP_VERSION_MIN_REQ='2.3.0'
            ;;
        archlinux)
            ONAPP_VERSION_MIN_REQ='2.3.0'
            ;;
        slackware)
            ONAPP_VERSION_MIN_REQ='3.0.0'
            ;;
    esac

    if [ "x${OS}" = "xwindows" ]; then
        case $DISTROVERSION in
            2003)
            ;;
            2008)
            ;;
            7)
            ;;
            XP)
            ;;
            2012)
                ONAPP_VERSION_MIN_REQ='3.0.0'
                case $DISTROTAIL_FULL in
                    R2) ONAPP_VERSION_MIN_REQ='3.1.2'
                    ;;
                esac

            ;;
            8)
                ONAPP_VERSION_MIN_REQ='3.0.0'
            ;;
            Vista)
            ;;
            "8.1")
                ONAPP_VERSION_MIN_REQ='3.1.2'
            ;;
            10)
                ONAPP_VERSION_MIN_REQ='5.2.0'
            ;;
            2016)
                ONAPP_VERSION_MIN_REQ='5.2.0'
            ;;
            2019)
                ONAPP_VERSION_MIN_REQ='6.0.0'
            ;;
            2022)
                ONAPP_VERSION_MIN_REQ='6.6.0'
            ;;
            *) continue
            ;;
        esac
        case $TMPLVERSION in
            2.1)
                ONAPP_VERSION_MIN_REQ='2.2.0'
                ;;
            3.?)
                ONAPP_VERSION_MIN_REQ='2.3.2'
                ;;
            4.?)
                if [ "x${DISTROVERSION}" != "x10" -a "x${DISTROVERSION}" != "x2016" -a "x${DISTROVERSION}" != "x2019" ]; then
                    ONAPP_VERSION_MIN_REQ='4.0.0'
                fi
                ;;
        esac
    fi
    STR=

    for NOT_RESIZEBLE in ${NOT_RESIZE}; do
        if echo $APPNAME 2>/dev/null | grep -i $NOT_RESIZEBLE >/dev/null 2>&1; then
            RESIZE='0'
        fi
    done

    if [ "x${VIRTUALIZATION}" = "xvmware" ]; then
        FILE_NAME=`echo $FILE | sed 's/\.ova$//g'`
    fi
    if [ "x${ONAPPVERSION}" = "x3.0.1" -o "x${ONAPPVERSION}" = "x3.1" -o "x${ONAPPVERSION}" = "x3.2" -o "x${ONAPPVERSION}" = "x3.3" -o "x${ONAPPVERSION}" = "x3.4" -o "x${ONAPPVERSION}" = "x3.5" -o "x${ONAPPVERSION}" = "x4.0" -o "x${ONAPPVERSION}" = "x4.1" -o "x${ONAPPVERSION}" = "x4.2" -o "x${ONAPPVERSION}" = "x4.3" -o "x${ONAPPVERSION}" = "x5.0" -o "x${ONAPPVERSION}" = "x5.1" -o "x${ONAPPVERSION}" = "x5.2" -o "x${ONAPPVERSION}" = "x5.3" -o "x${ONAPPVERSION}" = "x5.4" -o "x${ONAPPVERSION}" = "x5.5" -o "x${ONAPPVERSION}" = "x5.6" -o "x${ONAPPVERSION}" = "x5.7" -o "x${ONAPPVERSION}" = "x5.8" -o "x${ONAPPVERSION}" = "x5.9" -o "x${ONAPPVERSION}" = "x5.10" -o "x${ONAPPVERSION}" = "x6.0" -o "x${ONAPPVERSION}" = "x6.1" -o "x${ONAPPVERSION}" = "x6.2" -o "x${ONAPPVERSION}" = "x6.3" -o "x${ONAPPVERSION}" = "x6.4" -o "x${ONAPPVERSION}" = "x6.5" -o "x${ONAPPVERSION}" = "x6.6" -o "x${ONAPPVERSION}" = "x6.7" -o "x${ONAPPVERSION}" = "x6.8" ]; then
        SMART=0
        BAREMETAL=0
        if [ $CHECK_SMART -eq 1 ]; then
           # Any KVM or KVM+Virtio Linux or FreeBSD template is SmartServer compatible
           if echo $VIRTUALIZATION | grep 'kvm' > /dev/null 2>&1; then
               if [ "x${OS}" != "xwindows" ] && ! echo $NOT_SMART | grep "${MASK1}-${DISTROVERSION}-${ARCH}" >/dev/null 2>&1; then
                   SMART=1
               fi
               if [ "x${OS}" = "xwindows" -a "x${DISTROVERSION}" != "xXP" -a "x${DISTROVERSION}" != "xVista" -a "x${DISTROVERSION}" != "x7" ] && ! echo $NOT_SMART | grep "${DISTRO}-${DISTROVERSION}-${ARCH}" >/dev/null 2>&1; then
                   SMART=1
               fi
           fi
        fi
        if [ $CHECK_BAREMETAL -eq 1 ]; then
           if echo $VIRTUALIZATION | grep 'xen' > /dev/null 2>&1 && echo $VIRTUALIZATION | grep 'kvm' > /dev/null 2>&1 || echo $VIRTUALIZATION | grep -i 'baremetal' > /dev/null 2>&1; then
               # Any Xen (because of hypervisor_type >xen) and KVM Linux template is Baremetal compatible
               if [ "x${OS}" = "xlinux" -a "x${VIRTUALIZATION}" != "xkvm_virtio" ] && ! echo $NOT_BAREMETAL | grep "${MASK1}-${DISTROVERSION}-${ARCH}" >/dev/null 2>&1; then
                   BAREMETAL=1
                   if echo $FILE | grep -i 'Vyatta' >/dev/null 2>&1; then
                       BAREMETAL=0
                   fi
               elif [ "x${OS}" = "xwindows" ]; then
                   # Any Windows template with baremetal in the name (on the virtualization) is Baremetal compatible
                   if [ "x$VIRTUALIZATION" = "xbaremetal" ]; then
                       # HACK we should have xen and kvm in 'virtualization' to get the template available
                       VIRTUALIZATION='xen,kvm'
                       BAREMETAL=1
                   fi
               fi
           fi
        fi
    fi
    if [ "x$MODE" = "xSQL" ]; then
        if [ -e $FILE_PATH ]; then
            if [ $COMPUTE_MD5 -eq 1 ]; then
                MD5=`md5sum $FILE_PATH|sed 's/[[:space:]]\+.\+$//'`
                MD5="'$MD5'"
            else
                MD5='NULL'
            fi
            FILE_SIZE=`du -sk $FILE_PATH | sed 's/[[:space:]].\+.*$//g'`
            FILE_SIZE="'$FILE_SIZE'"
        else
            MD5='NULL'
            FILE_SIZE='NULL'
        fi
        if [ $CDN -eq 1 ]; then
            LABEL=$CDN_LABEL;
            MIN_MEMORY_SIZE=2048
            DISKSIZE=20
            SWAP=0
        fi
        if [ $LBVA -eq 1 ]; then
            LABEL=$LBVA_LABEL
            DISTRO='lbva'
            MIN_MEMORY_SIZE='512'
        fi
        if [ $ASVA -eq 1 ]; then
            LABEL=$ASVA_LABEL
            MIN_MEMORY_SIZE='512'
        fi
        if `echo $ONAPPVERSION | grep '2.1' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '2.2' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '2.3' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '3.0.0' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '3.0.1' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '3.0.2' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '3.0.3' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '3.0.4' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '3.0.5' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '3.0.6' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '3.0.7' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '3.0.8' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '3.1' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '3.2' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '3.3' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '3.4' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '3.5' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '4.0' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '4.1' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '4.2' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '4.3' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '5.0' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '5.1' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '5.2' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '5.3' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '5.4' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '5.5' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '5.6' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '5.7' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '5.8' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '5.9' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '5.10' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '6.0' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '6.1' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '6.2' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '6.3' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '6.4' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '6.5' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '6.6' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '6.7' >/dev/null 2>&1` || \
           `echo $ONAPPVERSION | grep '6.8' >/dev/null 2>&1`; then
            ADD_SQL_COLUMNS=', template_size, operating_system_arch, operating_system_edition, operating_system_tail'
            ADD_SQL_VALUES=", $FILE_SIZE, '$ARCH', $DISTROEDITION, $DISTROTAIL"
            UPDATE_SQL_STR=", template_size=$FILE_SIZE, operating_system_arch='$ARCH', operating_system_edition=$DISTROEDITION, operating_system_tail=$DISTROTAIL"
            if [ "x$OS" = "xlinux" ]; then
                # For Linux distros Hot Migrate must be disabled for all from LIVE_MIGRATE_DISABLE and CloudLinux (it is OpenVZ based)
                if [ "x$APPNAME" != "x" ] && `echo ${LIVE_MIGRATE_DISABLE} | grep -i "$APPNAME" >/dev/null` || `echo $FILE|grep cloudlinux >/dev/null 2>&1`; then
                    ADD_SQL_COLUMNS="$ADD_SQL_COLUMNS, allowed_hot_migrate"
                    ADD_SQL_VALUES="$ADD_SQL_VALUES, 0"
                    UPDATE_SQL_STR="$UPDATE_SQL_STR, allowed_hot_migrate=0"
                else
                    ADD_SQL_COLUMNS="$ADD_SQL_COLUMNS, allowed_hot_migrate"
                    ADD_SQL_VALUES="$ADD_SQL_VALUES, $MIGRATE"
                    UPDATE_SQL_STR="$UPDATE_SQL_STR, allowed_hot_migrate=$MIGRATE"
                fi
            fi
            if [ "x$OS" = "xwindows" ]; then
                # For Windows distros Hot Migrate is enabled for 2008 and 7
                #if [ "x$DISTROVERSION" = "x2008" -o "x$DISTROVERSION" = "x7" ]; then
                if [ "x$DISTROVERSION" = "x7" ]; then
                    ADD_SQL_COLUMNS="$ADD_SQL_COLUMNS, allowed_hot_migrate"
                    ADD_SQL_VALUES="$ADD_SQL_VALUES, 1"
                    UPDATE_SQL_STR="$UPDATE_SQL_STR, allowed_hot_migrate=1"
                fi
            fi
            ADD_SQL_COLUMNS="$ADD_SQL_COLUMNS, virtualization"
            ADD_SQL_VALUES="$ADD_SQL_VALUES, '$VIRTUALIZATION'"
            UPDATE_SQL_STR="$UPDATE_SQL_STR, virtualization='$VIRTUALIZATION'"
            if [ "x$ONAPPVERSION" = "x2.3" -o "x$ONAPPVERSION" = "x3.0.0" -o "x$ONAPPVERSION" = "x3.0.1" -o "x$ONAPPVERSION" = "x3.0.2" -o "x$ONAPPVERSION" = "x3.0.3" -o "x$ONAPPVERSION" = "x3.0.4" -o "x$ONAPPVERSION" = "x3.0.5" -o "x$ONAPPVERSION" = "x3.0.6" -o "x$ONAPPVERSION" = "x3.0.7" -o "x$ONAPPVERSION" = "x3.0.8" -o "x$ONAPPVERSION" = "x3.1" -o "x$ONAPPVERSION" = "x3.2"  -o "x$ONAPPVERSION" = "x3.3" -o "x$ONAPPVERSION" = "x3.4" -o "x$ONAPPVERSION" = "x3.5" -o "x$ONAPPVERSION" = "x4.0" -o "x$ONAPPVERSION" = "x4.1" -o "x$ONAPPVERSION" = "x4.2" -o "x$ONAPPVERSION" = "x4.3" -o "x$ONAPPVERSION" = "x5.0" -o "x$ONAPPVERSION" = "x5.1" -o "x$ONAPPVERSION" = "x5.2" -o "x$ONAPPVERSION" = "x5.3" -o "x$ONAPPVERSION" = "x5.4" -o "x$ONAPPVERSION" = "x5.5" -o "x$ONAPPVERSION" = "x5.6" -o "x$ONAPPVERSION" = "x5.7" -o "x$ONAPPVERSION" = "x5.8" -o "x$ONAPPVERSION" = "x5.9" -o "x$ONAPPVERSION" = "x5.10" -o "x$ONAPPVERSION" = "x6.0" -o "x$ONAPPVERSION" = "x6.1" -o "x$ONAPPVERSION" = "x6.2" -o "x$ONAPPVERSION" = "x6.3" -o "x$ONAPPVERSION" = "x6.4" -o "x$ONAPPVERSION" = "x6.5" -o "x$ONAPPVERSION" = "x6.6" -o "x$ONAPPVERSION" = "x6.7" -o "x$ONAPPVERSION" = "x6.8" ]; then
                ADD_SQL_COLUMNS="$ADD_SQL_COLUMNS, disk_target_device, min_memory_size"
                ADD_SQL_VALUES="$ADD_SQL_VALUES, \"$DISK_TARGET_DEVICE\", $MIN_MEMORY_SIZE"
                UPDATE_SQL_STR="$UPDATE_SQL_STR, disk_target_device=\"$DISK_TARGET_DEVICE\", min_memory_size=$MIN_MEMORY_SIZE"
            fi
            if [ "x$ONAPPVERSION" = "x3.0.0" -o "x$ONAPPVERSION" = "x3.0.2" -o "x$ONAPPVERSION" = "x3.0.3" -o "x$ONAPPVERSION" = "x3.0.4" -o "x$ONAPPVERSION" = "x3.0.5" -o "x$ONAPPVERSION" = "x3.0.6" -o "x$ONAPPVERSION" = "x3.0.7" -o "x$ONAPPVERSION" = "x3.0.8" -o "x$ONAPPVERSION" = "x3.1" -o "x$ONAPPVERSION" = "x3.2" -o "x$ONAPPVERSION" = "x3.3" -o "x$ONAPPVERSION" = "x3.4" -o "x$ONAPPVERSION" = "x3.5" -o "x$ONAPPVERSION" = "x4.0" -o "x$ONAPPVERSION" = "x4.1" -o "x$ONAPPVERSION" = "x4.2" -o "x$ONAPPVERSION" = "x4.3" -o "x$ONAPPVERSION" = "x5.0" -o "x$ONAPPVERSION" = "x5.1" -o "x$ONAPPVERSION" = "x5.2" -o "x$ONAPPVERSION" = "x5.3" -o "x$ONAPPVERSION" = "x5.4" -o "x$ONAPPVERSION" = "x5.5" -o "x$ONAPPVERSION" = "x5.6" -o "x$ONAPPVERSION" = "x5.7" -o "x$ONAPPVERSION" = "x5.8" -o "x$ONAPPVERSION" = "x5.9" -o "x$ONAPPVERSION" = "x5.10" -o "x$ONAPPVERSION" = "x6.0" -o "x$ONAPPVERSION" = "x6.1" -o "x$ONAPPVERSION" = "x6.2" -o "x$ONAPPVERSION" = "x6.3" -o "x$ONAPPVERSION" = "x6.4" -o "x$ONAPPVERSION" = "x6.5" -o "x$ONAPPVERSION" = "x6.6" -o "x$ONAPPVERSION" = "x6.7" -o "x$ONAPPVERSION" = "x6.8" ]; then
                ADD_SQL_COLUMNS="$ADD_SQL_COLUMNS, ext4"
                ADD_SQL_VALUES="$ADD_SQL_VALUES, $EXT4"
                UPDATE_SQL_STR="$UPDATE_SQL_STR, ext4=$EXT4"
            fi
            if [ "x$ONAPPVERSION" = "x3.0.1" -o "x$ONAPPVERSION" = "x3.1" -o "x$ONAPPVERSION" = "x3.2" -o "x$ONAPPVERSION" = "x3.3" -o "x$ONAPPVERSION" = "x3.4" -o "x$ONAPPVERSION" = "x3.5" -o "x$ONAPPVERSION" = "x4.0" -o "x$ONAPPVERSION" = "x4.1" -o "x$ONAPPVERSION" = "x4.2" -o "x$ONAPPVERSION" = "x4.3" -o "x$ONAPPVERSION" = "x5.0" -o "x$ONAPPVERSION" = "x5.1" -o "x$ONAPPVERSION" = "x5.2" -o "x$ONAPPVERSION" = "x5.3" -o "x$ONAPPVERSION" = "x5.4" -o "x$ONAPPVERSION" = "x5.5" -o "x$ONAPPVERSION" = "x5.6" -o "x$ONAPPVERSION" = "x5.7" -o "x$ONAPPVERSION" = "x5.8" -o "x$ONAPPVERSION" = "x5.9" -o "x$ONAPPVERSION" = "x5.10" -o "x$ONAPPVERSION" = "x6.0" -o "x$ONAPPVERSION" = "x6.1" -o "x$ONAPPVERSION" = "x6.2" -o "x$ONAPPVERSION" = "x6.3" -o "x$ONAPPVERSION" = "x6.4" -o "x$ONAPPVERSION" = "x6.5" -o "x$ONAPPVERSION" = "x6.6" -o "x$ONAPPVERSION" = "x6.7" -o "x$ONAPPVERSION" = "x6.8" ]; then
                ADD_SQL_COLUMNS="$ADD_SQL_COLUMNS, smart_server, baremetal_server"
                ADD_SQL_VALUES="$ADD_SQL_VALUES, $SMART, $BAREMETAL"
                UPDATE_SQL_STR="$UPDATE_SQL_STR, smart_server=$SMART, baremetal_server=$BAREMETAL"
            fi
        fi
        if [ "x$VIRTUALIZATION" = "xxen" -o "x$VIRTUALIZATION" = "xkvm" -o "x$VIRTUALIZATION" = "xkvm_virtio" -o "x$VIRTUALIZATION" = "xvmware" ]; then
            LABEL="$LABEL $VIRTUALIZATION"
        fi
        if [ "x${DISTRO}" = "xlbva" ]; then
            LABEL=$DISTRONAME
        fi
        if [ $CDN -eq 1 ]; then
            ADD_SQL_COLUMNS="$ADD_SQL_COLUMNS, cdn"
            ADD_SQL_VALUES="$ADD_SQL_VALUES, 1"
            UPDATE_SQL_STR="$UPDATE_SQL_STR, cdn=1"
        fi
        if [ $ASVA -eq 1 ]; then
            if [ "x$ONAPPVERSION" = "x4.1" -o "x$ONAPPVERSION" = "x4.2" -o "x$ONAPPVERSION" = "x4.3" -o "x$ONAPPVERSION" = "x5.0" -o "x$ONAPPVERSION" = "x5.1" -o "x$ONAPPVERSION" = "x5.2" -o "x$ONAPPVERSION" = "x5.3" -o "x$ONAPPVERSION" = "x5.4" -o "x$ONAPPVERSION" = "x5.5" -o "x$ONAPPVERSION" = "x5.6" -o "x$ONAPPVERSION" = "x5.7" -o "x$ONAPPVERSION" = "x5.8" -o "x$ONAPPVERSION" = "x5.9" -o "x$ONAPPVERSION" = "x5.10" -o "x$ONAPPVERSION" = "x6.0" -o "x$ONAPPVERSION" = "x6.1" -o "x$ONAPPVERSION" = "x6.2" -o "x$ONAPPVERSION" = "x6.3" -o "x$ONAPPVERSION" = "x6.4" -o "x$ONAPPVERSION" = "x6.5" -o "x$ONAPPVERSION" = "x6.6" -o "x$ONAPPVERSION" = "x6.7" -o "x$ONAPPVERSION" = "x6.8" ]; then
                ADD_SQL_COLUMNS="$ADD_SQL_COLUMNS, application_server, manager_id"
                ADD_SQL_VALUES="$ADD_SQL_VALUES, 1, 'applicationserver'"
                UPDATE_SQL_STR="$UPDATE_SQL_STR, application_server=1, manager_id='applicationserver'"
            fi
        fi
        if [ $CDN -eq 1 ]; then
            if [ "x$ONAPPVERSION" = "x4.1" -o "x$ONAPPVERSION" = "x4.2" -o "x$ONAPPVERSION" = "x4.3" -o "x$ONAPPVERSION" = "x5.0" -o "x$ONAPPVERSION" = "x5.1" -o "x$ONAPPVERSION" = "x5.2" -o "x$ONAPPVERSION" = "x5.3" -o "x$ONAPPVERSION" = "x5.4"  -o "x$ONAPPVERSION" = "x5.5" -o "x$ONAPPVERSION" = "x5.6" -o "x$ONAPPVERSION" = "x5.7" -o "x$ONAPPVERSION" = "x5.8" -o "x$ONAPPVERSION" = "x5.9" -o "x$ONAPPVERSION" = "x5.10" -o "x$ONAPPVERSION" = "x6.0" -o "x$ONAPPVERSION" = "x6.1" -o "x$ONAPPVERSION" = "x6.2" -o "x$ONAPPVERSION" = "x6.3" -o "x$ONAPPVERSION" = "x6.4" -o "x$ONAPPVERSION" = "x6.5" -o "x$ONAPPVERSION" = "x6.6" -o "x$ONAPPVERSION" = "x6.7" -o "x$ONAPPVERSION" = "x6.8" ]; then
                ADD_SQL_COLUMNS="$ADD_SQL_COLUMNS, manager_id"
                ADD_SQL_VALUES="$ADD_SQL_VALUES, 'cdn'"
                UPDATE_SQL_STR="$UPDATE_SQL_STR, manager_id='cdn'"
            fi
        fi
        if [ $LBVA -eq 1 ]; then
            if [ "x$ONAPPVERSION" = "x4.1" -o "x$ONAPPVERSION" = "x4.2" -o "x$ONAPPVERSION" = "x4.3" -o "x$ONAPPVERSION" = "x5.0" -o "x$ONAPPVERSION" = "x5.1" -o "x$ONAPPVERSION" = "x5.2" -o "x$ONAPPVERSION" = "x5.3" -o "x$ONAPPVERSION" = "x5.4" -o "x$ONAPPVERSION" = "x5.5" -o "x$ONAPPVERSION" = "x5.6" -o "x$ONAPPVERSION" = "x5.7" -o "x$ONAPPVERSION" = "x5.8" -o "x$ONAPPVERSION" = "x5.9" -o "x$ONAPPVERSION" = "x5.10" -o "x$ONAPPVERSION" = "x6.0" -o "x$ONAPPVERSION" = "x6.1" -o "x$ONAPPVERSION" = "x6.2" -o "x$ONAPPVERSION" = "x6.3" -o "x$ONAPPVERSION" = "x6.4" -o "x$ONAPPVERSION" = "x6.5" -o "x$ONAPPVERSION" = "x6.6" -o "x$ONAPPVERSION" = "x6.7" -o "x$ONAPPVERSION" = "x6.8" ]; then
                ADD_SQL_COLUMNS="$ADD_SQL_COLUMNS, manager_id"
                ADD_SQL_VALUES="$ADD_SQL_VALUES, 'lbva'"
                UPDATE_SQL_STR="$UPDATE_SQL_STR, manager_id='lbva'"
            fi
        fi

        if [ $SQL_UPDATE -eq 0 ]; then
            STR="INSERT INTO \`${TABLENAME}\` (label, created_at, updated_at, version, file_name, operating_system, operating_system_distro, allowed_swap, state, checksum, allow_resize_without_reboot, min_disk_size${ADD_SQL_COLUMNS}) VALUES ('$LABEL', NOW(), NOW(), '$TMPLVERSION', '$FILE_NAME', '$OS', '$DISTRO', $SWAP, 'active', $MD5, $RESIZE, $DISKSIZE${ADD_SQL_VALUES});"
        else
            #FILE_NAME_OLD=`echo $FILE_NAME | sed "s/$TMPLVERSION/$TMPLVERSION_OLD/g"`
            STR="UPDATE \`${TABLENAME}\` SET label='$LABEL', updated_at=NOW(), version='$TMPLVERSION', file_name='$FILE_NAME', operating_system='$OS', operating_system_distro='$DISTRO', allowed_swap=$SWAP, state='active', checksum=$MD5, allow_resize_without_reboot=$RESIZE, min_disk_size=$DISKSIZE${UPDATE_SQL_STR} WHERE $WHERE_STATEMENT;"
        fi
    elif [ "x$MODE" = "xCSV" ]; then
        STR="$LABEL;$FULLNAME;$DISTRONAME"
    elif [ "x$MODE" = "xTXT" ]; then
        for PARAM in $PARAMS; do
            eval PARAM_VAL=\$$PARAM
            #if [ "x${PARAM_VAL}" = "x0" -o "x${PARAM_VAL}" = "x1" ]; then
                if [ "x${PARAM_VAL}" = "x0" ]; then
                    PARAM_VAL="No "
                fi
                if [ "x${PARAM_VAL}" = "x1" ]; then
                    PARAM_VAL="Yes"
                fi
            #fi
            PARAM_VAL=`echo $PARAM_VAL|sed 's/NULL//g'`
            PARAM_VAL=`echo $PARAM_VAL|sed "s/'//g"`
            STR="${STR}${DELIMITER}${PARAM_VAL}"
        done
    fi
    if [ ! "x$OUT_FILE" = "x" ]; then
        echo $STR >> $OUT_FILE
    else
        echo $STR
    fi
done

cd - >/dev/null

if [ $QUIET -eq 0 ]; then
    echo
    echo "-- DONE --"
fi

exit 0
