<commit_before>#!/bin/bash
#===============================================================================
#          FILE:  lib.sh
#         USAGE:  ./helper.sh -h
#   DESCRIPTION:  SugarCRM bash helper library for Linux
#       AUTHORS:  <NAME>;
#       VERSION:  0.9.0
#===============================================================================

error() 
{
    echo -e "\033[1;31m${1}\033[0m" 1>&2
    draw _ ${#1} red
    exit 1
}

checkWhich()
{
    which ${1} > /dev/null 2>&1
    return $?
}

helperAlias()
{
    local aliasHelper="alias helper='bash ${DIR}/helper.sh'"
    echo "You may add the Sugar Bash Helper as alias using:"
    echo ${aliasHelper}
}

parse_yaml()
{
   local prefix=$2
   local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
   sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
        -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p"  $1 |
   awk -F${fs} '{
      indent = length($1)/2;
      vname[indent] = $2;
      for (i in vname) {if (i > indent) {delete vname[i]}}
      if (length($3) > 0) {
         vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
         printf("%s%s%s=\"%s\"\n", "'${prefix}'",vn, $2, $3);
      }
   }'
}

setYamlVal()
{
	ymlVal=${!1}

	if [ ! -z "$2" ]; then
	    eval ${2}=${!1}
	fi
}

renderArray()
{
    local count=0
	setYamlVal "_$1_${count}_name"

	while (( ${#ymlVal} > 0 ))
	do
	    if [[ ( 10 -gt ${count} ) ]]; then
            echo " ${count} - ${ymlVal}"
	    else
	        echo ${count} - ${ymlVal}
        fi
	    count=$(( $count + 1 ))
	    setYamlVal "_$1_${count}_name"
	done

	draw - '' 'menu'
}

#   FUNCTION:   secho       Echo the message into specific color
# PARAMETERS:   $1          The message to display
#               $2          The color of the message
secho()
{
    local code=0

    case ${2} in
        red)
			code=31
        ;;
        green)
			code=32
        ;;
        yellow|menu)
			code=33
        ;;
        blue)
			code=34
        ;;
        *)
			code=${2}
        ;;
    esac

    echo -e "\e[${code}m${1}\e[0m"
}

#   FUNCTION:   draw        Echo Specific line
# PARAMETERS:   $1          Type of the line
#               $2          Lenght of the line
#               $3          Color of the line
draw()
{
    local line
    local lineLenght=${titleLenght}

    if [ ! -z ${2} ]; then
        lineLenght=${2}
    fi

    for i in `seq 1 ${lineLenght}`;
    do
        line="${line}${1}"
    done
    secho "${line}" ${3}
}

drawOptionDone()
{
    draw - "" 'menu'
}

showOptions()
{
    setYamlVal "_${1/_opt/_name}"
    if [[ -z ${ymlVal} ]]; then
        ymlVal=${1}
    fi

    local title="SugarBash Helper: ${ymlVal}"
    echo ""
    secho "${title}" 'menu'
    titleLenght=${#title}
    draw "=" ${titleLenght} 'menu'
    renderArray $1
}

menu()
{
    while ((OPT != 0));
        showOptions $1
        if [ ! -z $2 ] && [ "${OPT}" == "" ]; then
            OPT=${2}
        else
            read -p "Select your menu option: " OPT
            draw _ "" 'menu'
        fi
    do
        setYamlVal "_${1}_${OPT}_func"
        ${ymlVal}
    done
}

vpn() 
{
    secho "# VPN: $@" 'menu'

    case ${1} in

        'kill')
            sudo killall vpnc
            drawOptionDone
        ;;

        'connectSugar')
            setYamlVal "_vpn_conf"
            sudo vpnc ${ymlVal}
            drawOptionDone
        ;;

        *)
			break
        ;;

    esac
}

gitConfig() 
{
    secho "# gitConfig: $@" 'menu'

    case ${1} in

        'global')
			secho "Before config:" 'menu'
            git config --global -l
            git config --global user.name "${_git_user_name}"
            git config --global user.email "${_git_user_email}"
            secho "After config:" 'menu'
            git config --global -l
            drawOptionDone
        ;;

        *)
			break
        ;;

    esac
}

gitMango()
{
    secho "# gitMango: $@" 'menu'

    case ${1} in

        'clone')
            setYamlVal "_git_clone_mango_origin"
            secho "Will clone ${ymlVal} into ${PWD}" 'menu'
            read -p "Give Mango prefix: " OPT
            if [ -z ${OPT} ]; then
                error 'Proceeding abort!'
            fi

            git clone ${ymlVal} "${OPT}-mango"
            cd ${OPT}-mango
            git status
            git remote -v
            setYamlVal "_git_clone_mango_upstream"
            git remote add upstream ${ymlVal}
            git remote -v
            gitMango postCheckout

        ;;

        'postCheckout')
            git fetch upstream
            git submodule update --init
            git submodule update
            secho "Submodules:" 'menu'
            git submodule
            composer install -d=sugarcrm
		;;

        'patch')
            xbuild setParameters

            secho "# Patch Info #" 'menu'
            echo "Based on staged files of:       ${rootPath}/${repoName}"
            echo "Mango repo applying on:         ${repoPath}/sugarcrm"

            askToProceed "applying patch"

            secho "Creating patch based on staged files from: ${rootPath}/${repoName}" 'menu'
            cd ${rootPath}/${repoName}
            git diff --staged > /tmp/${repoName}.patch
            secho "Apply patch into Mango repo: ${repoPath}/sugarcrm" 'menu'
            cd ${repoPath}/sugarcrm
            patch -p1 < /tmp/${repoName}.patch
            git status
		;;

        *)
			break
        ;;

    esac

    drawOptionDone
}

mountFstab()
{
    secho "# mountFstab: $@" 'menu'

	local count=0
	setYamlVal "_mount_fstab_${count}"

	while (( ${#ymlVal} > 0 ))
	do
	    sudo mount ${ymlVal}
	    count=$(( $count + 1 ))
	    setYamlVal "_mount_fstab_${count}"
	done

    drawOptionDone
}

backup()
{
    setYamlVal "_backup_path" "PATH"
	setYamlVal "_backup_serverip" "SERVER_IP"
	setYamlVal "_backup_mountpoint" "MOUNTPOINT"
	# comenzi 
	setYamlVal "_backup_command_mount" "MOUNT"
	setYamlVal "_backup_command_umount" "UMOUNT"
	setYamlVal "_backup_command_rsync" "RSYNC"
	setYamlVal "_backup_command_domain" "DOMAIN"
	setYamlVal "_backup_command_username" "USERNAME"
	# ----------------------------------------------

	if [ ! "$SUDO_UID" ]; then
		error "Use sudo bash $0 $@"
	fi

	# ----    aici trebuia sa verific daca exista comenzile
	checkWhich ${MOUNT} && checkWhich ${RSYNC}
	if [  "$?" -ne "0"  ]; then
		unixInstall $MOUNT
		unixInstall $RSYNC
	fi

	checkWhich ${MOUNT} && checkWhich ${RSYNC}
	if [  "$?" -ne "0"  ]; then
		error "Can't find $MOUNT or $RSYNC";
	fi

	if [ ! -d ${MOUNTPOINT} ]; then
		mkdir -p ${MOUNTPOINT} || ( echo "Unable to create mount point. Exiting..." exit 1; )
	fi

	${MOUNT} '//'${SERVER_IP}'/'${USERNAME} ${MOUNTPOINT} -o username=${USERNAME},domain=${DOMAIN},uid=${SUDO_UID}
	if [  "$?" -eq "0"  ]; then
	    echo "Remote folder mounted in $MOUNTPOINT";
	else 
	    error "Error mounting. Exiting" ;
	fi       
	                  
	${RSYNC}
	if [  "$?" -eq "0"  ]; then
		echo "Sync OK";
	else 
		error "Error sync folders" ;
	fi       
	${UMOUNT} ${MOUNTPOINT} || ${UMOUNT} -l ${MOUNTPOINT} || echo "Umounting failed. Please run  $UMOUNT $MOUNTPOINT"

    drawOptionDone
}

askToProceed()
{
    echo -e "\033[93m"; read -e -p "Proceed $1 ? (y/n): " -i "y" OPT ; echo -e "\033[0m"
    if [ ${OPT} != "y" ]; then
        error 'Proceeding abort!'
    fi
}

xbuild()
{
    secho "# xbuild: $@" 'menu'

    case ${1} in

        'setParameters')
            renderArray "xbuild_repo"
            read -p "Choose repo: " RID

            setYamlVal "_xbuild_repo_${RID}_name" "repoName"
            setYamlVal "_xbuild_repo_${RID}_path" "repoPath"
            setYamlVal "_xbuild_rootPath" "rootPath"
            setYamlVal "_xbuild_repo_${RID}_url" "buildUrl"
            setYamlVal "_xbuild_repo_${RID}_name" "db"
            setYamlVal "_xbuild_repo_${RID}_db_user" "dbUser"
            setYamlVal "_xbuild_repo_${RID}_db_password" "<PASSWORD>"
            setYamlVal "_xbuild_repo_${RID}_db_host" "dbHost"
            setYamlVal "_xbuild_repo_${RID}_db_type" "dbType"
            setYamlVal "_xbuild_repo_${RID}_version" "builVersion"
            setYamlVal "_xbuild_repo_${RID}_flav" "buildFlav"
            setYamlVal "_xbuild_repo_${RID}_license" "license"
            if [ -z ${builVersion} ]; then
                setYamlVal "_xbuild_version" "builVersion"
            fi
            if [ -z ${buildFlav} ]; then
                setYamlVal "_xbuild_flav" "buildFlav"
            fi
            if [ -z ${license} ]; then
                setYamlVal "_xbuild_license" "license"
            fi
            if [ -z ${dbUser} ]; then
                setYamlVal "_xbuild_db_user" "dbUser"
            fi
            if [ -z ${dbPass} ]; then
                setYamlVal "_xbuild_db_password" "dbPass"
            fi
            if [ -z ${dbHost} ]; then
                setYamlVal "_xbuild_db_host" "dbHost"
            fi
            if [ -z ${dbType} ]; then
                setYamlVal "_xbuild_db_type" "dbType"
            fi
            db=${db//[^[:alnum:]]/}
        ;;

        'prepare')
            secho "# Build Info #" 'menu'
            echo "build name:          ${repoName}"
            echo "source path:         ${repoPath}"
            echo "destination:         ${rootPath}/${repoName}"
            echo "db name:             ${db}"
            echo "db user:             ${dbUser}"
            echo "db password:         ${<PASSWORD>}"
            echo "db host:             ${dbHost}"
            echo "db type:             ${dbType}"
            echo "url:                 ${buildUrl}"
            echo "flav:                ${buildFlav}"
            echo "version              ${builVersion}"
            echo "license:             ${license}"
            draw - 60

            if [ ! -d ${repoPath} ]; then
                error "Source repo path ${repoPath} aka 'Mango' folder desn't exist! Check config.yml"
            fi

            if [ -d "${rootPath}/${repoName}" ]; then
                secho "Cleaning ${rootPath}/${repoName}"
                cd ${rootPath}/${repoName}
                askToProceed "to remove folder content"
                proceedXBuild=1
                rm -Rf ./*
                rm -Rf .htaccess .git .gitignore
            else
                mkdir ${rootPath}/${repoName}
            fi

            if [ -d "/tmp/sugarbuild$repoName" ]; then
                rm -Rf /tmp/sugarbuild${repoName}
            fi

            cd ${repoPath}
            gitMango postCheckout
        ;;

        'building')
            cd ${repoPath}/build/rome

            php -n build.php \
            --ver=${builVersion} \
            --flav=${buildFlav} \
            --dir=sugarcrm \
            --build_dir=/tmp/sugarbuild_${repoName}

            mv /tmp/sugarbuild_${repoName}/${buildFlav}/sugarcrm/* ${rootPath}/${repoName}
        ;;

        'configBuild')
            if [[  "${dbType}" == "mysql"  ]]; then
                echo "drop database if exists ${db};" | mysql -u ${dbUser} -p${dbPass}
            fi
            cd ${rootPath}/${repoName}
            cat > config_si.php <<EOL
<?php

\$sugar_config_si = array (
    'setup_site_admin_user_name'=>'admin',
    'setup_site_admin_password' => '<PASSWORD>',

    'setup_db_host_name' => '${dbHost}',
    'setup_db_database_name' => '${db}',
    'setup_db_drop_tables' => 'true',
    'setup_db_create_database' => 'true',
    'setup_db_admin_user_name' => '${dbUser}',
    'setup_db_admin_password' => <PASSWORD>}',
    'setup_db_type' => '${dbType}',

    'setup_license_key' => '${license}',
    'setup_system_name' => 'SugarCRM',
    'setup_site_url' => 'http://${buildUrl}',

    'default_currency_iso4217' => 'USD',
    'default_currency_name' => 'US Dollar',
    'default_currency_significant_digits' => '2',
    'default_currency_symbol' => '$',
    'default_date_format' => 'Y-m-d',
    'default_time_format' => 'H:i',
    'default_decimal_seperator' => '.',
    'default_language' => 'en_us',
    'default_locale_name_format' => 's f l',
    'default_number_grouping_seperator' => ',',
    'export_delimiter' => ',',

    'setup_fts_type' => 'Elastic',
    'setup_fts_host' => 'localhost',
    'setup_fts_port' => '9200',
    #'setup_fts_hide_config' => 'true',

EOL
            if [[  "${dbType}" == "oci8"  ]]; then
                secho "Oracle Db config" 'menu'

                cat >> config_si.php <<EOL
# Config Oracle DB
    'setup_db_create_sugarsales_user' => 'false',
    'setup_license_key_users' => '100',
    'setup_license_key_expire_date' => '2016-10-01',
    'setup_num_lic_oc' => '10',
    'dbUSRData' => 'same',
    'install_type' => 'typical',
    'demoData' => 'yes',
    'setup_db_pop_demo_data' => '0',
    'setup_site_sugarbeet_anonymous_stats' => 'true',
    'setup_site_sugarbeet_automatic_checks' => '1',
    'setup_db_database_name' => '0.0.0.0/orcl',
#    'web_user' => 'vagrant',
#    'web_group' => 'apache',
    'setup_fts_skip' => 'true',
EOL
           fi
           cat >> config_si.php <<EOL
);
EOL

            cat > .gitignore <<EOL
# OS and IDE
*~
.DS_Store
.idea/
.project
.settings

# SugarCRM
*.log
config_override.php

cache/
upload/

include/javascript/yui3/
include/javascript/yui/
include/javascript/tiny_mce/
<commit_msg>GitIgnore: - custom/blowfish/;<commit_after>#!/bin/bash
#===============================================================================
#          FILE:  lib.sh
#         USAGE:  ./helper.sh -h
#   DESCRIPTION:  SugarCRM bash helper library for Linux
#       AUTHORS:  <NAME>;
#       VERSION:  0.9.0
#===============================================================================

error() 
{
    echo -e "\033[1;31m${1}\033[0m" 1>&2
    draw _ ${#1} red
    exit 1
}

checkWhich()
{
    which ${1} > /dev/null 2>&1
    return $?
}

helperAlias()
{
    local aliasHelper="alias helper='bash ${DIR}/helper.sh'"
    echo "You may add the Sugar Bash Helper as alias using:"
    echo ${aliasHelper}
}

parse_yaml()
{
   local prefix=$2
   local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
   sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
        -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p"  $1 |
   awk -F${fs} '{
      indent = length($1)/2;
      vname[indent] = $2;
      for (i in vname) {if (i > indent) {delete vname[i]}}
      if (length($3) > 0) {
         vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
         printf("%s%s%s=\"%s\"\n", "'${prefix}'",vn, $2, $3);
      }
   }'
}

setYamlVal()
{
	ymlVal=${!1}

	if [ ! -z "$2" ]; then
	    eval ${2}=${!1}
	fi
}

renderArray()
{
    local count=0
	setYamlVal "_$1_${count}_name"

	while (( ${#ymlVal} > 0 ))
	do
	    if [[ ( 10 -gt ${count} ) ]]; then
            echo " ${count} - ${ymlVal}"
	    else
	        echo ${count} - ${ymlVal}
        fi
	    count=$(( $count + 1 ))
	    setYamlVal "_$1_${count}_name"
	done

	draw - '' 'menu'
}

#   FUNCTION:   secho       Echo the message into specific color
# PARAMETERS:   $1          The message to display
#               $2          The color of the message
secho()
{
    local code=0

    case ${2} in
        red)
			code=31
        ;;
        green)
			code=32
        ;;
        yellow|menu)
			code=33
        ;;
        blue)
			code=34
        ;;
        *)
			code=${2}
        ;;
    esac

    echo -e "\e[${code}m${1}\e[0m"
}

#   FUNCTION:   draw        Echo Specific line
# PARAMETERS:   $1          Type of the line
#               $2          Lenght of the line
#               $3          Color of the line
draw()
{
    local line
    local lineLenght=${titleLenght}

    if [ ! -z ${2} ]; then
        lineLenght=${2}
    fi

    for i in `seq 1 ${lineLenght}`;
    do
        line="${line}${1}"
    done
    secho "${line}" ${3}
}

drawOptionDone()
{
    draw - "" 'menu'
}

showOptions()
{
    setYamlVal "_${1/_opt/_name}"
    if [[ -z ${ymlVal} ]]; then
        ymlVal=${1}
    fi

    local title="SugarBash Helper: ${ymlVal}"
    echo ""
    secho "${title}" 'menu'
    titleLenght=${#title}
    draw "=" ${titleLenght} 'menu'
    renderArray $1
}

menu()
{
    while ((OPT != 0));
        showOptions $1
        if [ ! -z $2 ] && [ "${OPT}" == "" ]; then
            OPT=${2}
        else
            read -p "Select your menu option: " OPT
            draw _ "" 'menu'
        fi
    do
        setYamlVal "_${1}_${OPT}_func"
        ${ymlVal}
    done
}

vpn() 
{
    secho "# VPN: $@" 'menu'

    case ${1} in

        'kill')
            sudo killall vpnc
            drawOptionDone
        ;;

        'connectSugar')
            setYamlVal "_vpn_conf"
            sudo vpnc ${ymlVal}
            drawOptionDone
        ;;

        *)
			break
        ;;

    esac
}

gitConfig() 
{
    secho "# gitConfig: $@" 'menu'

    case ${1} in

        'global')
			secho "Before config:" 'menu'
            git config --global -l
            git config --global user.name "${_git_user_name}"
            git config --global user.email "${_git_user_email}"
            secho "After config:" 'menu'
            git config --global -l
            drawOptionDone
        ;;

        *)
			break
        ;;

    esac
}

gitMango()
{
    secho "# gitMango: $@" 'menu'

    case ${1} in

        'clone')
            setYamlVal "_git_clone_mango_origin"
            secho "Will clone ${ymlVal} into ${PWD}" 'menu'
            read -p "Give Mango prefix: " OPT
            if [ -z ${OPT} ]; then
                error 'Proceeding abort!'
            fi

            git clone ${ymlVal} "${OPT}-mango"
            cd ${OPT}-mango
            git status
            git remote -v
            setYamlVal "_git_clone_mango_upstream"
            git remote add upstream ${ymlVal}
            git remote -v
            gitMango postCheckout

        ;;

        'postCheckout')
            git fetch upstream
            git submodule update --init
            git submodule update
            secho "Submodules:" 'menu'
            git submodule
            composer install -d=sugarcrm
		;;

        'patch')
            xbuild setParameters

            secho "# Patch Info #" 'menu'
            echo "Based on staged files of:       ${rootPath}/${repoName}"
            echo "Mango repo applying on:         ${repoPath}/sugarcrm"

            askToProceed "applying patch"

            secho "Creating patch based on staged files from: ${rootPath}/${repoName}" 'menu'
            cd ${rootPath}/${repoName}
            git diff --staged > /tmp/${repoName}.patch
            secho "Apply patch into Mango repo: ${repoPath}/sugarcrm" 'menu'
            cd ${repoPath}/sugarcrm
            patch -p1 < /tmp/${repoName}.patch
            git status
		;;

        *)
			break
        ;;

    esac

    drawOptionDone
}

mountFstab()
{
    secho "# mountFstab: $@" 'menu'

	local count=0
	setYamlVal "_mount_fstab_${count}"

	while (( ${#ymlVal} > 0 ))
	do
	    sudo mount ${ymlVal}
	    count=$(( $count + 1 ))
	    setYamlVal "_mount_fstab_${count}"
	done

    drawOptionDone
}

backup()
{
    setYamlVal "_backup_path" "PATH"
	setYamlVal "_backup_serverip" "SERVER_IP"
	setYamlVal "_backup_mountpoint" "MOUNTPOINT"
	# comenzi 
	setYamlVal "_backup_command_mount" "MOUNT"
	setYamlVal "_backup_command_umount" "UMOUNT"
	setYamlVal "_backup_command_rsync" "RSYNC"
	setYamlVal "_backup_command_domain" "DOMAIN"
	setYamlVal "_backup_command_username" "USERNAME"
	# ----------------------------------------------

	if [ ! "$SUDO_UID" ]; then
		error "Use sudo bash $0 $@"
	fi

	# ----    aici trebuia sa verific daca exista comenzile
	checkWhich ${MOUNT} && checkWhich ${RSYNC}
	if [  "$?" -ne "0"  ]; then
		unixInstall $MOUNT
		unixInstall $RSYNC
	fi

	checkWhich ${MOUNT} && checkWhich ${RSYNC}
	if [  "$?" -ne "0"  ]; then
		error "Can't find $MOUNT or $RSYNC";
	fi

	if [ ! -d ${MOUNTPOINT} ]; then
		mkdir -p ${MOUNTPOINT} || ( echo "Unable to create mount point. Exiting..." exit 1; )
	fi

	${MOUNT} '//'${SERVER_IP}'/'${USERNAME} ${MOUNTPOINT} -o username=${USERNAME},domain=${DOMAIN},uid=${SUDO_UID}
	if [  "$?" -eq "0"  ]; then
	    echo "Remote folder mounted in $MOUNTPOINT";
	else 
	    error "Error mounting. Exiting" ;
	fi       
	                  
	${RSYNC}
	if [  "$?" -eq "0"  ]; then
		echo "Sync OK";
	else 
		error "Error sync folders" ;
	fi       
	${UMOUNT} ${MOUNTPOINT} || ${UMOUNT} -l ${MOUNTPOINT} || echo "Umounting failed. Please run  $UMOUNT $MOUNTPOINT"

    drawOptionDone
}

askToProceed()
{
    echo -e "\033[93m"; read -e -p "Proceed $1 ? (y/n): " -i "y" OPT ; echo -e "\033[0m"
    if [ ${OPT} != "y" ]; then
        error 'Proceeding abort!'
    fi
}

xbuild()
{
    secho "# xbuild: $@" 'menu'

    case ${1} in

        'setParameters')
            renderArray "xbuild_repo"
            read -p "Choose repo: " RID

            setYamlVal "_xbuild_repo_${RID}_name" "repoName"
            setYamlVal "_xbuild_repo_${RID}_path" "repoPath"
            setYamlVal "_xbuild_rootPath" "rootPath"
            setYamlVal "_xbuild_repo_${RID}_url" "buildUrl"
            setYamlVal "_xbuild_repo_${RID}_name" "db"
            setYamlVal "_xbuild_repo_${RID}_db_user" "dbUser"
            setYamlVal "_xbuild_repo_${RID}_db_password" "dbPass"
            setYamlVal "_xbuild_repo_${RID}_db_host" "dbHost"
            setYamlVal "_xbuild_repo_${RID}_db_type" "dbType"
            setYamlVal "_xbuild_repo_${RID}_version" "builVersion"
            setYamlVal "_xbuild_repo_${RID}_flav" "buildFlav"
            setYamlVal "_xbuild_repo_${RID}_license" "license"
            if [ -z ${builVersion} ]; then
                setYamlVal "_xbuild_version" "builVersion"
            fi
            if [ -z ${buildFlav} ]; then
                setYamlVal "_xbuild_flav" "buildFlav"
            fi
            if [ -z ${license} ]; then
                setYamlVal "_xbuild_license" "license"
            fi
            if [ -z ${dbUser} ]; then
                setYamlVal "_xbuild_db_user" "dbUser"
            fi
            if [ -z ${dbPass} ]; then
                setYamlVal "_xbuild_db_password" "dbPass"
            fi
            if [ -z ${dbHost} ]; then
                setYamlVal "_xbuild_db_host" "dbHost"
            fi
            if [ -z ${dbType} ]; then
                setYamlVal "_xbuild_db_type" "dbType"
            fi
            db=${db//[^[:alnum:]]/}
        ;;

        'prepare')
            secho "# Build Info #" 'menu'
            echo "build name:          ${repoName}"
            echo "source path:         ${repoPath}"
            echo "destination:         ${rootPath}/${repoName}"
            echo "db name:             ${db}"
            echo "db user:             ${dbUser}"
            echo "db password:         <PASSWORD>}"
            echo "db host:             ${dbHost}"
            echo "db type:             ${dbType}"
            echo "url:                 ${buildUrl}"
            echo "flav:                ${buildFlav}"
            echo "version              ${builVersion}"
            echo "license:             ${license}"
            draw - 60

            if [ ! -d ${repoPath} ]; then
                error "Source repo path ${repoPath} aka 'Mango' folder desn't exist! Check config.yml"
            fi

            if [ -d "${rootPath}/${repoName}" ]; then
                secho "Cleaning ${rootPath}/${repoName}"
                cd ${rootPath}/${repoName}
                askToProceed "to remove folder content"
                proceedXBuild=1
                rm -Rf ./*
                rm -Rf .htaccess .git .gitignore
            else
                mkdir ${rootPath}/${repoName}
            fi

            if [ -d "/tmp/sugarbuild$repoName" ]; then
                rm -Rf /tmp/sugarbuild${repoName}
            fi

            cd ${repoPath}
            gitMango postCheckout
        ;;

        'building')
            cd ${repoPath}/build/rome

            php -n build.php \
            --ver=${builVersion} \
            --flav=${buildFlav} \
            --dir=sugarcrm \
            --build_dir=/tmp/sugarbuild_${repoName}

            mv /tmp/sugarbuild_${repoName}/${buildFlav}/sugarcrm/* ${rootPath}/${repoName}
        ;;

        'configBuild')
            if [[  "${dbType}" == "mysql"  ]]; then
                echo "drop database if exists ${db};" | mysql -u ${dbUser} -p${dbPass}
            fi
            cd ${rootPath}/${repoName}
            cat > config_si.php <<EOL
<?php

\$sugar_config_si = array (
    'setup_site_admin_user_name'=>'admin',
    'setup_site_admin_password' => '<PASSWORD>',

    'setup_db_host_name' => '${dbHost}',
    'setup_db_database_name' => '${db}',
    'setup_db_drop_tables' => 'true',
    'setup_db_create_database' => 'true',
    'setup_db_admin_user_name' => '${dbUser}',
    'setup_db_admin_password' => '${<PASSWORD>}',
    'setup_db_type' => '${dbType}',

    'setup_license_key' => '${license}',
    'setup_system_name' => 'SugarCRM',
    'setup_site_url' => 'http://${buildUrl}',

    'default_currency_iso4217' => 'USD',
    'default_currency_name' => 'US Dollar',
    'default_currency_significant_digits' => '2',
    'default_currency_symbol' => '$',
    'default_date_format' => 'Y-m-d',
    'default_time_format' => 'H:i',
    'default_decimal_seperator' => '.',
    'default_language' => 'en_us',
    'default_locale_name_format' => 's f l',
    'default_number_grouping_seperator' => ',',
    'export_delimiter' => ',',

    'setup_fts_type' => 'Elastic',
    'setup_fts_host' => 'localhost',
    'setup_fts_port' => '9200',
    #'setup_fts_hide_config' => 'true',

EOL
            if [[  "${dbType}" == "oci8"  ]]; then
                secho "Oracle Db config" 'menu'

                cat >> config_si.php <<EOL
# Config Oracle DB
    'setup_db_create_sugarsales_user' => 'false',
    'setup_license_key_users' => '100',
    'setup_license_key_expire_date' => '2016-10-01',
    'setup_num_lic_oc' => '10',
    'dbUSRData' => 'same',
    'install_type' => 'typical',
    'demoData' => 'yes',
    'setup_db_pop_demo_data' => '0',
    'setup_site_sugarbeet_anonymous_stats' => 'true',
    'setup_site_sugarbeet_automatic_checks' => '1',
    'setup_db_database_name' => '0.0.0.0/orcl',
#    'web_user' => 'vagrant',
#    'web_group' => 'apache',
    'setup_fts_skip' => 'true',
EOL
           fi
           cat >> config_si.php <<EOL
);
EOL

            cat > .gitignore <<EOL
# OS and IDE
*~
.DS_Store
.idea/
.project
.settings

# SugarCRM
*.log
config_override.php

cache/
upload/

include/javascript/yui3/
include/javascript/yui/
include/javascript/tiny_mce/

custom/blowfish/
{'fraction_non_alphanumeric': 0.16756491915727584, 'fraction_numerical': 0.01062827422455056, 'mean_word_length': 1.8750677213132516, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 122, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5018794', 'n_tokens_mistral': 9462, 'n_tokens_neox': 9015, 'n_words': 2020}