#!/bin/bash

#####
# ESGF Data Node Manager Service
# description: Central manager for all ESGF Node services and components
#
#****************************************************************************
#*                                                                          *
#*  Organization: Lawrence Livermore National Lab (LLNL)                    *
#*   Directorate: Computation                                               *
#*    Department: Computing Applications and Research                       *
#*      Division: S&T Global Security                                       *
#*        Matrix: Atmospheric, Earth and Energy Division                    *
#*       Program: PCMDI                                                     *
#*       Project: Earth Systems Grid Fed (ESGF) Node Software Stack         *
#*  First Author: Gavin M. Bell (gavin@llnl.gov)                            *
#*                                                                          *
#****************************************************************************
#*                                                                          *
#*   Copyright (c) 2009, Lawrence Livermore National Security, LLC.         *
#*   Produced at the Lawrence Livermore National Laboratory                 *
#*   Written by: Gavin M. Bell (gavin@llnl.gov)                             *
#*   LLNL-CODE-420962                                                       *
#*                                                                          *
#*   All rights reserved. This file is part of the:                         *
#*   Earth System Grid Fed (ESGF) Node Software Stack, Version 1.0          *
#*                                                                          *
#*   For details, see http://esgf.org/                                      *
#*   Please also read this link                                             *
#*    http://esgf.org/LICENSE                                               *
#*                                                                          *
#*   * Redistribution and use in source and binary forms, with or           *
#*   without modification, are permitted provided that the following        *
#*   conditions are met:                                                    *
#*                                                                          *
#*   * Redistributions of source code must retain the above copyright       *
#*   notice, this list of conditions and the disclaimer below.              *
#*                                                                          *
#*   * Redistributions in binary form must reproduce the above copyright    *
#*   notice, this list of conditions and the disclaimer (as noted below)    *
#*   in the documentation and/or other materials provided with the          *
#*   distribution.                                                          *
#*                                                                          *
#*   Neither the name of the LLNS/LLNL nor the names of its contributors    *
#*   may be used to endorse or promote products derived from this           *
#*   software without specific prior written permission.                    *
#*                                                                          *
#*   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS    *
#*   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      *
#*   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS      *
#*   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LAWRENCE    *
#*   LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR     *
#*   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,           *
#*   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT       *
#*   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF       *
#*   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND    *
#*   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,     *
#*   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT     *
#*   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF     *
#*   SUCH DAMAGE.                                                           *
#*                                                                          *
#****************************************************************************
#####

#uses: perl, awk, ifconfig, tar, wget, curl, su, useradd, groupadd,
#      id, chmod, chown, chgrp, cut, svn, mkdir, killall, java, egrep,
#      lsof, unlink, ln, pax, keytool, openssl

#note: usage of readlink not macosx friendly :-( usage of useradd /
#      groupadd is RedHat/CentOS dependent :-(

DEBUG=${DEBUG:-0}
VERBOSE=${VERBOSE:-0}
envfile="/etc/esg.env"

#--------------
#User Defined / Settable (public)
#--------------
install_prefix=${install_prefix:-"/usr/local"}
cdat_home=${cdat_home:-${install_prefix}/cdat}
esg_root_dir=${esg_root_dir:-"/esg"}
workdir=${workdir:-~/workbench/esg}
install_manifest=${install_manifest:-"${esg_root_dir}/esgf-install-manifest"}
esg_functions_file=${install_prefix}/bin/esg-functions
#--------------

# Sourcing esg-installarg esg-functions file and esg-init file
[ -e ${esg_functions_file} ] && source ${esg_functions_file} && ((VERBOSE)) && printf "sourcing from: ${esg_functions_file} \n"

date_format=${date_format:-"+%Y_%m_%d_%H%M%S"}
compress_extensions=${compress_extensions:-".tar.gz|.tar.bz2|.tgz|.bz2"}
force_install=${force_install:-0}

tomcat_user=${tomcat_user:-tomcat}
tomcat_group=${tomcat_group:-$tomcat_user}
tomcat_install_dir=${CATALINA_HOME:-${install_prefix}/tomcat}
python_version=${python_version:-"2.6"}
config_file=${esg_root_dir}/config/esgf.properties




init() {
    #[ -n "${envfile}" ] && [ -e "${envfile}" ] && source ${envfile} && ((VERBOSE)) && printf "node manager: sourcing environment from: ${envfile} \n"

    esgf_node_manager_version=${esgf_node_manager_version:-"0.5.1"}
    esgf_node_manager_db_version=${esgf_node_manager_db_version:-"0.1.5"}
    esgf_node_manager_egg_file=esgf_node_manager-${esgf_node_manager_db_version}-py${python_version}.egg

    node_dist_url=${esg_dist_url}/esgf-node-manager/esgf-node-manager-${esgf_node_manager_version}.tar.gz
    node_manager_app_context_root=esgf-node-manager

    get_property node_use_ssl && [ -z "${node_use_ssl}" ] && write_as_property node_use_ssl true

    get_property node_manager_service_app_home ${tomcat_install_dir}/webapps/${node_manager_app_context_root}
    write_as_property node_manager_service_app_home
    write_as_property node_manager_service_endpoint "https://${esgf_host}/esgf-nm/"

    get_property node_use_ips && [ -z "${node_use_ips}" ] && write_as_property node_use_ips true
    get_property node_poke_timeout && [ -z "${node_poke_timeout}" ] && write_as_property node_poke_timeout 6000

    #Database information....
    node_db_name=${node_db_name:-"esgcet"}
    node_db_node_manager_schema_name="esgf_node_manager"

    postgress_driver=${postgress_driver:-org.postgresql.Driver}
    postgress_protocol=${postgress_protocol:-jdbc:postgresql:}
    postgress_host=${PGHOST:-localhost}
    postgress_port=${PGPORT:-5432}
    postgress_user=${PGUSER:-dbsuper}
    pg_sys_acct_passwd=${pg_sys_acct_passwd:=${pg_secret:=changeme}}

    #Notification component information...
    mail_smtp_host=${mail_smtp_host:-smtp.`hostname --domain`} #standard guess.
    #Overwrite mail_smtp_host value if already defined in props file
    get_property mail_smtp_host ${mail_smtp_host}
    mail_admin_address=${mail_admin_address}

    #Launcher script for the esgf-sh
    esgf_shell_launcher="esgf-sh"

}


#####
# Install The Node Manager
#####
# - Takes boolean arg: 0 = setup / install mode (default)
#                      1 = updated mode
#
# In setup mode it is an idempotent install (default)
# In update mode it will always pull down latest after archiving old
#
setup_node_manager() {
    echo -n "Checking for node manager ${esgf_node_manager_version}"
    check_webapp_version "esgf-node-manager" ${esgf_node_manager_version}
    local ret=$?
    ((ret == 0)) && (( ! force_install )) && echo " [OK]" && return 0

    init

    echo
    echo "*******************************"
    echo "Setting up The ESGF Node Manager..."
    echo "*******************************"
    echo

    local upgrade=${1:-0}
    local default="Y"
    ((force_install)) && default="N"
    local dosetup
    if [ -d ${node_manager_service_app_home} ]; then
        db_set=1
        echo "Detected an existing node manager installation..."
        read -p "Do you want to continue with node manager installation and setup? $([ "$default" = "N" ] && echo "[y/N]" || echo "[Y/n]") " dosetup
        [ -z "${dosetup}" ] && dosetup=${default}
        if [ "${dosetup}" != "Y" ] && [ "${dosetup}" != "y" ]; then
            echo "Skipping node manager installation and setup - will assume it's setup properly"
            #resetting node manager version to what it is already, not what we prescribed in the script
            #this way downstream processes will use the *actual* version in play, namely the (access logging) filter(s)
            esgf_node_manager_version=$(get_current_webapp_version esgf_node_manager)
            return 0
        fi

        local dobackup="Y"
        read -p "Do you want to make a back up of the existing distribution [${node_manager_app_context_root}]?? [Y/n] " dobackup
        [ -z "${dobackup}" ] && dobackup=${default}
        if [ "${dobackup}" = "Y" ] || [ "${dobackup}" = "y" ]; then
            echo "Creating a backup archive of this web application [$node_manager_service_app_home]"
            backup ${node_manager_service_app_home}
        fi

        echo

        dobackup="Y"
        read -p "Do you want to make a back up of the existing database [${node_db_name}:esgf_node_manager]?? [Y/n] " dobackup
        [ -z "${dobackup}" ] && dobackup=${default}
        if [ "${dobackup}" = "Y" ] || [ "${dobackup}" = "y" ]; then
            echo "Creating a backup archive of the manager database schema [${node_db_name}:esgf_node_manager]"
            backup_db -db ${node_db_name} -s node_manager
        fi
        unset dobackup
        echo
    fi


    mkdir -p ${workdir}
    [ $? != 0 ] && return 1
    pushd ${workdir} >& /dev/null
    local fetch_file


    local node_dist_file=${node_dist_url##*/}
    #strip off .tar.gz at the end
    #(Ex: esgf-node-manager-0.9.0.tar.gz -> esgf-node-manager-0.9.0)
    node_dist_dir=$(echo ${node_dist_file} | awk 'gsub(/('$compress_extensions')/,"")')

    checked_get ${node_dist_file} ${node_dist_url} $((force_install))
    (( $? > 1 )) && echo " ERROR: Could not download ${node_dist_url} :-(" && popd && checked_done 1

    #make room for new install
    if (( force_install )) ; then
        echo "Removing Previous Installation of the ESGF Node Manager... (${node_dist_dir})" && \
            (rm -rf ${node_dist_dir} && echo "[OK]" || (echo "[FAILED]" && checked_done 1)) && \
            clean_node_manager_webapp_subsystem
    fi

    echo "unpacking ${node_dist_file}..."
    tar xzf ${node_dist_file}
    [ $? != 0 ] && echo " ERROR: Could not extract the ESG Node: ${node_dist_file}" && popd && checked_done 1

    pushd ${node_dist_dir} >& /dev/null

    stop_tomcat

    #strip the version number off(#.#.#) the dir and append .war to get the name of war file
    #(Ex: esgf-node-manager-0.9.0 -> esgf-node-manager.war)
    local trimmed_name=$(pwd)/${node_dist_dir%-*}
    node_war_file=${trimmed_name}.war
    echo "node_war_file = "${node_war_file}

    #----------------------------
    #make room for new INSTALL
    ((upgrade == 0)) && set_aside_web_app ${node_manager_service_app_home}
    #----------------------------
    mkdir -p ${node_manager_service_app_home}
    cd ${node_manager_service_app_home}


    #----------------------------
    fetch_file=esgf-node-manager.properties

    #NOTE: The saving of the last config file must be done *BEFORE* we untar the new distro!
    if ((upgrade)) && [ -e WEB-INF/classes/${fetch_file} ]; then
        cp WEB-INF/classes/${fetch_file} WEB-INF/classes/${fetch_file}.saved
        chmod 600 WEB-INF/classes/${fetch_file}*
    fi

    echo "Expanding war ${node_war_file} in $(pwd)"
    $JAVA_HOME/bin/jar xf ${node_war_file}
    local _ret=$?
    ((upgrade == 0)) && set_aside_web_app_cleanup ${node_manager_service_app_home} ${_ret}
    unset _ret

    #----------------------------
    #Property file fetching and token replacement...
    #----------------------------
    pushd WEB-INF/classes >& /dev/null
    cat ${fetch_file}.tmpl >> ${config_file}

    chown -R ${tomcat_user} ${node_manager_service_app_home}
    chgrp -R ${tomcat_group} ${node_manager_service_app_home}
    unset fetch_file
    popd >& /dev/null
    #----------------------------

    popd >& /dev/null

    #NOTE TODO: Create a function that reads the property file and for
    #every property that is not assigned and/or in a list of manidtory
    #properties go through and ask the user to assign a value. -gavin

    if [ -z "${mail_admin_address}" ]; then
        while [ 1 ]; do
            local input
            read -p "What email address should notifications be sent as? " input
            [ -n "${input}" ] && mail_admin_address=${input}  && unset input && break
        done
    fi


    (write_node_manager_config && configure_postgress) || checked_done 1



    touch_generated_whitelist_files
    write_node_manager_install_log
    write_shell_contrib_command_file


    fetch_shell_launcher

#    setup_conda_env
    setup_py_pkgs
    
    setup_nm_repo

    checked_done 0

}


touch_generated_whitelist_files() {
    debug_print "touch_generated_whitelist_files()... "
    touch ${esg_config_dir}/esgf_ats.xml; chown ${tomcat_user}:${tomcat_group} ${esg_config_dir}/esgf_ats.xml && chmod 644 ${esg_config_dir}/esgf_ats.xml
    touch ${esg_config_dir}/esgf_azs.xml; chown ${tomcat_user}:${tomcat_group} ${esg_config_dir}/esgf_azs.xml && chmod 644 ${esg_config_dir}/esgf_azs.xml
    touch ${esg_config_dir}/esgf_idp.xml; chown ${tomcat_user}:${tomcat_group} ${esg_config_dir}/esgf_idp.xml && chmod 644 ${esg_config_dir}/esgf_idp.xml
    touch ${esg_config_dir}/esgf_shards.xml; chown ${tomcat_user}:${tomcat_group} ${esg_config_dir}/esgf_shards.xml && chmod 644 ${esg_config_dir}/esgf_shards.xml
    if [ -d "${esg_root_dir}/content/las/conf/server" ]; then
        touch ${esg_root_dir}/content/las/conf/server/las_servers.xml
        chown ${tomcat_user}:${tomcat_group} ${esg_root_dir}/content/las/conf/server/las_servers.xml && chmod 644 ${esg_root_dir}/content/las/conf/server/las_servers.xml
    fi
}

fetch_shell_launcher() {
    pushd ${scripts_dir} >& /dev/null
    checked_get ${esgf_shell_launcher} ${node_dist_url%/*}/${esgf_shell_launcher} $((force_install))
    (( $? > 1 )) && echo " ERROR: Could not download ${node_dist_url%/*}/${esgf_shell_launcher} :-(" && popd >& /dev/null && return 1
    chmod 755 ${esgf_shell_launcher}
    popd >& /dev/null
    return 0
}

fetch_p2p_spotchecker() {
    pushd ${scripts_dir} >& /dev/null
    local script_name=${1:-"esgf-spotcheck"}
    checked_get ${script_name} ${node_dist_url%/*}/${script_name} $((force_install))
    (( $? > 1 )) && echo " ERROR: Could not download ${node_dist_url%/*}/${script_name} :-(" && popd >& /dev/null && return 1
    chmod 755 ${script_name}
    popd >& /dev/null
    return 0
}


write_node_manager_config() {
    #----------------------------
    ((DEBUG || VERBOSE)) && echo "Writing down database connection info and other node-wide properties"
    #----------------------------
    mkdir -p ${esg_root_dir}/config
    pushd ${esg_root_dir}/config >& /dev/null
    [ $? != 0 ] && return 1

    cat >> ${config_file} <<EOF
db.driver=${postgress_driver}
db.protocol=${postgress_protocol}
db.host=${postgress_host}
db.port=${postgress_port}
db.database=${node_db_name}
db.user=${postgress_user}
mail.smtp.host=${mail_smtp_host}
mail.admin.address=${mail_admin_address}
EOF
    [ $? != 0 ] && popd >& /dev/null && return 1

    dedup_properties ${config_file}
    chown ${tomcat_user}:${tomcat_group} ${config_file}
    chmod 600 ${config_file}
    popd >& /dev/null
    #----------------------------
    return 0
}

write_node_manager_install_log() {
    echo "$(date ${date_format}) webapp:esgf-node-manager=${esgf_node_manager_version} ${node_manager_service_app_home}" >> ${install_manifest}
    dedup ${install_manifest}
    return 0
}

write_shell_contrib_command_file() {
    local contrib_file=${esg_root_dir}/config/esgf_contrib_commands
    [ -e "${contrib_commands}" ] && return 0

    #----------------------------
    mkdir -p ${esg_root_dir}/config
    pushd ${esg_root_dir}/config >& /dev/null
    [ $? != 0 ] && return 1

    cat > ${contrib_file} <<EOF
#This file contains the mappings of contributed shell commands that
#are to be loaded into the esgf-sh shell
#Entries are of the form:

#[implmentation_language]
#command1 -> resource1
#command2 = resource2

#Depending on the resource value specifies the resource to load given
#the implementation language specified.  For example for commands
#implemented in Java the resource is the fully qualified class name of
#the implementing class.

[java]
test -> esg.common.shell.cmds.ESGFtest

EOF
    [ $? != 0 ] && popd >& /dev/null && return 1

    chmod 644 ${config_file}
    popd >& /dev/null
    #----------------------------
    return 0
}

#--------------------------------------------------
#NOTE: This must be run AFTER the esg node web app
#      installation/configuration (setup_node_manager)
#--------------------------------------------------
configure_postgress() {
    init

    echo
    echo "*******************************"
    echo "Configuring Postgres... for ESGF Node Manager"
    echo "*******************************"
    echo

    start_postgress

    if [ -z "$(postgres_list_dbs ${node_db_name})" ] ; then
        postgres_create_db ${node_db_name} || return 0
    else
        if [ -n "$(postgres_list_db_schemas ${node_db_node_manager_schema_name})" ]; then
            echo "Detected an existing node manager schema installation..."
        else
            postgres_clean_schema_migration "ESGF Node Manager"
        fi
    fi

    echo

    pushd ${workdir}/${node_dist_dir:-esgf-node-manager-${esgf_node_manager_version}}/db >& /dev/null
    [ $? != 0 ] && echo " ERROR: Could not find node distribution dir ${workdir}/${node_dist_dir}" && checked_done 1

    #------------------------------------------------------------------------
    #Based on the node type selection we build the appropriate database tables
    #------------------------------------------------------------------------

    #download the egg file from the distribution server is necessary....
    checked_get ${esgf_node_manager_egg_file} ${esg_dist_url}/esgf-node-manager/${esgf_node_manager_egg_file} $((force_install))
    (( $? > 1 )) && return 0

    #install the egg....
    ((DEBUG)) && echo "easy_install ${esgf_node_manager_egg_file}"


    source ${cdat_home}/bin/activate esgf-pub

    easy_install ${esgf_node_manager_egg_file}
    [ $? != 0 ] && echo "ERROR: Could not create esgf node manager python module" && checked_done 1

    if [ -n "$(postgres_list_db_schemas ${node_db_node_manager_schema_name})" ]; then
        local default="N"
        ((force_install)) && default="Y"
        local dobackup
        read -p "Do you want to make a back up of the existing database schema [${node_db_name}:${node_db_node_manager_schema_name}]? $([ "$default" = "N" ] && echo "[y/N]" || echo "[Y/n]") " dobackup
        [ -z "${dobackup}" ] && dobackup=${default}
        if [ "${dobackup}" = "Y" ] || [ "${dobackup}" = "y" ]; then
            echo "Creating a backup archive of the database schema [$node_db_name:${node_db_node_manager_schema_name}]"
            backup_db -db ${node_db_name} -s ${node_db_node_manager_schema_name}
        fi
        unset dobackup
        unset default
        echo
    fi

    #run the code to build the database and install sql migration...
    ((DEBUG)) && echo "$cdat_home/bin/esgf_node_manager_initialize --dburl ${postgress_user}:${pg_sys_acct_passwd}@${postgress_host}:${postgress_port}/${node_db_name} -c"
    esgf_node_manager_initialize --dburl ${postgress_user}:${pg_sys_acct_passwd}@${postgress_host}:${postgress_port}/${node_db_name} -c
    [ $? != 0 ] && echo "ERROR: Could not create esgf manager database tables in ${node_db_name}" && checked_done 1

    source deactivate

    write_node_manager_db_install_log
    echo

    popd >& /dev/null
    echo
    echo
    checked_done 0
}

write_node_manager_db_install_log() {
    echo "$(date ${date_format}) python:esgf_node_manager=${esgf_node_manager_db_version} " >> ${install_manifest}
    dedup ${install_manifest}
    return 0
}

#--------------------------------------
# Clean / Uninstall this module...
#--------------------------------------

clean_node_manager_webapp_subsystem() {
    init
    local doit="N"
    if [ -e ${node_manager_service_app_home} ]; then
        read -p "remove ESG Node Manager web service? (${node_manager_service_app_home}) [y/N]: " doit
        if [ "doit" = "Y" ] || [ "$doit" = "y" ]; then
            echo "removing ${node_manager_service_app_home}"
            if [ -n ${node_manager_service_app_home} ] ; then
                rm -rf ${node_manager_service_app_home}
                [ $? != 0 ] && echo "ERROR: Unable to remove ${node_manager_service_app_home}" && return 1
                perl -n -i -e'print unless m!webapp:esgf-node-manager!' ${install_manifest}
            fi
        fi
    fi
    if [ ! -e ${node_manager_service_app_home} ]; then
        remove_property node_manager_service_app_home
        remove_property node_manager_service_endpoint
    fi
    return 0
}

#NOTE: There are certain fields that we are not pulling from (node_db_name and postgress_host).
#      If the fit hits the shan I want to make it clear that we are not nuking anyone else's database, etc.
#      strictly the "esgcet" database on "localhost"
clean_node_manager_database_subsystem_installation() {
    init
    local doit="N"

    read -p "remove ESG Node Manager database, database subsystem AND DATA? (esgf_node_manager.*) [y/N]: " doit
    if [ "doit" = "Y" ] || [ "$doit" = "y" ]; then
        start_postgress
        if $(psql -U ${postgress_user} esgcet -c "\dt esgf_node_manager.;" | egrep '^\([1-9]* row[s]?\)' >& /dev/null); then

            #Call me paranoid but I want some integrity check that this code is what is meant to be straight from the distribution.
            diff <(md5sum ${scripts_dir}/esg-node-manager | tr -s " " | cut -d " " -f 1) <(curl -s -L --insecure ${esg_dist_url}/esgf-node-manager/esg-node-manager.md5 | tr -s " " | cut -d " " -f 1) >& /dev/null
            [ $? != 0 ] && echo " WARNING: Could not verify ${scripts_dir}/esg-node-manager, aborting this uninstall" && return 3
            echo "[VERIFIED]"
            
            backup_db -db ${node_db_name} -s node_manager
            [ $? != 0 ] && echo "unable to successfully backup database... will NOT proceed with database removal" && return 2

            echo "removing esgf_node_manager.* schema and tables and DATA"

            source ${cdat_home}/bin/activate esgf-pub

            esgf_node_manager_initialize --dburl ${postgress_user}:${pg_sys_acct_passwd}@localhost:${postgress_port}/esgcet -d 0
            [ $? != 0 ] && echo "ERROR: Unable to remove ${tomcat_install_dir}/webapps/${node_manager_app_context_root}" && return 1
            source deactivate

            rm -rvf ${cdat_home}/envs/esgf-pub/bin/esgf_node_manager_initialize && \
                rm -rf ${cdat_home}/envs/esgf-pub/lib/python${python_version}/site-packages/${esgf_node_manager_egg_file} && \
                perl -n -i -e'print unless m!python:esgf_node_manager!' ${install_manifest} && echo "[REMOVED]"
        else
            echo " Could not find node manager tables in the database."
        fi
    fi
    return 0
}

setup_py_pkgs() {

    export LD_LIBRARY_PATH=/opt/esgf/python/lib:/opt/esgf/python/lib/python2.7
    source /opt/esgf/virtual/python/bin/activate
    
#    pipcmd="pip install  --index-url=http://pypi.python.org/simple --trusted-host pypi.python.org"
    pipcmd="pip install"

    $pipcmd mod_wsgi    
    $pipcmd django==1.10.3
    $pipcmd requests
    $pipcmd psycopg2
    $pipcmd sqlalchemy
    $pipcmd pyopenssl
    
    source deactivate
    unset LD_LIBRARY_PATH 
}


setup_conda_env() {

    /usr/local/conda/bin/conda create -y -n esgf-nm -c conda-forge django

    [ $? != 0 ] && " ERROR: Could not create conda env for node manager with django" && popd && checked_done 1

    source /usr/local/conda/bin/attach esgf-nm
    conda install -y sqlalchemy psycopg2 requests
    [ $? != 0 ] && " ERROR: Could not install node manager python package dependencies" && popd && checked_done 1
    pip install mod_wsgi    
    [ $? != 0 ] && " ERROR: Could not install mod_wsgi for node manager using pip install" && popd && checked_done 1

    source deactivate

    return 0
}

setup_nm_repo() {

    

    peergroup=`grep node.peer.group /esg/config/esgf.properties | cut -d'=' -f 2`
    if [ $peergroup == "esgf-demo" ] ; then
        FED_NAME="demonet"
        else
        FED_NAME=$peergroup
    fi
 
    
    # this can be integrated into the installer
    INST_DIR=/usr/local
    quotedinstdir=`echo $INST_DIR|sed 's/[./*?|#\t]/\\\\&/g'`
    NM_DIR=$INST_DIR/esgf-node-manager/src
    PREFIX=__prefix__
    pushd $INST_DIR
    if [ ! -d esgf-node-manager ]; then

        git clone https://github.com/ESGF/esgf-node-manager.git
    else
        export GIT_SSL_NO_VERIFY=true
        pushd esgf-node-manager && git pull;
        popd
    fi

    popd

    pushd $NM_DIR 

    if [ $devel -eq 1 ]; then  
        git checkout devel
    else
        git checkout master
    fi

    # check for entry in apache conf and add if not present
    # count=`grep -c esgfnm /etc/httpd/conf/esgf-httpd.conf`

    # if [ $count > 0 ] ; then
    #     mystr=''; while read ln; do mystr=${mystr}\\n\\t"$ln"; done < scripts/nm-httpconf-lines
    #     quotedmystr=`echo $mystr|sed 's/[./*?|#%!^]/\\\\&/g'`
    #     sed -i "s/WSGIDaemonProcess\ cog\-site/$quotedmystr/" /etc/httpd/conf/esgf-httpd.conf
    # fi

    #generate a secret key and apply to the settings
    d=`date`
    ho=`hostname -f`
    sk=`echo $d $ho | sha256sum | awk '{print $1}'`

    sed -i s/changeme1/$sk/ python/server/nodemgr/nodemgr/settings.py
    popd


    cp $NM_DIR/scripts/esgf-nm-ctl $INST_DIR/bin/esgf-nm-ctl
    cp $NM_DIR/scripts/esgf-nm-func $INST_DIR/bin/esgf-nm-func


    chmod u+x $INST_DIR/bin/esgf-nm-ctl  $NM_DIR/scripts/esgfnmd
    adduser nodemgr
    usermod -a -G tomcat nodemgr

    usermod -a -G apache nodemgr

    mkdir -p /esg/log /esg/tasks /esg/config

    touch /esg/log/django.log
    touch /esg/log/esgf_nm.log
    touch /esg/log/esgf_nm_dj.log
    touch /esg/log/esgfnmd.out.log
    touch /esg/log/esgfnmd.err.log
    touch /esg/config/nm.properties
    touch /esg/config/registration.xml

    wget -O /esg/config/timestamp https://aims1.llnl.gov/nm-cfg/timestamp
    wget -O /esg/config/esgf_supernodes_list.json https://aims1.llnl.gov/nm-cfg/$FED_NAME/esgf_supernodes_list.json

    chown nodemgr:nodemgr /esg/log/esgf_nm.log
    chown nodemgr:nodemgr /esg/log/esgfnmd.out.log
    chown nodemgr:nodemgr /esg/log/esgfnmd.err.log
    chown nodemgr:apache /esg/config/nm.properties
    chown nodemgr:apache /esg/config/registration.xml
    chown nodemgr:nodemgr /esg/config/timestamp
    chown nodemgr:nodemgr /esg/config/esgf_supernodes_list.json
    chmod 777 /esg/tasks
    chown nodemgr:nodemgr $NM_DIR/scripts/esgfnmd
    chown apache:apache /esg/log/esgf_nm_dj.log
    chown apache:apache /esg/log/django.log
    

    chmod g+r /esg/config/.esg_pg_pass
    
    
    #rm -rf /root/apache_frontend
    pushd $NM_DIR/python/server

    fqdn=`grep esgf.host= /esg/config/esgf.properties | cut -d'=' -f 2`
    # get python to work
    source /usr/local/conda/bin/activate esgf-pub
    cmd="python gen_nodemap.py $NM_INIT $fqdn"
    echo $cmd
    $cmd

    local choice="Y"
    read -e -p "Automatic peer with super-node (if you administer one, ensure that it is running) [Y/n] " choice
    if [ -z $choice ] ; then
        choice="Y"
    fi
    choice=$(echo ${choice} | tr 'A-Z' 'a-z')
    if [ $choice != "n" ] ; then
        cmd="python ../client/member_node_cmd.py add default 0"
        echo $cmd
        $cmd

    fi
    source deactivate
    
    chmod 755 /esg/config/esgf_nodemgr_map.json
    chown nodemgr:apache /esg/config/esgf_nodemgr_map.json

    popd 

}