#! /bin/bash

#--------------
#User Defined / Settable (public)
#--------------
_t=${0%.*}
install_prefix=${install_prefix:-${ESGF_INSTALL_PREFIX:-"/usr/local"}}
esg_root_dir=${esg_root_dir:-${ESGF_HOME:-"/esg"}}
esg_config_dir=${esg_root_dir}/config
esg_config_type_file=${esg_config_dir}/config_type
esgf_secret_file=${esg_config_dir}/.esgf_pass
pg_secret_file=${esg_config_dir}/.esg_pg_pass
pub_secret_file=${esg_config_dir}/.esg_pg_publisher_pass
ks_secret_file=${esg_config_dir}/.esg_keystore_pass
install_manifest=${install_manifest:-"${esg_root_dir}/esgf-install-manifest"}
install_logfile=${install_manifest}
logfile=${logfile:-"/tmp/${_t##*/}.out"}
#--------------


init() {
    #--------------------------------
    # Internal esgf node code versions
    #--------------------------------

    apache_frontend_version=${apache_frontend_version:-"1.0.8"}
    apache_frontend_tag=${apache_frontend_tag:-"v1.08"}

    cdat_version=${cdat_version:-"2.2.0"}
#    cdat_tag="1.5.1.esgf-v1.7.0"


    esgcet_version=${esgcet_version:-"3.2.6"}
    publisher_tag=${publisher_tag:-"v3.2.6"}
    esgprep_version="2.7.10"
    cmor_version="3.2.5"  # concidence its same as esgcet


    #see esgf-node-manager project:
    esgf_node_manager_version=${esgf_node_manager_version:-"1.0.0"}
    esgf_node_manager_db_version=${esgf_node_manager_db_version:-"0.1.5"}

    #see esgf-security project:
    esgf_security_version=${esgf_security_version:-"2.7.15"}
    esgf_security_db_version=${esgf_security_db_version:-"0.1.5"}

    #see esg-orp project:
    esg_orp_version=${esg_orp_version:-"2.9.6"}

    #see esgf-idp project:
    esgf_idp_version=${esgf_idp_version:-"2.7.11"}

    #see esg-search project:
    esg_search_version=${esg_search_version:-"4.13.4"}

    #see esgf-web-fe project:

    #see esgf-dashboard project:
    esgf_dashboard_version=${esgf_dashboard_version:-"1.5.3"}
    esgf_dashboard_db_version=${esgf_dashboard_db_version:-"0.0.2"}

    #see esgf-desktop project:
    esgf_desktop_version=${esgf_desktop_version:-"0.0.22"}

    esgf_stats_api_version=${esgf_stats_api_version:-"1.0.1"}

    #--------------------------------
    # External programs' versions
    #--------------------------------
    openssl_version=${openssl_version:="0.9.8r"} ; openssl_min_version=${openssl_min_version:="0.9.8e"} ; openssl_max_version=${openssl_max_version:="0.9.9z"}
    java_version=${java_version:-"1.8.0_131"} ; java_min_version=${java_min_version:-"1.8.0_131"}
    ant_version=${ant_version:-"1.9.1"} ; ant_min_version=${ant_min_version:-"1.9.1"}
    postgress_version=${postgress_version:-"8.4.20"} ; postgress_min_version=${postgress_min_version:-"8.4.20"}
    tomcat_version=${tomcat_version:-"8.5.9"} ; tomcat_min_version=${tomcat_min_version:-"8.5.9"}
    #cmake_version=${cmake_version:="2.8.12.2"} ; cmake_min_version=${cmake_min_version:="2.8.10.2"} ; cmake_max_version=${cmake_max_version:="2.8.12.2"}
    tds_version=${tds_version:-"5.0.2"} ; tds_min_version=${tds_min_version:-"5.0.2"}

    # Since ESGF 1.8, LAS version is declared in esg-product-server
    #las_version=${las_version:-"8.1"}; las_min_version=${las_min_version:-"8.1"}

    python_version=${python_version:-"2.7"}
    #--------------------------------
    # Script vars (~external)
    #--------------------------------
    openssl_install_dir=${OPENSSL_HOME:-${install_prefix}/openssl}
    postgress_install_dir=${PGHOME:-/var/lib/pgsql}
    postgress_bin_dir=${PGBINDIR:-/usr/bin}
    postgress_lib_dir=${PGLIBDIR:-/usr/lib64/pgsql}
    postgress_user=${PGUSER:-dbsuper}

    local pg_secret=$(cat ${pg_secret_file} 2> /dev/null)
    pg_sys_acct_passwd=${pg_sys_acct_passwd:=${pg_secret:=changeme}}
    unset pg_secret #only here to hold the tertiary ephemeral value from file, unset asap
    local pub_secret=$(cat ${pub_secret_file} 2> /dev/null)
    publisher_db_user_passwd=${publisher_db_user_passwd:-${pub_secret}}
    unset pub_secret #only here to hold the tertiary ephemeral value from file, unset asap
    postgress_host=${PGHOST:-localhost}
    postgress_port=${PGPORT:-5432}
    #cmake_install_dir=${CMAKE_HOME:-${install_prefix}/cmake}
    cdat_home=${CDAT_HOME:-${install_prefix}/conda}
    java_opts=${JAVA_OPTS:-""}
    java_install_dir=${JAVA_HOME:-${install_prefix}/java}
    ant_install_dir=${ANT_HOME:-${install_prefix}/ant}
    tomcat_install_dir=${CATALINA_HOME:-${install_prefix}/tomcat}
    tomcat_conf_dir=${esg_config_dir}/tomcat
    tomcat_opts=${CATALINA_OPTS}
    tomcat_user=${tomcat_user:-tomcat}
    tomcat_group=${tomcat_group:-$tomcat_user}
    globus_location=${GLOBUS_LOCATION:-${install_prefix}/globus}
    mail_smtp_host=${mail_smtp_host:-smtp.`hostname --domain`} #standard guess.
    mail_admin_address=${mail_admin_address}

    if [ -n "${ESGINI}" ]; then
        publisher_home=${ESGINI%/*}
        publisher_config=${ESGINI##*/}
    fi

    ############################################
    ####  DO NOT EDIT BELOW THIS POINT!!!!! ####
    ############################################
    export GIT_SSL_NO_VERIFY=1
    export OPENSSL_HOME=${openssl_install_dir}
    export PGHOME=${postgress_install_dir}
    export PGBINDIR=${postgress_bin_dir}
    export PGLIBDIR=${postgress_lib_dir}
    export PGUSER=${postgress_user}
    export PGHOST=${postgress_host}
    export PGPORT=${postgress_port}
    #export CMAKE_HOME=${cmake_install_dir}
    export CDAT_HOME=${cdat_home}
    export JAVA_HOME=${java_install_dir}
    export JAVA_OPTS=${java_opts}
    export ANT_HOME=${ant_install_dir}
    export CATALINA_HOME=${tomcat_install_dir}
    export CATALINA_BASE=${CATALINA_HOME}
    export CATALINA_OPTS=${tomcat_opts}
    export GLOBUS_LOCATION=${globus_location}

    myPATH=$OPENSSL_HOME/bin:$CMAKE_HOME/bin:$JAVA_HOME/bin:$ANT_HOME/bin:$CDAT_HOME/bin:$CDAT_HOME/Externals/bin:$CATALINA_HOME/bin:$GLOBUS_LOCATION/bin:${install_prefix}/bin:/bin:/sbin:/usr/bin:/usr/sbin
    myLD_LIBRARY_PATH=$OPENSSL_HOME/lib:$CDAT_HOME/Externals/lib:$GLOBUS_LOCATION/lib:${install_prefix}/geoip/lib:/usr/lib64:/usr/lib
    export PATH=$(_path_unique $myPATH:$PATH)
    export LD_LIBRARY_PATH=$(_path_unique $myLD_LIBRARY_PATH:$LD_LIBRARY_PATH)
    export CFLAGS="-I${OPENSSL_HOME}/include -I/usr/include ${CFLAGS} -fPIC"
    export LDFLAGS="-L${OPENSSL_HOME}/lib -L/usr/lib64 -L/usr/lib -Wl,--rpath,${OPENSSL_HOME}/lib"

    #--------------
    # ID Setting
    #--------------
    #fix: id will always return the root id no matter what flags we use if we start this via sudo
    installer_user=${ESG_USER:-${SUDO_USER:-$(echo $HOME | sed 's#.*/\([^/]\+\)/\?$#\1#')}}
    installer_uid=${ESG_USER_UID:-${SUDO_UID:-$(id -u $installer_user)}}
    installer_gid=${ESG_USER_GID:-${SUDO_GID:-$(id -g $installer_user)}}
    installer_home=${ESG_USER_HOME:-/usr/local/src/esgf}

    #deprecate SUDO_?ID so we only use one variable for all this
    [[ $SUDO_UID ]] && ESG_USER_UID=${SUDO_UID} && unset SUDO_UID
    [[ $SUDO_GID ]] && ESG_USER_GID=${SUDO_GID} && unset SUDO_GID

    verbose_print "${installer_user}:${installer_uid}:${installer_gid}:${installer_home}"

    #--------------
    # Script vars (internal)
    #--------------
    esg_backup_dir=${esg_backup_dir:-"${esg_root_dir}/backups"}
    esg_config_dir=${esg_config_dir:-"${esg_root_dir}/config"}
    esg_log_dir=${esg_log_dir:-"${esg_root_dir}/log"}
    esg_tools_dir=${esg_tools_dir:-"${esg_root_dir}/tools"}
    esg_etc_dir=${esg_etc_dir:-"${esg_root_dir}/etc"}
    workdir=${workdir:-${ESGF_INSTALL_WORKDIR:-${installer_home}/workbench/esg}}

    word_size=${word_size:-$(file /bin/bash | perl -ple 's/^.*ELF\s*(32|64)-bit.*$/$1/g')}
    let num_cpus=1+$(cat /proc/cpuinfo | sed -n 's/^processor[ \t]*:[ \t]*\(.*\)$/\1/p' | tail -1)
    date_format="+%Y_%m_%d_%H%M%S"
    num_backups_to_keep=${num_backups_to_keep:-7}
    compress_extensions=".tar.gz|.tar.bz2|.tgz|.bz2|.tar"
    certificate_extensions="pem|crt|cert|key"

    openssl_dist_url=http://www.openssl.org/source/openssl-${openssl_version}.tar.gz
    java_dist_url=${esg_dist_url_root}/java/${java_version}/jdk${java_version}-${word_size}.tar.gz
    ant_dist_url=http://archive.apache.org/dist/ant/binaries/apache-ant-${ant_version}-bin.tar.gz
    openssl_workdir=${workdir}/openssl
    esgf_dashboard_ip_workdir=${workdir}/esgf-dashboard-ip
    bash_completion_url=${esg_dist_url}/thirdparty/bash-completion-20060301-1.noarch.rpm
    db_database=${ESGF_DB_NAME:-${db_database:-"esgcet"}}
    node_db_name=${db_database}
    postgress_jar=postgresql-8.4-703.jdbc3.jar
    postgress_driver=org.postgresql.Driver
    postgress_protocol=jdbc:postgresql:
    pg_sys_acct=${pg_sys_acct:-postgres}
    pg_sys_acct_group=${pg_sys_acct_group:-$pg_sys_acct}
    #cmake_workdir=${workdir}/cmake
    #cmake_repo=http://www.cmake.org/cmake.git
    #cdat_repo=git://github.com/UV-CDAT/uvcdat.git
    #cdat_repo_https=https://github.com/UV-CDAT/uvcdat.git
    publisher_repo=git://github.com/ESGF/esg-publisher.git
    apache_frontend_repo=https://github.com/ESGF/apache-frontend.git
    publisher_repo_https=https://github.com/ESGF/esg-publisher.git
    esgcet_egg_file=esgcet-${esgcet_version}-py${python_version}.egg
    esg_testdir=${workdir}/../esg_test
    tomcat_dist_url=http://archive.apache.org/dist/tomcat/tomcat-${tomcat_version%%.*}/v${tomcat_version}/bin/apache-tomcat-${tomcat_version}.tar.gz
    tomcat_pid_file=/var/run/tomcat-jsvc.pid
    utils_url=${esg_dist_url}/utils
    thredds_dist_url=ftp://ftp.unidata.ucar.edu/pub/thredds/${tds_version%.*}/${tds_version}/thredds.war
    thredds_esg_dist_url=${esg_dist_url}/thredds/${tds_version%.*}/${tds_version}/thredds.war
    thredds_content_dir=${thredds_content_dir:-${esg_root_dir}/content}
    #NOTE: This root dir should match a root set in the thredds setup
    thredds_root_dir=${esg_root_dir}/data
    thredds_replica_dir=${thredds_root_dir}/replica
    esgf_stats_api_dist_url=${esg_dist_url}/esgf-stats-api/esgf-stats-api.war
    #NOTE: This is another RedHat/CentOS specific portion!!! it will break on another OS!
    show_summary_latch=0
    source_latch=0
    scripts_dir=${install_prefix}/bin
    esg_installarg_file=${scripts_dir}/esg-installarg
    no_globus=${no_globus:-0}
    force_install=${force_install:-0}
    extkeytool_download_url=${esg_dist_url}/etc/idptools.tar.gz
    tomcat_users_file=${tomcat_conf_dir}/tomcat-users.xml
    keystore_file=${tomcat_conf_dir}/keystore-tomcat
    keystore_alias=${keystore_alias:-my_esgf_node}
    keystore_password=${keystore_password}
    truststore_file=${tomcat_conf_dir}/esg-truststore.ts
    truststore_password=${truststore_password:-changeit}
    globus_global_certs_dir=/etc/grid-security/certificates
    #NOTE: java keystore style DN...
    default_dname="OU=ESGF.ORG, O=ESGF" #zoiks: allow this to be empty to allow prompting of user for fields!
    config_file=${esg_config_dir}/esgf.properties
    index_config="master slave"
}

init
