#!/bin/bash
#set -x
#Copyright (C) 2009-2012:
#    Gabes Jean, naparuba@gmail.com
#    Gerhard Lausser, Gerhard.Lausser@consol.de
#    David GUENAULT, dguenault@monitoring-fr.org
#    Romain FORLOT, rforlot@yahoo.com
#
#This file is part of Shinken.
#
#Shinken is free software: you can redistribute it and/or modify
#it under the terms of the GNU Affero General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#Shinken is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU Affero General Public License for more details.
#
#You should have received a copy of the GNU Affero General Public License
#along with Shinken.  If not, see <http://www.gnu.org/licenses/>.





# ENVIRONNEMENT


export myscripts=$(readlink -f $(dirname $0))
src=$myscripts
. "$myscripts"/install.d/shinken.conf


function remove(){
    cadre "Removing shinken" green
    skill

    manageparameters "quiet"

    if [ -d "$TARGET" ]
    then
        cecho " > Removing $TARGET" green
        rm -Rf $TARGET
    fi


    if [ -d "$ETC" ]
    then
        cecho " > Removing $ETC" green
        rm -Rf $ETC
    fi

    if [ -h "/etc/default/shinken" ]
    then
        cecho " > Removing defaults" green
        rm -Rf /etc/default/shinken
    fi
    if [ -f "/etc/init.d/shinken" ] || [ -f ${SYSTEMDPATH}lib/systemd/system/shinken-arbiter.service ]
    then
        cecho " > Removing startup scripts" green
        case $INIT in
            init)
                disable_service shinken
                rm -f /etc/init.d/shinken* >> ${LOGFILE} 2>&1
                ;;
            systemd)
                systemctl disable $SYSTEMDSERVICES >> ${LOGFILE} 2>&1
                rm -f ${SYSTEMDPATH}lib/systemd/system/shinken* >> ${LOGFILE} 2>&1
                systemctl --system daemon-reload
                ;;
        esac
    fi

    # Remove sudo file used for shinken
    rm -f /etc/sudoers.d/shinken

    if [ -d "$PNPPREFIX" ]
    then
        doremove="n"
        cread " > found a pnp4nagios installation: do you want to remove it? (y|N) =>  " yellow "n" "y n"
        if [ "$readvalue" == "y" ]
        then
            cecho " > Removing pnp4nagios" green
            rm -Rf $PNPPREFIX
            manage_service npcd stop >> ${LOGFILE} 2>&1
            disable_service npcd
            rm -f /etc/init.d/npcd >> ${LOGFILE} 2>&1
            rm -f /etc/${WEBSERVER}/conf.d/pnp4nagios.conf >> ${LOGFILE} 2>&1
            manage_service ${WEBSERVER} restart >> ${LOGFILE} 2>&1
        else
            cecho " > Aborting uninstallation of pnp4nagios" yellow
        fi
    fi

    if [ -d "$MKPREFIX" ]
    then
        doremove="n"
        cread " > found a check_mk multisite installation: do you want to remove it? (y|N) =>  " yellow "n" "y n"
        if [ "$readvalue" == "y" ]
        then
            cecho " > Removing check_mk multisite" green
            rm -Rf $MKPREFIX
            if [ -d $PNPPREFIX.MK ]
            then
                rm -Rf $PNPPREFIX.MK
            fi
            manage_service ${WEBSERVER} restart
        else
            cecho " > Aborting uninstallation of check_mk multisite" yellow
        fi
    fi

    if [ -d "$NAGVISPREFIX" ]
    then
        doremove="n"
        cread " > found a nagvis installation: do you want to remove it? (y|N) =>  " yellow "n" "y n"
        if [ "$readvalue" == "y" ]
        then
            cecho " > Removing nagvis" green
            rm -Rf $NAGVISPREFIX
            rm -f /etc/${WEBSERVER}/conf.d/nagvis.conf >> ${LOGFILE} 2>&1
            manage_service ${WEBSERVER} restart
        else
            cecho " > Aborting uninstallation of nagvis" yellow
        fi
    fi

    return 0
}

function disable_service(){
    service=$1
    case $CODE in
    REDHAT)
        chkconfig $service off >> ${LOGFILE} 2>&1
        chkconfig --del $service >> ${LOGFILE} 2>&1
        ;;
    Fedora)
        systemctl disable ${service}.service >> ${LOGFILE} 2>&1
        systemctl --system daemon-reload >> ${LOGFILE} 2>&1
        ;;
    DEBIAN)
        update-rc.d -f $service remove >> ${LOGFILE} 2>&1
        ;;
    esac
}

function enable_service(){
    service=$1
    case $CODE in
    REDHAT)
        chkconfig --add $service >> ${LOGFILE} 2>&1
        chkconfig $service on >> ${LOGFILE} 2>&1
        ;;
    Fedora)
        systemctl --system daemon-reload >> ${LOGFILE} 2>&1
        systemctl enable ${service}.service >> ${LOGFILE} 2>&1
        ;;
    DEBIAN)
        update-rc.d $service defaults >> ${LOGFILE} 2>&1
        ;;
    esac
}

function manage_service() {
    service=$1
    operation=$2

    case $CODE in
    REDHAT)
        service $service $operation >> ${LOGFILE} 2>&1
        ;;
    Fedora)
        systemctl $operation ${service}.service >> ${LOGFILE} 2>&1
        ;;
    DEBIAN)
        /etc/init.d/${service} $operation >> ${LOGFILE} 2>&1
        ;;
    esac
}

function purgeSQLITE(){
    cadre "Purge livestatus db logs" green
    if [ ! -f $TARGET/var/livestatus.db ]
    then
        cecho " > Livestatus db not found " yellow
        exit 1
    fi
    skill >> ${LOGFILE} 2>&1
    cecho " > We keep $KEEPDAYSLOG days of logs" green
    sqlite3 $TARGET/var/livestatus.db "delete from logs where time < strftime('%s', 'now') - 3600*24*$KEEPDAYSLOG"
    cecho " > Vaccum the sqlite DB" green
    sqlite3 $TARGET/var/livestatus.db VACUUM
}

function skill(){
    cecho " > Stopping Shinken" green
    case $INIT in
        init)
            /etc/init.d/shinken stop >> ${LOGFILE} 2>&1
            ;;
        systemd)
            systemctl stop $SYSTEMDSERVICES >> ${LOGFILE} 2>&1
            systemctl status $SYSTEMDSERVICES >> ${LOGFILE} 2>&1
            ;;
    esac
    pc=$(ps -aef | grep "$TARGET" | grep -v "grep" | wc -l )
    if [ $pc -ne 0 ]
    then
        OLDIFS=$IFS
        IFS=$'\n'
        for p in $(ps -aef | grep "$TARGET" | grep -v "grep" | awk '{print $2}')
        do
            cecho " > Killing $p " green
            kill -9 $p
        done

        IFS=$OLDIFS
    fi
    rm -Rf $TMP/bad_start*
    rm -Rf $TARGET/var/*.pid
}

function sstart() {
    cecho " > Starting shinken" green
    case $INIT in
        init)
            /etc/init.d/shinken start >> ${LOGFILE} 2>&1
            ;;
        systemd)
            systemctl --system daemon-reload
            systemctl start $SYSTEMDSERVICES >> ${LOGFILE} 2>&1
            systemctl status $SYSTEMDSERVICES >> ${LOGFILE} 2>&1
            ;;
        *)
            cecho " > Your init system aren't supported in shinken for now." red
            exit 2
            ;;
    esac
}


function setdirectives(){
    directives=$1
    fic=$2
    mpath=$3

    cecho "    > Going to $mpath" green
    pushd $mpath >> ${LOGFILE} 2>&1

    for pair in $directives
    do
        directive=$(echo $pair | awk -F= '{print $1}')
        value=$(echo $pair | awk -F= '{print $2}')
        cecho "       > Setting $directive to $value in $fic" green
        sed -i 's#^\# \?'$directive'=\(.*\)$#'$directive'='$value'#g' $fic
    done
    popd >> ${LOGFILE} 2>&1
}

##############################
### INSTALLATION FUNCTIONS ###
##############################

function create_user(){
    cadre "Creating user" green
    if [ ! -z "$(cat /etc/passwd | grep $SKUSER)" ]
    then
        cecho " > User $SKUSER already exists" yellow
    else
            useradd -r -s /bin/bash $SKUSER -m
    fi
        usermod -G $SKGROUP $SKUSER
}

function check_exist(){
    cadre "Checking for existing installation" green
    if [ -d "$TARGET" ]
    then
        cecho " > Target folder already exists" red
        exit 2
    fi
    if [ -e "/etc/init.d/shinken" ]
    then
        cecho " > Init scripts already exists" red
        exit 2
    fi
    if [ -L "/etc/default/shinken" ]
    then
        cecho " > Shinken default already exists" red
        exit 2
    fi

}

function shinken_exist(){
    if [ -d $TARGET ]
    then
        echo 1
        return 1
    else
        echo 0
        return 0
    fi
}

function installpkg(){
    type=$1
    shift
    package="$*"

    if [ "$type" == "python" ]
    then
        PVER=$(pythonvershort)
        PYEI="easy_install-$PVER"
        $PYEI $package >> ${LOGFILE} 2>&1
        return $?
    elif [ "$type" = "pkg" ]
    then
        case $CODE in
            REDHAT|Fedora)
                yum install -y $package >> ${LOGFILE} 2>&1
                if [ $? -ne 0 ]
                then
                    return 2
                fi
                ;;
            DEBIAN)
                apt-get update >> ${LOGFILE} 2>&1
                apt-get --allow-unauthenticated --force-yes install -y $package  >> ${LOGFILE} 2>&1
                if [ $? -ne 0 ]
                then
                    return 2
                fi
                ;;
        esac
    fi
    return 0
}

function debinstalled(){
    package=$1
    if [ -z "$(dpkg -l $package | grep "^ii")" ]
    then
        return 1
    else
        return 0
    fi
}

function prerequisites(){
    cadre "Checking prerequisites" green
    # common prereq
    bins="wget sed gawk grep python bash sudo"

    for b in $bins
    do
        rb=$(which $b >> ${LOGFILE} 2>&1)
        if [ $? -eq 0 ]
        then
            cecho " > Checking for $b: OK" green
        else
            cecho " > Checking for $b: NOT FOUND. Installing it!" red
            installpkg pkg $b
        fi
    done

    # distro prereq
    case $CODE in
        REDHAT)
            case $VERS in
                [5-6])
                    PACKAGES=$YUMPKGS
                    QUERY="rpm -q "
                    cd $TMP
                    $QUERY $EPELNAME >> ${LOGFILE} 2>&1
                    if [ $? -ne 0 ]
                    then
                        if [ $SKIPPREREQUISITES -eq 1 ]
                        then
                            cecho " SKIPPREREQUISITES enabled: won't install $EPEL" red
                        else
                            cecho " > Installing $EPELPKG" yellow
                            wget $WGETPROXY $EPEL  >> ${LOGFILE} 2>&1
                            if [ $? -ne 0 ]
                            then
                                cecho " > Error while trying to download EPEL repositories" red
                                exit 2
                            fi
                            rpm -Uvh ./$EPELPKG >> ${LOGFILE} 2>&1
                            find /etc/pki/rpm-gpg -name "RPM-GPG-KEY-EPEl*" -exec rpm --import {} \;
                        fi
                    else
                        cecho " > $EPELPKG already installed" green
                    fi
                    ;;
                *)
                    cecho " > Unsupported RedHat/CentOs version" red
                    exit 2
                    ;;
            esac
            ;;
        Fedora)
            case $VERS in
                16|17|18)
                    PACKAGES=$YUMPKGS
                    QUERY="rpm -q "
                    ;;
                *)
                    cecho " > Unsupported Fedora version" red
                    exit 2
                    ;;
            esac
            ;;

        DEBIAN)
            PACKAGES=$APTPKGS
            if [ $SKIPPREREQUISITES -ne 1 ]
            then
                apt-get update >> ${LOGFILE} 2>&1
            fi
            QUERY="debinstalled "
            ;;
    esac
    if [ $SKIPPREREQUISITES -eq 1 ]
    then
        cecho " > SKIPPREQUISITES enabled: won't install $PACKAGES" red
    else
        cecho " > Installing $PACKAGES " yellow
        installpkg pkg $PACKAGES
        if [ $? -ne 0 ]
        then
            cecho " > Error while trying to install $PACKAGES" red
            exit 2
        fi
    fi


    cecho " > Defining which python version to use :" green
    export PY="python-$(pythonvershort)"
    which $PY >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        export PY="python$(pythonvershort)"
    fi
    cecho " > $PY" green

    # setuptools
    cecho " > Installing setuptools" green
    wget --no-check-certificate $WGETPROXY $SETUPTOOLS >> ${LOGFILE} 2>&1
    tar zxvf setuptools-$SETUPTOOLSVERS.tar.gz >> ${LOGFILE} 2>&1
    cd setuptools-$SETUPTOOLSVERS
    $PY setup.py install >> ${LOGFILE} 2>&1

    # python prereq
    PYEI="easy_install-$PVER"
    if [ "$CODE" = "REDHAT" ]
    then
        if [ "$VERS" = "5" ]
        then
            PYLIBS=$PYLIBSRHEL
        elif [ "$VERS" = "6" ]
        then
            PYLIBS=$PYLIBSRHEL6
        fi
    elif [ "$CODE" = "Fedora" ]
    then
        case $VERS in
            16|17)
                PYLIBS=$PYLIBS
                ;;
        esac
    elif [ "$CODE" == "DEBIAN" ]
    then
        PYLIBS=$PYLIBSDEB
    fi

    # this is a fix for MySQL-python complainig about outdated version of distribute
    #cecho " > Installing distribute" green
    #$PYEI -U distribute >> ${LOGFILE} 2>&1

    for p in $PYLIBS
    do
        module=$(echo $p | awk -F: '{print $1'})
        import=$(echo $p | awk -F: '{print $2'})

        $PY "$myscripts"/install.d/tools/checkmodule.py -m $import  >> ${LOGFILE} 2>&1
        if [ $? -eq 2 ]
        then
            if [ $SKIPPREREQUISITES -eq 1 ]
            then
                cecho " > SKIPPREQUISITES enabled: won't install $p " red
            else
                cecho " > Module $module ($import) not found. Installing..." yellow
                installpkg python $module >> ${LOGFILE} 2>&1
                if [ $? -ne 0 ]
                then
                    cecho " > $type package $package doesn't installed properly. Run 'easy_install $package' manually" red
                    cecho " > and try to figure out what's is going on."
                fi
            fi
        else
            cecho " > Module $module found." green
        fi
    done

    if [ $MANAGEPYRO == 1 ]
    then
        cecho " > Installing managed python (experimental) " green
        #$PYEI pip
        export PIP="pip-$(pythonvershort)"
        yes | $PIP uninstall Pyro
        yes | $PIP uninstall Pyro4
        pyroset
        $PYEI "$PYRO"
    fi
}

function check_distro(){
    cadre "Verifying compatible distros" green

    if [ ! -e /usr/bin/lsb_release ]
    then
        cecho " > No compatible distribution found" red
        cecho " > maybe the lsb_release utility is not found" red
        cecho " > on redhat like distro you should try yum install redhat-lsb"
        exit 2
    fi

    if [ -z "$CODE" ]
    then
        cecho " > $DIST is not supported" red
        exit 2
    fi

    versionok=0
    distrook=0

    for d in $DISTROS
    do
        distro=$(echo $d | awk -F: '{print $1}')
        version=$(echo $d | awk -F: '{print $2}')
        if [ "$CODE" = "$distro" ]
        then
            if [ "$version" = "" ]
            then
                cecho " > Found $CODE ($DIST $VERS $ARCH)" green
                cecho " > Version checking for $DIST is not needed" green
                versionok=1
                return
            else
                if [ "$VERS" = "$version" ]
                then
                    cecho " > Found $CODE ($DIST $VERS $ARCH)" green
                    versionok=1
                    return
                fi
            fi
        fi
    done

    if [ $versionok -ne 1 ]
    then
        cecho " > $DIST $VERS is not supported" red
        exit 2
    fi
}

function get_from_git(){
    cadre "Getting shinken" green
    cd $TMP
    if [ -e "shinken" ]
    then
        rm -Rf shinken
    fi
    env GIT_SSL_NO_VERIFY=true git clone $GIT >> ${LOGFILE} 2>&1
    cd shinken
    cecho " > Switching to version $VERSION" green
    git checkout $VERSION >> ${LOGFILE} 2>&1
    export src=$TMP/shinken
    # clean up .git folder
    rm -Rf .git
}

function register(){
    cadre "API key registration" green

    if [ -f $ETC/apikey ]
    then
        exist=$(cat $ETC/apikey)
        if [ $? -ne 0 ]
        then
            exist=""
        fi
    else
        exist=""
    fi

    if [ -z "$exist" ]
    then
        cecho " > Api key not found" yellow
        cread " > Do you already have an api key? [y|n] " green "y" "y n"
        if [ "$readvalue" == "Y" ] || [ "$readvalue" == "y" ]
        then
            creads " --> Please Provide your apikey" yellow
            if [ ! -z "$readvalue" ]
            then
                cecho " --> apikey saved " green
                echo "$readvalue" > $ETC/apikey
            else
                cecho "  --> api key must not be empty" red
                register
            fi
            return 0
        fi
    else
        cecho " > Found api key" green
        exit 0
    fi

    cecho " > You must register on community.shinken-monitoring.org in order to use online pack management" yellow
    cread " --> Do you want to register? [Y|n] " yellow "Y" "y n"
    if [ "$readvalue" == "N" ] || [ "$readvalue" == "n" ]
    then
        cecho "  --> api key not configured. You will have to do it manualy. See: http://www.shinken-monitoring.org/wiki/packs/start" red
        return 0
    fi

    creads " -->login: " yellow
    login=$readvalue
    creads " --> password: " yellow
    pass1=$readvalue
    creads " --> confirm password: " yellow
    pass2=$readvalue
    if [ "$pass1" != "$pass2" ]
    then
        cecho " ----> Password confirmation does not match!" red
        register
    else
        pass=$pass1
        if [ -z "$pass" ]
        then
            cecho " ----> Password must not be empty" red
            register
        fi
    fi
    creads " --> Email" yellow
    email=$readvalue

    cecho " > trying to register " yellow

    result=$($TARGET/bin/shinken-packs -r -l $login -P $pass -e $email | grep "^OK")
    if [ ! -z "$result" ]
    then
        creads " --> Registration success. Please look at your email and click in the link in it to validate your account (Hit ENTER when done)" green
        cecho " ----> Getting key" green
        apikey=$($TARGET/bin/shinken-packs -g -l $login -P $pass | grep "api key is" | awk '{print $5}')
        if [ -z "$apikey" ]
        then
            cecho " ------> There was a problem getting your api key!" red
            cecho " ------> Yous should register manualy at http://community.shinken-monitoring.org" red
            return 1
        else
            cecho " ------> Registration success" green
            echo $apikey > $ETC/apikey
            apikey=$(cat $ETC/apikey)
            cd $ETC
            sed -i "s#api_key=.*#api_key="$apikey"#g" skonf.cfg
            return 0
        fi
    else
        cecho " --> Registration failed!" red
        register
    fi
    return 0
}

function relocate(){
    cadre "Relocate source tree to $TARGET" green
    # relocate source tree
    cd $TARGET

    # relocate macros
    for f in $(find $TARGET/install.d/tools/macros | grep "\.macro$")
    do
        cecho " > relocating macro $f" green
        sed -i "s#__PREFIX__#$TARGET#g" $f
        sed -i "s#__ETC__#$ETC#g" $f
    done

    # relocate nagios plugin path
    cecho " > relocate nagios plugin path" green
    sed -i "s#/usr/lib/nagios/plugins#$LIBEXEC#g" $ETC/resource.cfg
    sed -i "s#/usr/local/shinken/libexec#$LIBEXEC#g" $ETC/resource.cfg

    # relocate default /usr/local/shinken path
    if [ "$TARGET" != "/usr/local/shinken" ]
    then
	    for fic in $(find . | grep -v "shinken-install" | grep -v "\.pyc$" | xargs grep -snH "/usr/local/shinken" --color | cut -f1 -d' ' | awk -F : '{print $1}' | sort | uniq)
	    do
	        cecho " > Processing $fic" green
	        cp "$fic" "$fic.orig"
	        sed -i 's#/usr/local/shinken#'$TARGET'#g' "$fic"
	    done
    fi

    # do the same for etc dir if etc dir does not reside beside target
    if [ "$TARGET/etc" != "$ETC" ]
    then
        cd $ETC
        for fic in $(find . | grep -v "shinken-install" | grep -v "\.pyc$" | xargs grep -snH "/usr/local/shinken" --color | cut -f1 -d' ' | awk -F : '{print $1}' | sort | uniq)
        do
            cecho " > Processing $fic" green
            cp "$fic" "$fic.orig"
            sed -i 's#/usr/local/shinken#'$TARGET'#g' "$fic"
        done
        cd $TARGET
    fi

    # set some directives
    cadre "Set some configuration directives" green
    directives="workdir=$TARGET/var user=$SKUSER shinken_user=$SKUSER group=$SKGROUP shinken_group=$SKGROUP"
    for fic in $(ls -1 $ETC/daemons/*.ini)
    do
        cecho " > Processing $fic" green;
        setdirectives "$directives" $fic $ETC/daemons
    done
    daemons="arbiters schedulers brokers reactionners receivers pollers modules"
    for element in $dameons
    do
        cecho "  > processing $element" green
        files=$(ls -1 *.cfg)
        for file in $files
        do
            cecho "    > processing $file" green
        done
    done
    #directives="workdir=$TARGET/var shinken_user=$SKUSER shinken_group=$SKGROUP local_log=$TARGET/var/arbiter.log lock_file=$TARGET/var/arbiter.pid"
    #for fic in $(ls -1 $ETC/nagios*cfg)
    #do
    #    cecho " > Processing $fic" green;
    #    setdirectives "$directives" $fic $TARGET
    #done
    # relocate default file
    cd $TARGET/bin/default
    cat $TARGET/bin/default/shinken.in | sed  -e 's#LOG\=\(.*\)$#LOG='$TARGET'/var#g' -e 's#RUN\=\(.*\)$#RUN='$TARGET'/var#g' -e  's#ETC\=\(.*\)$#ETC='$ETC'#g' -e  's#VAR\=\(.*\)$#VAR='$TARGET'/var#g' -e  's#BIN\=\(.*\)$#BIN='$TARGET'/bin#g' > $TARGET/bin/default/shinken
    rm $TARGET/bin/default/shinken.in
    # relocate init file
    sed -i -e "s#\#export PYTHONPATH=.*#export PYTHONPATH="$TARGET"#g" $TARGET/bin/init.d/shinken
    # relocate skonf.cfg
    cd $ETC


    # relocate she bang python in every python files
    PY=$(which $(pythonver))
    for f in $(grep "\#\!.*python" $TARGET/bin/shinken* | grep -v "\.py" | awk -F: '{print $1}')
    do
        cecho "Relocate python interpreter to $PY in $f" green
        sed -i "s#/usr/bin/env python#$PY#g" $f
    done

}

function fix(){
    cadre "Applying various fixes" green

    # fix some path
    for f in $(grep -R "/var/run/shinken" $TARGET/ | awk -F: '{print $1}' | sort | uniq)
    do
        sed -i "s#/var/run/shinken#"$TARGET"/var#g" $f
    done

    for f in $(grep -R "/var/log/shinken" $TARGET/ | awk -F: '{print $1}' | sort | uniq)
    do
        sed -i "s#/var/log/shinken#"$TARGET"/var#g" $f
    done

    for f in $(grep -R "/var/lib/shinken" $TARGET/ | awk -F: '{print $1}' | sort | uniq)
    do
        sed -i "s#/var/lib/shinken#"$TARGET"#g" $f
    done

    # fix the htpasswd.users file path for WEBUI authentication
    export PYTHONPATH=$TARGET
    export PY="$(pythonver)"
    result=$($PY $TARGET/install.d/tools/skonf.py -f $ETC/shinken-specific.cfg -a setparam -o module -r "module_name=Apache_passwd" -d "passwd" -v "$ETC/htpasswd.users")
    cecho " > $result" green

    cecho " > Make files permissions and owners good" green
    [ -f /etc/init.d/shinken ] && chmod +x /etc/init.d/shinken
    chmod +x $TARGET/bin/init.d/shinken
    chown -R $SKUSER:$SKGROUP $TARGET
    chown -R $SKUSER:$SKGROUP $ETC
    chmod -R g+w $TARGET
    chmod -R g+w $ETC

    cecho " > Fix /etc/sudoers file for shinken integration" green
    cp /etc/sudoers /etc/sudoers.$(date +%Y%m%d%H%M%S)
    sed -i "s/^Defaults.*requiretty/#&/g" /etc/sudoers
    cecho " > Add Shinken entry for skonf in sudoers" green
    echo "$SKUSER ALL=(ALL) NOPASSWD: /usr/bin/nmap" >> /etc/sudoers.d/shinken
    chmod u=r,g=r,o= /etc/sudoers.d/shinken
    [ $? -ne 0 ] && cecho "$DIST isn't supported, you must fix sudo permission at your own !!!" red


}

function enable_initd(){
    cecho " > Installing startup scripts" green
    case $INIT in
        init)
            cp $TARGET/bin/init.d/shinken* /etc/init.d/
            cecho " > Enabling $DIST startup script" green
            enable_service shinken
            ;;
        systemd)
            cp -f  "$myscripts"/for_fedora/systemd/* ${SYSTEMDPATH}lib/systemd/system
            # Fix path to daemon in service file for systemd
            sed -i -e 's/sbin/local\/shinken\/bin/' -e 's/etc\/shinken/usr\/local\/shinken\/etc/g' ${SYSTEMDPATH}lib/systemd/system/shinken*
            sed -i -e 's/Type=forking/&\nWorkingDirectory=\/usr\/local\/shinken\/shinken/g' ${SYSTEMDPATH}lib/systemd/system/shinken-arbiter
            cecho " > Enabling $DIST startup script" green
            enable_service shinken-arbiter
            enable_service shinken-broker
            enable_service shinken-poller
            enable_service shinken-reactionner
            enable_service shinken-receiver
            enable_service shinken-scheduler
            ;;
        *)
            cecho " > Error! your init system isn't support by shinken for now." red
            ;;
    esac
}

function cleanuptarget(){
    # clean up unnecessary files and folders
    if [ "$TARGET" == "/" ]
    then
        cecho " > Invalid target folder" red
        exit 2
    fi
    if [ -z "$TARGET" ]
    then
        cecho " > Invalid target folder" red
        exit 2
    fi

    cd $TARGET
    rm -Rf test
    rm -Rf windows
    rm -Rf clean.sh
    rm -Rf for_fedora
    rm -Rf for_packages
#    rm -Rf install install.d
    rm -Rf MANIFEST.in setup.py
    rm -Rf $ETC/*windows*

    cp $ETC/shinken-specific.cfg $ETC/shinken-specific.cfg.orig
}

function manageparameters(){

    if [ ! -f "$HOME/.shinken" ]
    then
        touch $HOME/.shinken
        chmod +x $HOME/.shinken

        if [ "$1" != "quiet" ]
        then
            echo "ETC=$ETC" >> $HOME/.shinken
            echo "VAR=$VAR" >> $HOME/.shinken
            echo "LIBEXEC=$LIBEXEC" >> $HOME/.shinken
            echo "TARGET=$TARGET" >> $HOME/.shinken
        fi
    else
        OLDIFS=$IFS
        IFS=$'\n'
        cecho " > Found installation parameters" yellow
        for l in $(cat $HOME/.shinken)
        do
            cecho " --> $l" yellow
        done
        IFS=$OLDIFS
        if [ "$1" != "quiet" ]
        then
            cread " > Do you want to reuse those parameters? " "yellow" "y" "y n"
        else
            readvalue="y"
        fi
        if [ "$readvalue" == "y" ]
        then
            for l in $(cat $HOME/.shinken)
            do
                export $l
            done
        else
            rm -f $HOME/.shinken
            touch $HOME/.shinken
            chmod +x $HOME/.shinken
            echo "ETC=$ETC" >> $HOME/.shinken
            echo "VAR=$VAR" >> $HOME/.shinken
            echo "LIBEXEC=$LIBEXEC" >> $HOME/.shinken
            echo "TARGET=$TARGET" >> $HOME/.shinken
        fi
    fi
}



function sinstall(){
    #cecho "Installing shinken" green

    manageparameters

    check_distro
    check_exist
    prerequisites
    create_user

    # control if target parent folder exist
    parent=$(echo $TARGET | sed -e "s#"$(echo $TARGET | awk -F/ '{print $NF}')"##g")

    if [ ! -d $parent ]
    then
        mkdir -p $parent
    fi

    if [ ! -d $ETC ]
    then
        mkdir -p $ETC
    fi

    if [ ! -d $TARGET ]
    then
        mkdir -p $TARGET
    fi

    cp -Rf  "$myscripts"/* $TARGET/

    if [ "$TARGET" == "/" ] || [ -z "$TARGET" ]
    then
        cecho " ALERT! TARGET should not be empty or /" red
        exit 2
    fi

    if [ "$TARGET/etc" != "$ETC" ]
    then
        rm -Rf $TARGET/etc
        cp -Rf  "$myscripts"/etc/* $ETC/
    fi

    # We want to copy the src/share into the var
    if [ ! -d $TARGET/var/share ]
    then
	mkdir -p $TARGET/var/share
	cp -Rf  "$myscripts"/share/* $TARGET/var/share
    fi

    cleanuptarget
    relocate
    ln -s $TARGET/bin/default/shinken /etc/default/shinken
    mkdir -p $TARGET/var/archives
    #enableretention
    enable_initd
    if [[ $NOMONGO -eq 1 ]]
    then
        cecho " > Skip mongo installation" green
    else
        install_mongodb
    fi

    if [ "$LOGSTORE" == "mongo" ]
    then
        enablemongologs
    fi

    fix

    sstart
    cecho "+------------------------------------------------------------------------------" green
    cecho "| Shinken is now installed on your server " green
    cecho "| The install location is: $TARGET" green
    cecho "| The configuration folder is: $ETC" green
    cecho "| " green
    cecho "| The Web Interface is available at: http://localhost:7767" green
    cecho "| The default credentials for the webui are admin/admin" green
    cecho "| " green
    cecho "| You can now learn how to configure shinken at: http://www.shinken-monitoring.org/wiki" green
    cecho "+------------------------------------------------------------------------------" green
    echo ""
    cecho "Do not forget to allow localhost to browse the entire snmp tree. Sample configuration" yellow
    cecho "use snmp checks and the plugins should be able to access local snmp tree." yellow
    cecho "For testing purpose you could modify your snmpd configuration this way :" yellow
    echo ""
    cecho "Debian :" yellow
    cecho " edit /etc/snmp/snmpd.conf and replace the following lines :" yellow
    cecho " view   systemonly  included   .1.3.6.1.2.1.1" yellow
    cecho " view   systemonly  included   .1.3.6.1.2.1.25.1" yellow
    cecho " by this one : " yellow
    cecho " view   systemonly  included   .1" yellow
    echo ""
    cecho "RedHat/CentOS :" yellow
    cecho " edit /etc/snmp/snmpd.conf and replace the following lines :" yellow
    cecho " view   systemview  included   .1.3.6.1.2.1.1" yellow
    cecho " view   systemview  included   .1.3.6.1.2.1.25.1" yellow
    cecho " by this one : " yellow
    cecho " view   systemview  included   .1" yellow

}


########################
### BACKUP FUNCTIONS ###
########################

function backup(){
    cadre "Backup shinken platform" green

    manageparameters "quiet" > /dev/null 2>&1

    cecho " > Stop shinken" green
    skill
    if [ -f /etc/init.d/npcd ]
    then
        cecho " > Stop npcd" green
        manage_service npcd stop > /dev/null 2>&1
    fi

    if [ ! -e $BACKUPDIR ]
    then
        mkdir $BACKUPDIR
    fi
    mkdir -p $BACKUPDIR/bck-shinken.$DATE/etc
    mkdir -p $BACKUPDIR/bck-shinken.$DATE/libexec
    mkdir -p $BACKUPDIR/bck-shinken.$DATE/var
    # Sugg:
    # Add: cp -Rfp $TARGET/bin $BACKUPDIR/bck-shinken.$DATE/ line to backup bin
    cecho " > Backup shinken" green
    cecho " --> Backup shinken/etc" green
    cp -Rfp $ETC/* $BACKUPDIR/bck-shinken.$DATE/etc
    cecho " --> Backup shinken/libexec" green
    cp -Rfp $LIBEXEC/* $BACKUPDIR/bck-shinken.$DATE/libexec
    cecho " --> Backup shinken/var" green
    cp -Rfp $TARGET/var/* $BACKUPDIR/bck-shinken.$DATE/var

    if [ -d $NAGVISPREFIX ]
    then
        cecho " > Backup nagvis" green
        mkdir -p $BACKUPDIR/bck-shinken.$DATE/nagvis
        cecho " --> Backup nagvis/share/userfiles" green
        cp -Rfp $NAGVISPREFIX/share/userfiles $BACKUPDIR/bck-shinken.$DATE/nagvis/
        cecho " --> Backup nagvis/etc" green
        cp -Rfp $NAGVISPREFIX/etc $BACKUPDIR/bck-shinken.$DATE/nagvis/
    fi

    if [ -d $MKPREFIX ]
    then
        mkdir -p $BACKUPDIR/bck-shinken.$DATE/check_mk
        cecho " > Backup check_mk" green
        cecho " --> Backup check_mk/etc" green
        cp -Rfp $MKPREFIX/etc $BACKUPDIR/bck-shinken.$DATE/check_mk/
        cecho " --> Backup check_mk/share" green
        cp -Rfp $MKPREFIX/share/web/plugins/dashboard $BACKUPDIR/bck-shinken.$DATE/check_mk/
        cp -Rfp $MKPREFIX/share/web/plugins/perfometer $BACKUPDIR/bck-shinken.$DATE/check_mk/
        cp -Rfp $MKPREFIX/share/web/htdocs/images/icons $BACKUPDIR/bck-shinken.$DATE/check_mk/
        cecho " --> Backup check_mk/var" green
        cp -Rfp $MKPREFIX/var/lib/web $BACKUPDIR/bck-shinken.$DATE/check_mk/

    fi

    if [ -d $PNPPREFIX ]
    then
        mkdir -p $BACKUPDIR/bck-shinken.$DATE/pnp4nagios
        cecho " > Backup pnp4nagios" green
        cecho " --> Backup pnp4nagios/etc" green
        cp -Rfp $PNPPREFIX/etc $BACKUPDIR/bck-shinken.$DATE/pnp4nagios/
        cecho " --> Backup pnp4nagios/var" green
        cp -Rfp $PNPPREFIX/var $BACKUPDIR/bck-shinken.$DATE/pnp4nagios/
        cecho " --> Backup pnp4nagios/share/templates" green
        cp -Rfp $PNPPREFIX/share/templates $BACKUPDIR/bck-shinken.$DATE/pnp4nagios/
    fi

    cecho " > Compressing backup file" green
    tar czPf $BACKUPDIR/bck-shinken.$DATE.tar.gz $BACKUPDIR/bck-shinken.$DATE
    cecho " > Removing backup folder. Backup is now at " green
    echo $BACKUPDIR/bck-shinken.$DATE.tar.gz
    rm -rf $BACKUPDIR/bck-shinken.$DATE

    cecho " > Start shinken" green
    sstart
    if [ -f /etc/init.d/npcd ]
    then
        cecho " > Start npcd" green
        manage_service npcd start
    fi

    cadre " > Backup done. Id is $DATE" green
}

function backuplist(){
    cadre "List of available backups in $BACKUPDIR" green
    manageparameters "quiet"
    for d in $(ls -1 $BACKUPDIR | grep "bck-shinken" | awk -F. '{print $2}')
    do
        cecho " > $d" green
    done

}

function restore(){
    cadre "Restore shinken configuration, plugins and data" green

    manageparameters "quiet"

    skill
    if [ ! -e $BACKUPDIR ]
    then
        cecho " > Backup folder not found" red
        exit 2
    fi
    if [ -z $1 ]
    then
        cecho " > No backup timestamp specified" red
        backuplist
        exit 2
    fi
    if [ ! -e $BACKUPDIR/bck-shinken.$1.tar.gz ]
    then
        cecho " > Backup not found: $BACKUPDIR/bck-shinken.$1 " red
        backuplist
        exit 2
    fi

    cecho " > Unpacking backup files" green
    mkdir $BACKUPDIR/bck-shinken.$1
    tar xzPf $BACKUPDIR/bck-shinken.$1.tar.gz -C $BACKUPDIR/bck-shinken.$1

    # Keep Shinken original files as samples conf files and restore.
    cp -Rfp -b -S ".example" $BACKUPDIR/bck-shinken.$1/etc/* $ETC/
    cp -Rfp $BACKUPDIR/bck-shinken.$1/var/* $VAR/
    cp -Rfp $BACKUPDIR/bck-shinken.$1/libexec/* $LIBEXEC/

    if [ -d $NAGVISPREFIX ]
    then
        cecho " --> Restoring Restore Nagvis" green
	 cp -Rfp $BACKUPDIR/bck-shinken.$1/nagvis/etc/* $NAGVISPREFIX/etc
	 cp -Rfp $BACKUPDIR/bck-shinken.$1/nagvis/userfiles/* $NAGVISPREFIX/share/userfiles
    fi

    if [ -d $MKPREFIX ]
    then
        cecho " --> Restoring Restore Check_mk" green
	 cp -Rfp $BACKUPDIR/bck-shinken.$1/check_mk/etc/* $MKPREFIX/etc
	 cp -Rfp $BACKUPDIR/bck-shinken.$1/check_mk/dashboard/* $MKPREFIX/share/web/plugins/dashboard
	 cp -Rfp $BACKUPDIR/bck-shinken.$1/check_mk/perfometer/* $MKPREFIX/share/web/plugins/perfometer
	 cp -Rfp $BACKUPDIR/bck-shinken.$1/check_mk/icons/* $MKPREFIX/share/web/htdocs/images/icons
	 cp -Rfp $BACKUPDIR/bck-shinken.$1/check_mk/web/* $MKPREFIX/var/lib/web
    fi

    if [ -d $PNPPREFIX ]
    then
        cecho " --> Restoring Restore PNP4Nagios" green
	 cp -Rfp $BACKUPDIR/bck-shinken.$1/pnp4nagios/etc/* $PNPPREFIX/etc
	 cp -Rfp $BACKUPDIR/bck-shinken.$1/pnp4nagios/var/* $PNPPREFIX/var
	 cp -Rfp $BACKUPDIR/bck-shinken.$1/pnp4nagios/templates/* $PNPPREFIX/share/templates
    fi

    rm -rf $BACKUPDIR/bck-shinken.$1

    # Start Shinken after restore
    sstart
    if [ -f /etc/init.d/npcd ]
    then
        cecho " > Start npcd" green
        /etc/init.d/npcd start > /dev/null 2>&1
    fi


    cecho " > Restoration done. Original files was kept with '.example' suffix, use 'diff' to check differences between your files and original." green
}

########################
### UPDATE FUNCTIONS ###
########################

function supdate(){

    curpath=$(pwd)
    if [ "$myscripts" == "$TARGET" ]
    then
        cecho "You should use the source tree to update and not use the target folder!!!!!" red
        exit 2
    fi

    cadre "Updating shinken" green

    skill
    backup
    remove
    get_from_git
    cp -Rf  "$myscripts" $TARGET
    relocate
    ln -sf $TARGET/bin/default/shinken /etc/default/shinken
    cp $TARGET/bin/init.d/shinken* /etc/init.d/
    mkdir -p $TARGET/var/archives
    fix
    restore $DATE
}

##############################
### EXPLOITATION FUNCTIONS ###
##############################

function compresslogs(){
    cadre "Compress rotated logs" green
    if [ ! -d $TARGET/var/archives ]
    then
        cecho " > Archives directory not found" yellow
        exit 0
    fi
    cd $TARGET/var/archives
    if [ ! -z "$(ls)" ]
    then
        for l in $(ls ./*.log)
        do
            file=$(basename $l)
            if [ -e $file ]
            then
                cecho " > Processing $file" green
                tar czf $file.tar.gz $file
                rm -f $file
            fi
        done
    fi
}

########################
### CONFIG FUNCTIONS ###
########################

function cleanconf(){
    if [ -z "$myscripts" ]
    then
        cecho " > Files/Folders list not found" yellow
        exit 2
    else
        for f in $(cat "$myscripts"/install.d/config.files)
        do
            cecho " > Removing $ETC/$f" green
            rm -Rf $ETC/$f
        done
    fi
}

function fixsudoers(){
    cecho " > Fix /etc/sudoers.d/shinken file for shinken integration" green
    cat "$myscripts"/install.d/sudoers.centreon | sed -e 's#TARGET#'$TARGET'#g' >> /etc/sudoers.d/shinken
}

function fixcentreondb(){
    cecho " > Fix centreon database path for shinken integration" green

    # get existing db access
    host=$(cat /etc/centreon/conf.pm | grep "mysql_host" | awk '{print $3}' | sed -e "s/\"//g" -e "s/;//g")
    user=$(cat /etc/centreon/conf.pm | grep "mysql_user" | awk '{print $3}' | sed -e "s/\"//g" -e "s/;//g")
    pass=$(cat /etc/centreon/conf.pm | grep "mysql_passwd" | awk '{print $3}' | sed -e "s/\"//g" -e "s/;//g")
    db=$(cat /etc/centreon/conf.pm | grep "mysql_database_oreon" | awk '{print $3}' | sed -e "s/\"//g" -e "s/;//g")

    cp "$myscripts"/install.d/centreon.sql $TMP/centreon.sql
    sed -i 's#TARGET#'$TARGET'#g' $TMP/centreon.sql
    sed -i 's#CENTREON#'$db'#g' $TMP/centreon.sql

    if [ -z "$pass" ]
    then
        mysql -h $host -u $user $db < $TMP/centreon.sql
    else
        mysql -h $host -u $user -p$pass $db < $TMP/centreon.sql
    fi
}

function fixforfan(){
    if [ ! -z "cat /etc/issue | grep FAN" ]
    then
        chown -R apache:nagios $ETC
        chmod -R g+rw $ETC/*
    fi
}

# ENABLE MONGO STORAGE FOR LOGS
function enablemongologs(){
    cecho " > Enable mongodb log storage" green
    export PYTHONPATH=$TARGET
    export PY="$(pythonver)"
    result=$($PY $TARGET/install.d/tools/skonf.py -a macros -f $TARGET/install.d/tools/macros/enable_log_mongo.macro -d $MONGOSERVER)
    if [ $? -ne 0 ]
    then
        cecho " > There was an error while trying to enable mongo log storage ($result)" red
        exit 2
    fi
}

function enablendodb(){
    cecho " > FIX shinken ndo configuration" green
    # get existing db access
    host=$(cat /etc/centreon/conf.pm | grep "mysql_host" | awk '{print $3}' | sed -e "s/\"//g" -e "s/;//g")
    user=$(cat /etc/centreon/conf.pm | grep "mysql_user" | awk '{print $3}' | sed -e "s/\"//g" -e "s/;//g")
    pass=$(cat /etc/centreon/conf.pm | grep "mysql_passwd" | awk '{print $3}' | sed -e "s/\"//g" -e "s/;//g")
    db="centreon_status"
    # add ndo module to broker
    # first get existing broker modules
    export PYTHONPATH=$TARGET
    export PY="$(pythonver)"
    result=$($PY $TARGET/install.d/tools/skonf.py -a macros -f $TARGET/install.d/tools/macros/ces_enable_ndo.macro -d $host,$db,$user,$pass)
    if [ $? -ne 0 ]
    then
        cecho $result red
        exit 2
    fi
}

function enableretention(){

    cecho " > Enable retention for broker scheduler and arbiter" green

    if [ "$RETENTIONMODULE" == "mongo" ]
    then
        export PYTHONPATH=$TARGET
        export PY="$(pythonver)"

        result=$($PY $TARGET/install.d/tools/skonf.py -a macros -f $TARGET/install.d/tools/macros/enable_retention_mongo.macro)
        if [ $? -ne 0 ]
        then
            cecho $result red
            exit 2
        fi
    else
        export PYTHONPATH=$TARGET
        export PY="$(pythonver)"
        result=$($PY $TARGET/install.d/tools/skonf.py -a macros -f $TARGET/install.d/tools/macros/enable_retention.macro)
        if [ $? -ne 0 ]
        then
            cecho $result red
            exit 2
        fi
    fi
}

function enableperfdata(){

    cecho " > Enable perfdata " green

    export PYTHONPATH=$TARGET
    export PY="$(pythonver)"
    cecho " > Getting existing broker modules list" green
    modules=$($PY $TARGET/install.d/tools/skonf.py -a getdirective -f $ETC/shinken-specific.cfg -o broker -d modules)
    if [ -z "$modules" ]
    then
        modules="Service-Perfdata, Host-Perfdata"
    else
        modules=$modules", Service-Perfdata, Host-Perfdata"
    fi
    result=$($PY $TARGET/install.d/tools/skonf.py -q -a setparam -f $ETC/shinken-specific.cfg -o broker -d modules -v "$modules")
    cecho " > $result" green
}

function setdaemonsaddresses(){
    export PYTHONPATH=$TARGET
    export PY="$(pythonver)"
    localip=$(ifconfig $IF | grep "^ *inet ad:" | awk -F : '{print $2}' | awk '{print $1}')
    result=$($PY $TARGET/install.d/tools/skonf.py -q -a setparam -f $ETC/shinken-specific.cfg -o arbiter -d address -v "$localip")
    cecho " > $result" green
    result=$($PY $TARGET/install.d/tools/skonf.py -q -a setparam -f $ETC/shinken-specific.cfg -o scheduler -d address -v "$localip")
    cecho " > $result" green
    result=$($PY $TARGET/install.d/tools/skonf.py -q -a setparam -f $ETC/shinken-specific.cfg -o reactionner -d address -v "$localip")
    cecho " > $result" green
    result=$($PY $TARGET/install.d/tools/skonf.py -q -a setparam -f $ETC/shinken-specific.cfg -o receiver -d address -v "$localip")
    cecho " > $result" green
    result=$($PY $TARGET/install.d/tools/skonf.py -q -a setparam -f $ETC/shinken-specific.cfg -o poller -d address -v "$localip" -r "poller_name=poller-1")
    cecho " > $result" green
}

function enableCESCentralDaemons(){
    setdaemons "arbiter reactionner receiver scheduler broker poller"
}

function enableCESPollerDaemons(){
    setdaemons "poller"
}

function disablenagios(){
    disable_service nagios
    disable_service ndo2db
    manage_service nagios stop
    manage_service ndo2db stop
}

function setdaemons(){
    daemons="$(echo $1)"
    avail="AVAIL_MODULES=\"$daemons\""
    cecho " > Enabling the followings daemons: $daemons" green
    sed -i "s/^AVAIL_MODULES=.*$/$avail/g" /etc/init.d/shinken
}

###########################
### addons installation ###
###########################

function install_graphite()
{

    if [ $SKIPPREREQUISITES -eq 1 ]
    then
        cecho " SKIPPREREQUISITES enabled: won't install nconf prerequisites" yellow
    else
            cecho " > Installing prerequisites" green
            cd $TMP
            installpkg python $GRAPHITEPKG
            case $CODE in
              DEBIAN)
                installpkg pkg $GRAPHITEPKGDEB
                ;;
            esac
    fi

    cadre "Install graphite add-on" green
    cd $TMP
    cecho "Downloading Graphite" green

    rm -rf graphite-web carbon whisper
    git clone ${GRAPHITE_WEB_GITHUB} >> $LOGFILE 2>&1
    git clone ${CARBON_GITHUB} >> $LOGFILE 2>&1
    git clone ${WHISPER_GITHUB} >> $LOGFILE 2>&1

    cecho "Installing Whisper component" green
    cd whisper
    python setup.py install >> $LOGFILE 2>&1

    cecho "Installing Carbon component" green
    cd ../carbon
    python setup.py install >> $LOGFILE 2>&1

    cecho "Configuring Carbon" green
    cd ..
    cd /opt/graphite/conf
    cp carbon.conf.example carbon.conf
    cp storage-aggregation.conf.example storage-aggregation.conf
    cp storage-schemas.conf.example storage-schemas.conf

    cecho "Installing Graphite webapp" green
    cd - > /dev/null 2>&1
    cd graphite-web
    python check-dependencies.py >> $LOGFILE 2>&1
    python setup.py install >> $LOGFILE 2>&1

    cecho "Configuring Graphite webapp" green
    # Set TZ to avoid issue with 4H graph into shinken and set default database engine
    cd /opt/graphite/webapp/graphite
    cp local_settings.py.example local_settings.py
    sed -i -r -e "s/^.(SECRET_KEY = ).*/\1'POk#dr5*4znkd6511z'/" -e "s/.TIME_ZONE.*/TIME_ZONE = \'Europe\/Paris\'/" -e 's/#(DATABASES = .*| *.default.: .*| *.NAME.*,| *.ENGINE.*| *.USER.*| *.PASSWORD.*| *.HOST.*| *.PORT.*|    \}|\})/\1/' local_settings.py
    python manage.py syncdb --noinput > $LOGFILE
    python manage.py createsuperuser --username=root --email=default@mail.org --noinput >> $LOGFILE 2>&1
    case $CODE in
        REDHAT|Fedora)
            cp /opt/graphite/examples/example-graphite-vhost.conf /etc/httpd/conf.d/graphite.conf
            ;;
        DEBIAN)
            cp /opt/graphite/examples/example-graphite-vhost.conf /etc/apache2/sites-available/graphite.conf
            sed -i 's%@DJANGO_ROOT@%/usr/local/lib/python2.7/dist-packages/Django-1.5.4-py2.7.egg/django/%g' /etc/apache2/sites-available/graphite.conf
            sed -i 's%WSGISocketPrefix .*$%WSGISocketPrefix /usr/local/shinken/var/apache2/wsgi%' /etc/apache2/sites-available/graphite.conf
            a2ensite graphite.conf
            ;;
        *)
            cecho " > Unknown distribution : $DIST. Webapp not properly integrated into apache configuration." yellow
            exit 2
            ;;
    esac
    cp /opt/graphite/conf/graphite.wsgi.example /opt/graphite/conf/graphite.wsgi
    # Fix permissions and activate vhost for graphite webapp. VERY IMPORTANT TO HAVE GOOD PERMISSIONS!
    chown $HTTPDUSER:$HTTPDGROUP /opt/graphite/storage/log/webapp
    chown $HTTPDUSER:$HTTPDGROUP /opt/graphite/storage/
    chown $HTTPDUSER:$HTTPDGROUP /opt/graphite/storage/graphite.db

    echo ""
    cecho " > Reboot Apache"
    case $CODE in
      DEBIAN)
        /etc/init.d/apache2 restart
        ;;
      REDHAT|Fedora)
        /etc/init.d/httpd restart
        ;;
    esac

    echo ""
    cecho "If you want to be able to log in graphite webapp using root account" yellow
    cecho "you have to set its password using command 'python manage.py changepassword' inside" yellow
    cecho "/opt/graphite/webapp/graphite directory." yellow
    echo ""
    cecho "Do not forget to configure broker and webui to include graphite module" yellow
    echo ""
    cadre "Graphite addon installation finished" green
}

function uninstall_graphite()
{
    cadre "Uninstalling graphite add-on" green
    rm -rf /opt/graphite
    case $CODE in
        REDHAT|Fedora)
            rm -f /etc/httpd/conf.d/graphite.conf
            ;;
        DEBIAN)
            a2disite graphite.conf
            rm -f /etc/httpd/sites-available/graphite.conf
            ;;
        *)
            cecho " > Unknown distribution : $DIST. Webapp not properly deleted into apache configuration." yellow
            exit 2
            ;;
    esac
}

# mongodb
function install_mongodb(){
    cadre "Install mongodb server" green
    cd "$myscripts"
    case $CODE in
        REDHAT)
            if [ ! -f "/etc/yum.repos.d/10gen.repo" ]
            then
                cp install.d/10gen.repo.in /etc/yum.repos.d/10gen.repo
                case $ARCH in
                    i386)
                        MARCH=i686
                        ;;
                    *)
                        MARCH=$ARCH
                        ;;
                esac
                sed -i "s/__ARCH__/"$MARCH"/g" /etc/yum.repos.d/10gen.repo
                yum clean all >> ${LOGFILE} 2>&1
                cecho " > Installing mongodb server" green
                installpkg pkg $MONGOPKGS
                cecho " > Enable and start mongodb server" green
                enable_service mongod
                manage_service mongod start
            else
                # check if mongo is installed
                exist=$(rpm -qa | grep mongo-10gen)
                if [[ -z "$exist" ]]
                then
                    cecho " > Installing mongodb server" green
                    installpkg pkg $MONGOPKGS
                    cecho " > Enable and start mongodb server" green
                    enable_service mongod
                    manage_service mongod start
                else
                    cecho "Already installed" yellow
                fi
            fi
            ;;
        Fedora)
            cecho " > Installing mongodb server" green
            if [ ! -f ${SYSTEMDPATH}lib/systemd/system/mongod.service ]
            then
                    installpkg pkg $MONGOPKGS
            else
                cecho "Already installed" yellow
            fi
            cecho " > Enable and start mongodb server" green
            enable_service mongod
            manage_service mongod start
            ;;
        DEBIAN)
            if [ ! -f "/etc/apt/sources.list.d/10gen.list" ]
            then
                cecho " > repository configuration not found. Adding 10 gen repository" yellow
                apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10 >> ${LOGFILE} 2>&1
                echo "deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen" > /etc/apt/sources.list.d/10gen.list
                cecho " > Updating repositories " yellow
                apt-get --allow-unauthenticated update >> ${LOGFILE} 2>&1
                cecho " > Installing mongodb server" green
                apt-get --allow-unauthenticated --force-yes install -y $MONGOPKGS >> ${LOGFILE} 2>&1
            else
                cecho " > 10gen repository found." green
                mongoexist=$(dpkg -l | grep "^ii" | grep "mongodb-10gen")
                if [ -z "$mongoexist" ]
                then
                    cecho " > Installing mongodb server" green
                    installpkg pkg $MONGOPKGS
                else
                    cecho " > Mongodb server already installed" green
                fi
            fi
            ;;
        *)
            cecho " > Unknown distribution: $DIST" red
            exit 2
            ;;
    esac
}

# nagvis

function install_nagvis(){
    cadre "Install nagvis addon" green
    manageparameters "quiet"
    HTTPDCONF="/etc/${WEBSERVER}/conf.d"
    HTTPDINIT="/etc/init.d/${WEBSERVER}"
    case $CODE in
        REDHAT|Fedora)
            cecho " > Installing prerequisites" green
            yum install -y $NAGVISYUMPKGS >> ${LOGFILE} 2>&1
            ;;
        DEBIAN)
            cecho " > Installing prerequisites" green
            DEBIAN_FRONTEND=noninteractive apt-get --allow-unauthenticated --force-yes install -y $NAGVISAPTPKGS >> ${LOGFILE} 2>&1
            ;;
        *)
            cecho " > Unknown distribution : $DIST" red
            exit 2
            ;;
    esac


    filename=$(echo $NAGVIS | awk -F"/" '{print $NF}')
    folder=$(echo $filename | sed -e "s/\.tar\.gz//g")

    cd $TMP

    if [ -d $TMP/$folder ]
    then
        rm -Rf $folder
    fi

    if [ ! -f $TMP/$filename ]
    then
        cecho " > Download $filename" green
        wget $WGETPROXY $NAGVIS >> ${LOGFILE} 2>&1
        if [ $? -ne 0 ]
        then
            cecho " > Error while downloading $NAGVIS" red
            exit 2
        fi
    fi
    cecho " > Extract archive content" green
    tar zxvf $filename >> ${LOGFILE} 2>&1
    cd $folder
    cecho " > Install nagvis" green
    ./install.sh -a y -q -F -l "tcp:localhost:50000" -i mklivestatus -n $TARGET -p $NAGVISPREFIX -u $HTTPDUSER -g $HTTPDGROUP -w $HTTPDCONF
    if [ -d $MKPREFIX ]
    then
        cread " > Found a check_mk multisite installation. Do you want to modify the nagvis url so links redirect to multisite?" yellow "y" "y n"
        if [ "$readvalue" == "y" ]
        then
            cecho " > Patching links for multisite use" green
            cd $NAGVISPREFIX/etc
            patch < "$myscripts"/install.d/nagvis.multisite.uri.patch >> ${LOGFILE} 2>&1
        fi
    fi
    cecho " > Restart Apache " green
    $HTTPDINIT restart >> ${LOGFILE} 2>&1
}

# mk multisite

function install_multisite(){
    cadre "Install check_mk addon" green
    manageparameters "quiet"
    cecho " > Installing prerequisites" green
    HTTPDINIT="/etc/init.d/${WEBSERVER}"
    HTTPDCONF="/etc/${WEBSERVER}"
    case $CODE in
        REDHAT|Fedora)
            cecho " -> Installing $p" green
            installpkg pkg $MKYUMPKG

            if [ -f /etc/selinux/config ]
            then
                if [ ! -z "$(cat /etc/selinux/config | grep "SELINUX=enforcing")" ]
                then
                    cecho " > WARNING: selinux is enabled with enforcing state. You should create a rule or disable selinux" yellow
                fi
            fi
            ;;
        DEBIAN)
            cecho " -> Installing $p" green
            installpkg pkg $MKAPTPKG
            ;;
        *)
            cecho " > Unsupported distro" red
            exit 2
            ;;
    esac


    usermod -s /bin/bash $HTTPDUSER >> ${LOGFILE} 2>&1
    usermod -a -G $HTTPDGROUP $SKUSER >> ${LOGFILE} 2>&1
    usermod -a -G $SKGROUP $HTTPDUSER >> ${LOGFILE} 2>&1

    cecho " > Configure response file" green
    cp install.d/check_mk_setup.conf.in $HOME/.check_mk_setup.conf
    sed -i "s#__PNPPREFIX__#$PNPPREFIX#g" $HOME/.check_mk_setup.conf
    sed -i "s#__MKPREFIX__#$MKPREFIX#g" $HOME/.check_mk_setup.conf
    sed -i "s#__SKPREFIX__#$TARGET#g" $HOME/.check_mk_setup.conf
    sed -i "s#__SKPREFIXETC__#$ETC#g" $HOME/.check_mk_setup.conf
    sed -i "s#__SKUSER__#$SKUSER#g" $HOME/.check_mk_setup.conf
    sed -i "s#__SKGROUP__#$SKGROUP#g" $HOME/.check_mk_setup.conf
    sed -i "s#__HTTPUSER__#"$HTTPDUSER"#g" $HOME/.check_mk_setup.conf
    sed -i "s#__HTTPGROUP__#"$HTTPDGROUP"#g" $HOME/.check_mk_setup.conf
    sed -i "s#__HTTPD__#/"$HTTPDCONF"#g" $HOME/.check_mk_setup.conf

    cd $TMP

    filename=$(echo $MKURI | awk -F"/" '{print $NF}')
    folder=$(echo $filename | sed -e "s/\.tar\.gz//g")
    if [ ! -f "$filename" ]
    then
        cecho " > Getting check_mk archive" green
        wget $WGETPROXY $MKURI >> ${LOGFILE} 2>&1
        if [ $? -ne 0 ]
        then
            cecho " > Error: the version of multisite provided could not be found" red
            cecho " > Go to http://mathias-kettner.de/check_mk.html and check the latest version" red
            cread " > Please type the correct version here (default: $MKVER)"  yellow "1.1.12p7" "nocheck"
            export MKVER=$readvalue
            export MKURI="http://mathias-kettner.de/download/check_mk-$MKVER.tar.gz"
            wget $WGETPROXY $MKURI >> ${LOGFILE} 2>&1
            if [ $? -ne 0 ]
            then
                cecho " > Error: the version of multisite provided could not be found" red
                exit 2
            fi
            filename=$(echo $MKURI | awk -F"/" '{print $NF}')
            folder=$(echo $filename | sed -e "s/\.tar\.gz//g")
        fi
    fi

    cecho " > Extracting archive" green
    if [ -d "$folder" ]
    then
        rm -Rf $folder
    fi
    tar zxvf $filename >> ${LOGFILE} 2>&1
    cd $folder

    # check if pnp4nagios is here if not move the resulting folder
    movepnp=0
    if [ -d "$PNPPREFIX" ]
    then
        movepnp=1
    fi

    # Fix sudo configuration in multisite installer script
    sed -i 's/\/etc\/sudoers/&.d\/shinken/' ./setup.sh

    cecho " > Install multisite" green
    ./setup.sh --yes >> ${LOGFILE} 2>&1

#    cread " > [ALPHA] Do you want to enable html form authentication for multisite? (y|N) =>  "  yellow "n" "y n"
#    if [ "$readvalue" == "y" ]
#    then
#        cecho " > Enable html form authentication " green
#        sed -i "/.*AuthType.*$/d" $HTTPDCONF/zzz_check_mk.conf > ${LOGFILE} 2>&1
#    fi

    # include check_mk configuration folder in nagios.cfg
    exist=$(cat $ETC/nagios.cfg | grep check_mk.d)
    if [ -z "$exist" ]
    then
        echo "cfg_dir=$ETC/check_mk.d" >> $ETC/nagios.cfg
    fi

    cecho " > Default configuration for multisite" green
#    echo 'sites = {' >> $MKPREFIX/etc/multisite.mk
#    echo '   "default": {' >> $MKPREFIX/etc/multisite.mk
#    echo '    "alias":          "default",' >> $MKPREFIX/etc/multisite.mk
#    echo '    "socket":         "tcp:127.0.0.1:50000",' >> $MKPREFIX/etc/multisite.mk
#    echo '    "url_prefix":     "/",' >> $MKPREFIX/etc/multisite.mk
#    echo '   },' >> $MKPREFIX/etc/multisite.mk
#    echo ' }' >> $MKPREFIX/etc/multisite.mk
    cp  "$myscripts"/install.d/multisite.mk $MKPREFIX/etc/
    ip=$(ifconfig | grep "inet ad" | grep -v 127.0.0.1 | awk '{print $2}' | awk -F : '{print $2}' | head -n 1)
    sed -i "s#__HOSTADDRESS__#"$ip"#g" $MKPREFIX/etc/multisite.mk
    rm -Rf $ETC/check_mk.d/*
    chown -R $SKUSER:$SKGROUP $ETC/check_mk.d
    chmod -R g+rwx $ETC/check_mk.d
    chown -R $SKUSER:$SKGROUP $MKPREFIX/etc/conf.d
    chmod -R g+rwx $MKPREFIX/etc/conf.d
    $HTTPDINIT restart >> ${LOGFILE} 2>&1
#    if [ $movepnp -eq 1 ]
#    then
#        mv $PNPPREFIX $PNPPREFIX.MK
#    fi
}

# pnp4nagios
function install_pnp4nagios(){
    cadre "Install pnp4nagios addon" green
    manageparameters "quiet"

    if [ $SKIPPREREQUISITES -eq 1 ]
    then
        cecho " SKIPPREREQUISITES enabled: won't install pnp4nagios prerequisites" yellow
    else
        case $CODE in
            REDHAT|Fedora)
                cecho " > Installing prerequisites" green
                installpkg pkg $PNPYUMPKG
                ;;
            DEBIAN)
                cecho " > Installing prerequisites" green
                installpkg pkg $PNPAPTPKG
                ;;
            *)
                cecho " > Unsupported distro" red
                exit 2
                ;;
        esac
    fi

    cd $TMP

    filename=$(echo $PNPURI | awk -F"/" '{print $NF}')
    folder=$(echo $filename | sed -e "s/\.tar\.gz//g")
    if [ ! -f "$filename" ]
    then
        cecho " > Getting pnp4nagios archive" green
        wget $WGETPROXY $PNPURI >> ${LOGFILE} 2>&1
    fi

    cecho " > Extracting archive" green
    if [ -d "$folder" ]
    then
        rm -Rf $folder
    fi
    tar zxvf $filename >> ${LOGFILE} 2>&1
    cd $folder
    #cecho " > Enable mod rewrite for apache" green
    #a2enmod rewrite >> ${LOGFILE} 2>&1
    manage_service ${WEBSERVER} restart
    cecho " > Configuring source tree" green
    ./configure --prefix=$PNPPREFIX --with-nagios-user=$SKUSER --with-nagios-group=$SKGROUP >> ${LOGFILE} 2>&1
    cecho " > Building ...." green
    make all >> ${LOGFILE} 2>&1
    cecho " > Installing" green
    make fullinstall >> ${LOGFILE} 2>&1
    rm -f $PNPPREFIX/share/install.php

    cecho " > Fix htpasswd.users path" green
    sed -i "s#/usr/local/nagios/etc/htpasswd.users#$ETC/htpasswd.users#g" /etc/${WEBSERVER}/conf.d/pnp4nagios.conf
    manage_service ${WEBSERVER} restart
    cecho " > Enable npcd startup" green
    enable_service npcd

    cecho " > Enable npcdmod" green
    ip=$(ifconfig | grep "inet ad" | grep -v 127.0.0.1 | awk '{print $2}' | awk -F : '{print $2}' | head -n 1)
    cecho " > using ip address: $ip"
    do_skmacro enable_npcd.macro $PNPPREFIX/etc/npcd.cfg,$ip
    cecho " > Starting npcd" green
    manage_service npcd start
}

function uninstall_pnp4nagios(){
    cadre "Uninstall pnp4nagios addon" green
    manageparameters "quiet"

    if [ -d "$PNPPREFIX" ]
    then
        #first stop daemons
        cecho " > Stopping daemons " green
        manage_service ${WEBSERVER} stop
        manage_service npcd stop
        skill

        # remove pnp4nagios folder
        cecho " > Removing pnp4nagios" green
        rm -Rf $PNPPREFIX >> ${LOGFILE} 2>&1
        rm -Rf /etc/init.d/npcd >> ${LOGFILE} 2>&1

        # remove startup scripts
        cecho " > Removing pnp4nagios startup scripts" green
        disable_service npcd
        rm -Rf /etc/init.d/npcd >> ${LOGFILE} 2>&1

        # remove pnp4nagios shinken configuration
        cecho " > Removing NPCPDMOD module from broker configuration " green
        BROKERS=$(PYTHONPATH=$TARGET $PY $TARGET/install.d/tools/skonf.py -a getobjectnames -f  $TARGET/etc/shinken-specific.cfg -o broker)
        for o in $BROKERS
        do
            cecho " --> Removing from broker $o" green
            do_skmacro disable_npcd.macro $o
        done

        # start daemons
        cecho " > Starting daemons " green
        manage_service ${WEBSERVER} start
        sstart
    else
        cecho " > pnp4nagios folder was not found at: $PNPPREFIX" red
        exit 2
    fi
}


function install_mysql(){
    cecho " > Checking for mysql server" green
    case $CODE in
        REDHAT|Fedora)
            if [[ -z "$(rpm -qa | grep mysql-server)" ]]
            then
                cecho " --> mysql-server not found. Going to install" yellow
                installpkg pkg mysql-server
                enable_service mysqld
                manage_service mysqld start
                while true
                do
                    creads " --> Please provide a password for root user: " yellow
                    MYSQLP1=$readvalue
                    creads " --> Please confirm root password: " yellow
                    MYSQLP2=$readvalue
                    if [ "$MYSQLP1" != "$MYSQLP2" ]
                    then
                        cecho " --> passwords does not match" red
                    else
                        if [ -z "$MYSQLP1" ] || [ -z "$MYSQLP2" ]
                        then
                            cecho " --> password should not be empty" red
                        else
                            export MYSQLP=$MYSQLP2
                            mysql -h localhost -u root -e "grant all privileges on *.* to 'root'@'localhost' identified by '$MYSQLP'; flush privileges;"
                            break
                        fi
                    fi
                done
            fi
            ;;
        DEBIAN)
            if [ -z "$(dpkg -l | grep ^ii.*mysql-server)" ]
            then
                cecho " --> mysql-server not found. Going to install" yellow
                while true
                do
                    creads " --> Please provide a password for root user: " yellow
                    MYSQLP1=$readvalue
                    creads " --> Please confirm root password: " yellow
                    MYSQLP2=$readvalue
                    if [ "$MYSQLP1" != "$MYSQLP2" ]
                    then
                        cecho " --> passwords does not match" red
                    else
                        if [ -z "$MYSQLP1" ] || [ -z "$MYSQLP2" ]
                        then
                            cecho " --> password should not be empty" red
                        else
                            export MYSQLP=$MYSQLP2
                            break
                        fi
                    fi
                done
                MYVERS=$(apt-cache search mysql-server- | grep -v "core\|cluster" | grep "mysql-server-" | awk -F\- '{print $3}')

                echo "mysql-server-$MYVERS mysql-server/root_password password $MYSQLP" | debconf-set-selections
                echo "mysql-server-$MYVERS mysql-server/root_password_again password $MYSQLP" | debconf-set-selections
                echo "mysql-server-$MYVERS mysql-server/root_password seen true" | debconf-set-selections
                echo "mysql-server-$MYVERS mysql-server/root_password_again seen true" | debconf-set-selections
                installpkg pkg mysql-server mysql-client
            fi
            ;;
    esac

}

function remove_nconf(){
    case $CODE in
        REDHAT|Fedora)
            NCONFTARGET=/var/www/html/nconf
            ;;
        DEBIAN)
            NCONFTARGET=/var/www/nconf
            ;;
        *)
            cecho " > Unsupported distro" red
            exit 2
            ;;
    esac

    if [ -d "$NCONFTARGET" ]
    then
        rm -Rf $NCONFTARGET
        creads " --> Please provide a password for root mysql user: " yellow
        MYSQLP=$readvalue
        mysql -h localhost -u root -p$MYSQLP -e "drop database nconf;"
    fi
}

function install_nconf(){
    cadre "Install nconf addon" green

    # manageparameters "quiet"

    case $CODE in
        REDHAT|Fedora)
            NCONFTARGET=/var/www/html/nconf
            ;;
        DEBIAN)
            NCONFTARGET=/var/www/nconf
            ;;
        *)
            cecho " > Unsupported distro" red
            exit 2
            ;;
    esac

    if [ -d "$NCONFTARGET" ]
    then
        cecho "nconf is already installed!" red
        exit 2
    fi

    if [ $SKIPPREREQUISITES -eq 1 ]
    then
        cecho " SKIPPREREQUISITES enabled: won't install nconf prerequisites" yellow
    else
        case $CODE in
            REDHAT|Fedora)
                cecho " > Installing prerequisites" green
                installpkg pkg $NCONFYUMPKG
                ;;
            DEBIAN)
                cecho " > Installing prerequisites" green
                installpkg pkg $NCONFAPTPKG
                ;;
            *)
                cecho " > Unsupported distro" red
                exit 2
                ;;
        esac
    fi

    enable_service ${WEBSERVER}
    manage_service ${WEBSERVER} start

    install_mysql

    cd $TMP

    filename=$(echo $NCONF | awk -F"/" '{print $NF}')
    folder=$(echo $filename | sed -e "s/\.tgz//g")
    if [ ! -f "$filename" ]
    then
        cecho " > Getting nconf archive" green
        wget $WGETPROXY $NCONF >> ${LOGFILE} 2>&1
    fi

    cecho " > Extracting archive" green
    if [ -d "$folder" ]
    then
        rm -Rf $folder
    fi
    tar zxvf $filename >> ${LOGFILE} 2>&1

    cecho " > Deploy nconf" green

    cp -a nconf $NCONFTARGET

    cecho " > Change files owner" green
    chown -R $HTTPDUSER:$HTTPDGROUP $NCONFTARGET

    cecho " > create deployment folders" green
    mkdir -p $ETC/global
    mkdir -p $ETC/Default_collector
    chown $SKUSER:$HTTPDGROUP $ETC/global
    chown $SKUSER:$HTTPDGROUP $ETC/Default_collector
    chmod g+rwx $ETC/global
    chmod g+rwx $ETC/Default_collector

    cecho " > configure sudo for deployment (should not work with systemd)" green
    if [ -z "$(cat /etc/sudoers.d/shinken | grep shinken-arbiter)" ]
    then
        echo "$HTTPDUSER ALL=NOPASSWD: /etc/init.d/shinken-arbiter restart" >> /etc/sudoers.d/shinken
    fi

    cecho " -> Create nconf user and database" green
    while true
    do
        creads " --> Please provide a password for nconf mysql user: " yellow
        MYSQLP1=$readvalue
        creads " --> Please confirm nconf password: " yellow
        MYSQLP2=$readvalue
        if [ "$MYSQLP1" != "$MYSQLP2" ]
        then
            cecho " --> passwords does not match" red
        else
            if [ -z "$MYSQLP1" ] || [ -z "$MYSQLP2" ]
            then
                cecho " --> password should not be empty" red
            else
                export NCONFP=$MYSQLP2
                break
            fi
        fi
    done

    if [ -z "$MYSQLP" ]
    then
        creads " --> Please provide a password for root mysql user: " yellow
        MYSQLP=$readvalue
    fi
    mysql -h localhost -u root -p$MYSQLP -e "create database if not exists nconf;"
    mysql -h localhost -u root -p$MYSQLP -e "grant all privileges on nconf.* to 'nconf'@'localhost' identified by '$NCONFP';flush privileges;"


    cecho " > Patching nconf for shinken support" green
    cd $NCONFTARGET/include/ajax
    patch < "$myscripts"/install.d/exec_generate_config.php.patch >> ${LOGFILE} 2>&1

    cecho " > Configure deployment.ini file" green
    cp -f "$myscripts"/install.d/deployment.ini.in $NCONFTARGET/config/deployment.ini
    sed -i "s#__SHINKENTARGET__#"$TARGET"#g" $NCONFTARGET/config/deployment.ini
    sed -i "s#__NCONFTARGET__#"$NCONFTARGET"#g" $NCONFTARGET/config/deployment.ini
    sed -i "s#__SHINKENETC__#"$ETC"#g" $NCONFTARGET/config/deployment.ini

    cecho " > Alter nagios.cfg config file" green
    sed -i "s/^cfg_dir.*$/#&/g" $TARGET/etc/nagios.cfg
    sed -i "s/^cfg_file.*$/#&/g" $TARGET/etc/nagios.cfg
    echo "cfg_dir=global" >> $TARGET/etc/nagios.cfg
    echo "cfg_dir=Default_collector" >> $TARGET/etc/nagios.cfg

    # restart the webserver for dependencies
    enable_service ${WEBSERVER}
    manage_service ${WEBSERVER} restart

    # automaticaly fill default response
    cecho " > Set default answers on web installer" green
    sed -i "s#\$nagios_bin =.*#\$nagios_bin = \""$TARGET"/bin/nagios\";#g" $NCONFTARGET/INSTALL.php
    sed -i "s#\"database_name\"#\"nconf\"#g" $NCONFTARGET/INSTALL.php
    sed -i "s#\"user_name\"#\"nconf\"#g" $NCONFTARGET/INSTALL.php
    sed -i "s#\"password\",#\""$NCONFP"\",#g" $NCONFTARGET/INSTALL.php

    cecho " > Nconf is installed, you must go to http://yourhost/nconf and finalize installation" yellow
    cecho " > mysql credentials and database are:" yellow
    cecho " --> user: nconf" yellow
    cecho " --> password: $NCONFP" yellow
    cecho " --> database: nconf" yellow
    cecho " > The answer for NAGIOS_BIN at step 'GENERAL CONFIGURATION is': $TARGET/bin/nagios" yellow
    cecho " > Hit ENTER when you are done with this step" red
    read

    rm -Rf $NCONFTARGET/INSTALL* $NCONFTARGET/UPDATE*

}

function do_skmacro(){
    macro=$1
    args=$2
    export PYTHONPATH="$TARGET"
    export SHINKEN="$PYTHONPATH"
    export SKTOOLS="$PYTHONPATH/install.d/tools"

    $PY $SKTOOLS/skonf.py -a macros -f $SKTOOLS/macros/$macro -d $args  >> ${LOGFILE} 2>&1


}


function install_notify_by_xmpp(){
    XMPP_CONFIG_FILE="$LIBEXEC/notify_by_xmpp.ini"
    cadre "Install notifiy_by_xmpp addon" green

    cecho " > Installing python-xmpp" green
    installpkg pkg python-xmpp

    cecho " > Configuring the XMPP account that will be used to send the notifications" green
    cecho " -> The credentials will be written in plain text in $XMPP_CONFIG_FILE" yellow
    while true
    do
        creads " -> Please provide the XMPP address (for ex. monitoring@jabber.org):" yellow
        XMPP_ADDRESS=$readvalue
        if [ -z "$XMPP_ADDRESS" ]
        then
            cecho " --> the address should not be empty" red
        else
            if [[ $XMPP_ADDRESS =~ @ ]]
            then
                XMPP_ACCOUNT=$(echo $XMPP_ADDRESS | cut -d@ -f1)
                XMPP_SERVER=$(echo $XMPP_ADDRESS | cut -d@ -f2)
                break
            else
                cecho " --> the adress should be like account_name@server_name" red
            fi
        fi
    done

    while true
    do
        creads " -> Please provide the password of this account:" yellow
        XMPP_PASSWORD_1=$readvalue
        creads " -> Please confirm the password:" yellow
        XMPP_PASSWORD_2=$readvalue
        if [ -z "$XMPP_PASSWORD_1" ] || [ -z "$XMPP_PASSWORD_2" ]
        then
            cecho " --> passwords should not be empty" red
        else
            if [ "$XMPP_PASSWORD_1" != "$XMPP_PASSWORD_2" ]
            then
                cecho " --> passwords does not match" red
            else
                break
            fi
        fi
    done

    cecho " > Writing credentials in $XMPP_CONFIG_FILE" green
    sed -i "s/^server=.*$/server=$XMPP_SERVER/g" $XMPP_CONFIG_FILE
    sed -i "s/^#username=.*$/username=$XMPP_ACCOUNT/g" $XMPP_CONFIG_FILE
    sed -i "s/^#password=.*$/password=$XMPP_PASSWORD_1/g" $XMPP_CONFIG_FILE
}

# thruk
function install_thruk(){
    cadre "Install thruk" green
    cd "$myscripts"
    case $CODE in
        REDHAT)
            if [ ! -f "/etc/yum.repos.d/consol-labs.repo" ]
            then
                case $VERS in
                    5)
                        cp install.d/consol-labs.rhel5.repo.in /etc/yum.repos.d/consol-labs.repo
                        ;;
                    6)
                        cp install.d/consol-labs.rhel6.repo.in /etc/yum.repos.d/consol-labs.repo
                        ;;
                    *)
                        cecho " > Unsupported RedHat/CentOs version" red
                        exit 2
                        ;;
                esac
                yum clean all >> ${LOGFILE} 2>&1
                cecho " > Installing thruk" green
                installpkg pkg $THRUKPKGS
                cp install.d/thruk_local.conf.in /etc/thruk/thruk_local.conf
                cecho " > Enable and start thruk" green
                enable_service thruk
                manage_service thruk start
                manage_service ${WEBSERVER} restart
            else
                # check if thruk is installed
                exist=$(rpm -qa | grep thruk)
                if [[ -z "$exist" ]]
                then
                    cecho " > Installing thruk" green
                    installpkg pkg $THRUKPKGS
                    install -b install.d/thruk_local.conf.in /etc/thruk/thruk_local.conf
                    cecho " > Enable and start thruk" green
                    enable_service thruk
                    manage_service thruk start
                    manage_service ${WEBSERVER} restart
                else
                    cecho "Already installed" yellow
                fi
            fi
            ;;
        DEBIAN)
            if [[ "$VERS" = "6" ]]; then
                if [ ! -f "/etc/apt/sources.list.d/consol-labs.list" ]
                then
                    cecho " > repository configuration not found. Adding consol-labs repository" yellow
                    apt-key adv --keyserver keys.gnupg.net --recv F8C1CA08A57B9ED7 >> ${LOGFILE} 2>&1
                    echo "deb http://labs.consol.de/repo/stable/debian squeeze main" > /etc/apt/sources.list.d/consol-labs.list
                    cecho " > Updating repositories " yellow
                    apt-get --allow-unauthenticated update >> ${LOGFILE} 2>&1
                    cecho " > Installing thruk" green
                    apt-get --allow-unauthenticated --force-yes install -y $THRUKPKGS >> ${LOGFILE} 2>&1
                else
                    cecho " > console-labs repository found." green
                    thrukexist=$(dpkg -l | grep "^ii" | grep "thruk")
                    if [ -z "$thrukexist" ]
                    then
                        cecho " > Installing thruk" green
                        installpkg pkg $THRUKPKGS
                        install -b install.d/thruk_local.conf.in /etc/thruk/thruk_local.conf
                        manage_service ${WEBSERVER} restart
                    else
                        cecho " > Thruk already installed" green
                    fi
                fi
            else
                cecho " > Unsupported Debian version" red
                exit 2
            fi
            ;;
        *)
            cecho " > Unknown distribution: $DIST" red
            exit 2
            ;;
    esac
}

#################################
### PLUGINS INSTALLATION PART ###
#################################

# CUCUMBER
function install_cucumber(){
    if [[ "$CODE" == "REDHAT" ]]
    then
        if [[ ! -f /etc/yum.repos.d/dag.repo ]]
        then
            cecho " > Enable dag repository for ffmpeg" green
            cp "$myscripts"/install.d/dag.repo /etc/yum.repos.d
            yum clean all >> ${LOGFILE} 2>&1
        fi
        cecho " > Installing eue dependencies" green
        installpkg pkg $EUEYUMPKG
    elif [[ "$CODE" == "Fedora" ]]
    then
        cecho " > Installing eue dependencies" green
        installpkg pkg $EUEYUMPKG
    elif [[ "$CODE" == "DEBIAN" ]]
    then
        cecho " > Unsuported at the moment ...." red
        exit 2
    else
        cecho " > Unsupported distribution" red
        exit 2
    fi

    cecho " --> Installing ruby gems" green
    for g in $EUEGEMS
    do
        gem=$(echo $g | awk -F: '{print $1}')
        gemver=$(echo $g | awk -F: '{print $2}')
        cecho " ----> Installing gem ${gem} version ${gemver}" green
        gem install --no-ri --no-rdoc $gem --version $gemver >> ${LOGFILE} 2>&1
        if [[ $? -ne 0 ]]
        then
            cecho " ------> There was a problem installing gem ${gem} version ${gemver}" red
        fi
    done

    cecho " --> Alter max_ouput_length for webui" green
    do_skmacro set_webui_ouptput_length.macro 200

    cecho " --> Deploy example eue service" green
    IP=$(ifconfig  | grep adr | awk -F: '{print $2}' | awk '{print $1}' | head -n 1)
    install -b -g $SKGROUP -o $SKUSER -m 0664 $TARGET/etc/sample/services/eue_glpi.cfg $TARGET/etc/services
    install -b -g $SKGROUP -o $SKUSER -m 0664 $LIBEXEC/eue/glpi.ini.in $LIBEXEC/eue/glpi.ini
    sed -i "s/@@IPWEBUI@@/"$IP"/g" $LIBEXEC/eue/glpi.ini

    cecho " --> restart shinken" green
    skill
    sstart
}




# CAPTURE_PLUGIN
function install_capture_plugin(){
    cadre "Install capture_plugin" green
    cd $TMP
    cecho " > Getting capture_plugin" green
    wget $WGETPROXY $CAPTURE_PLUGIN >> ${LOGFILE} 2>&1
    cecho " > Installing capture_plugin" green
    install -b -g $SKGROUP -o $SKUSER -m 0775 capture_plugin.txt $LIBEXEC/capture_plugin
}

# CHECK_HPASM
function install_check_hpasm(){

    cadre "Install check_hpasm plugin from consol.de" green
    cecho "You must install hpasm and or hp snmp agents on the monitored servers" yellow
    read taste

    case $CODE in
        REDHAT|Fedora)
            cecho " > Installing prerequisites" green
            installpkg pkg $CHECKHPASMYUMPKGS
            ;;
        DEBIAN)
            cecho " > Installing prerequisites" green
            installpkg pkg $CHECKHPASMAPTPKGS
            ;;
        *)
            cecho " > Unsupported distro" red
            exit 2
            ;;
    esac

    if [ $? -ne 0 ]
    then
        cecho " > Error while installing prerequisites" red
        exit 2
    fi

    cd $TMP

    archive=$(echo $CHECKHPASM | awk -F/ '{print $NF}')
    folder=$(echo $archive | sed -e 's/\.tar\.gz//g')

    if [ ! -f "$TMP/$archive" ]
    then
        cecho " > Downloading check_hpasm" green
        wget $WGETPROXY $CHECKHPASM  >> ${LOGFILE} 2>&1
        if [ $? -ne 0 ]
        then
            cecho " > Error while downloading check_hpasm" red
            exit 2
        fi
    fi
    cecho " > Extract archive content" green
    tar zxvf $archive >> ${LOGFILE} 2>&1
    cd $folder
    cecho " > Configure source tree" green
    ./configure >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while configuring source tree" red
        exit 2
    fi
    cecho " > Build" green
    make  >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while building" red
        exit 2
    fi
    cecho " > Installing check_hpasm" green
    install -b -g $SKGROUP -o $SKUSER -m 0775 plugins-scripts/check_hpasm $LIBEXEC/ >> ${LOGFILE} 2>&1
}

# CHECK_MONGODB
function install_check_mongodb(){

    cadre "Install check_mongodb plugin from Mike Zupan" green

    case $CODE in
        REDHAT|Fedora)
            if [ ! -z "$CHECKMONGOYUMPKG" ]
            then
                cecho " > Installing prerequisites" green
                installpkg pkg $CHECKMONGOYUMPKS
            fi
            ;;
        DEBIAN)
            if [ ! -z "$CHECKMONGOAPTPKG" ]
            then
                cecho " > Installing prerequisites" green
                installpkg pkg $CHECKMONGOAPTPKG
            fi
            ;;
    esac

    if [ $? -ne 0 ]
    then
        cecho " > Error while installing prerequisites" red
        exit 2
    fi

    cd $TMP

    if [ ! -f "$TMP/check_mongodb.py" ]
    then
        cecho " > Downloading check_mongodb.py" green
        wget $WGETPROXY $CHECKMONGO  >> ${LOGFILE} 2>&1
        if [ $? -ne 0 ]
        then
            cecho " > Error while downloading check_mongodb.py" red
            exit 2
        fi
    fi

    cecho " > Installing check_mongodb.py" green
    install -b -g $SKGROUP -o $SKUSER -m 0775 check_mongodb.py $LIBEXEC/check_mongodb.py
}

# Add two plugins to check IBM DS san devices.
function install_IBM_DS() {
    cadre "Install check_IBM_DS_health.sh and check_IBM_DS_performance.pl plugins" green

    cd $TMP

    SMcli_bin=$(which SMcli)
    if [ $? -ne 0 ]
    then
        cecho "You do not have Storage Manager installed. Go on:" red
        cecho "http://www-933.ibm.com/support/fixcentral/swg/selectFixes?parent=Entry-level+disk+systems&product=ibm/Storage_Disk/DS3500&release=All&platform=All&function=all#Storage%20Manager" red
        cecho "to download Storage Manager for Linux according to your arch" red
        exit 2
    else
        cecho "Downloading plugins..." green
        wget $WGETPROXY -O check_IBM_DS_health.sh $CHECKIBMDSHEALTH >> ${LOGFILE} 2>&1
        wget $WGETPROXY -O check_IBM_DS_performance.pl $CHECKIBMDSPERF >> ${LOGFILE} 2>&1
    fi

    cecho "Installing plugins..." green
    install -b -g $SKGROUP -o $SKUSER -m 0775 -t $LIBEXEC check_IBM_DS_health.sh check_IBM_DS_performance.pl >> ${LOGFILE} 2>&1

    cadre "Successfully installed IBM DS san monitoring plugins." green
}

function install_IBM_plugins() {
    cadre "Install check_snmp_aixVGstate.pl, check_snmp_HACMP.pl, check_snmp_Safekit.pl and check_snmp_systemHealth.pl" green

    cd $TMP

    cecho "Downloading plugins..." green
    wget $WGETPROXY $CHECKIBM >> ${LOGFILE} 2>&1

    cecho "Installing plugins..." green
    install -b -g $SKGROUP -o $SKUSER -m 0775 -t $LIBEXEC check_snmp_systemHealth.pl check_snmp_aixVGstate.pl check_snmp_HACMP.pl check_snmp_Safekit.pl >> ${LOGFILE} 2>&1
}

function install_HPUX_plugins() {
    cadre "Install HPUX plugins" green

    cd $TMP

    cecho "Downloading plugins..." green
    wget $WGETPROXY $CHECKHPUX >> ${LOGFILE} 2>&1

    cecho "Installing plugins..." green
    install -b -g $SKGROUP -o $SKUSER -m 0775 -t $LIBEXEC check_snmp_hpux_storage.pl check_snmp_mem >> ${LOGFILE} 2>&1
}

# CHECK_LOGFILES
function install_check_logfiles(){

    cadre "Install check_logfiles plugin" green

    cd $TMP

    cecho " > Downloading check_logfiles" green
    wget $WGETPROXY $CHECKLOGFILES -O check_logfiles-3.5.3.2.tar.gz  >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while downloading check_logfiles" red
        exit 2
    fi
    cecho " > Extract check_logfiles" green
    rm -Rf check_logfiles-3.5.3.2 >> ${LOGFILE} 2>&1
    tar zxvf check_logfiles-3.5.3.2.tar.gz >> ${LOGFILE} 2>&1
    folder=$(ls -1 | grep "^check_logfiles")
    cd $folder
    cecho " > Build check_logfiles" green
    autoreconf >> ${LOGFILE} 2>&1
    ./configure --with-nagios-user=$SKUSER --with-nagios-group=$SKGROUP --prefix=$TARGET >> ${LOGFILE} 2>&1
    make >> ${LOGFILE} 2>&1
    cecho " > Install check_logfiles" green
    make install >> ${LOGFILE} 2>&1
}

# CHECK_NWC_HEALTH
function install_check_nwc_health(){

    cadre "Install check_nwc_health plugin" green

    case $CODE in
        REDHAT|Fedora)
            if [ ! -z "$CHECKNWCYUMPKG" ]
            then
                cecho " > Installing prerequisites" green
                installpkg pkg $CHECKNWCYUMPKG
            fi
            ;;
        DEBIAN)
           if [ ! -z "$CHECKNWCAPTPKG" ]
            then
                cecho " > Installing prerequisites" green
                installpkg pkg $CHECKNWCAPTPKG
            fi
            ;;
    esac

    if [ $? -ne 0 ]
    then
        cecho " > Error while installing prerequisites" red
        exit 2
    fi

    cd $TMP

    if [ ! -f "$TMP/master" ]
    then
        cecho " > Downloading check_nwc_health" green
        wget $WGETPROXY $CHECKNWC -O check_nwc_health.tar.gz  >> ${LOGFILE} 2>&1
        if [ $? -ne 0 ]
        then
            cecho " > Error while downloading check_wc" red
            exit 2
        fi
    fi
    cecho " > Extract check_nwc_health" green
    folder=$(ls -1 | grep "^lausser-check_nwc_health")
    if [ ! -z "$folder" ]
    then
        rm -Rf lausser-check_nwc_health* >> ${LOGFILE} 2>&1
    fi
    tar zxvf check_nwc_health.tar.gz >> ${LOGFILE} 2>&1
    folder=$(ls -1 | grep "^lausser-check_nwc_health")
    cd $folder
    cecho " > Build check_nwc_health.pl" green
    autoreconf >> ${LOGFILE} 2>&1
    ./configure --with-nagios-user=$SKUSER --with-nagios-group=$SKGROUP --enable-perfdata --enable-extendedinfo --prefix=$TARGET >> ${LOGFILE} 2>&1
    make >> ${LOGFILE} 2>&1
    cecho " > Install check_nwc_health.pl" green
    make install >> ${LOGFILE} 2>&1
}

# CHECK_EMC_CLARIION
function install_check_emc_clariion(){

    cadre "Install check_emc_clariion plugin from netways" green

    cecho " You will need the DELL/EMC Navisphere agent in order to use this
plugin. Ask your vendor to know how to get it." yellow
    cecho " You should also customize the navisphere agent path in the plugin" yellow
    read taste

    case $CODE in
        REDHAT|Fedora)
            if [ ! -z "$CHECKEMCYUMPKG" ]
            then
                cecho " > Installing prerequisites" green
                installpkg pkg $CHECKEMCYUMPKG
            fi
            ;;
        DEBIAN)
            if [ ! -z "$CHECKEMCAPTPKG" ]
            then
                cecho " > Installing prerequisites" green
                installpkg pkg $CHECKEMCAPTPKG
            fi
            ;;
    esac

    if [ $? -ne 0 ]
    then
        cecho " > Error while installing prerequisites" red
        exit 2
    fi

    cd $TMP

    if [ ! -f "$TMP/check_emc.zip" ]
    then
        cecho " > Downloading check_emc.zip" green
        wget $WGETPROXY $CHECKEMC  >> ${LOGFILE} 2>&1
        if [ $? -ne 0 ]
        then
            cecho " > Error while downloading check_emc.zip" red
            exit 2
        fi
    fi

    cecho " > Extract check_emc.zip" green
    if [ -d check_emc ]
    then
        rm -Rf check_emc
    fi
    unzip check_emc.zip >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while trying to extract check_emc.zip" red
        exit 2
    fi

    cecho " > Install check_emc_clariion.pl" green
    cd check_emc
    install -b -g $SKGROUP -o $SKUSER -m 0775 check_emc_clariion.pl $LIBEXEC/
}

# CHECK_ESX3
function install_check_esx3(){

    cadre "Install check_esx3 plugin from op5" green

    cecho " > Installing prerequisites" green
    case $CODE in
        REDHAT|Fedora)
            # because redhat package nagios-plugins-perl does not ship all files from Nagios::plugins
            yum install -y $NAGPLUGYUMPKGS  >> ${LOGFILE} 2>&1
            cd $TMP
            wget $WGETPROXY $NAGPLUGPERL >> ${LOGFILE} 2>&1
            tar zxvf $(echo $NAGPLUGPERL | awk -F "/" '{print $NF}') >> ${LOGFILE} 2>&1
            cd  $(echo $NAGPLUGPERL | awk -F "/" '{print $NF}' |sed -e "s/\.tar\.gz//g") >> ${LOGFILE} 2>&1
            perl Makefile.PL >> ${LOGFILE} 2>&1
            make >> ${LOGFILE} 2>&1
            if [ $? -ne 0 ]
            then
                cecho " > Error while building Nagios::Plugins perl modules" red
                exit 2
            fi
            make install >> ${LOGFILE} 2>&1
            yum install -y $VSPHERESDKYUMPKGS  >> ${LOGFILE} 2>&1
            ;;
        DEBIAN)
            installpkg pkg $VSPHERESDKAPTPKGS
            ;;
    esac
    cd $TMP

    # workaround arch naming
    case $ARCH in
        i686)
            MARCH=i386
            ;;
        i586)
            MARCH=i386
            ;;
        *)
            MARCH=$ARCH
            ;;
    esac

    VSPHERESDK=$(echo $VSPHERESDK | sed -e "s/$ARCH/$MARCH/g")

    if [ ! -f "$TMP/VMware-vSphere-SDK-for-Perl-$VSPHERESDKVER.$MARCH.tar.gz" ]
    then
        cecho " > Downloading VSPHERE SPHERE SDK FOR PERL" green
        wget $WGETPROXY $VSPHERESDK  >> ${LOGFILE} 2>&1
        if [ $? -ne 0 ]
        then
            cecho " > Error while downloading vsphere sdk for perl" red
            exit 2
        fi
    fi

    cecho " > Extracting vsphere sdk for perl" green
    if [ -d "$TMP/vmware-vsphere-cli-distrib" ]
    then
        cecho " > Removing old building folder" green
        rm -Rf vmware-vsphere-cli-distrib
    fi
    tar zxvf VMware-vSphere-SDK-for-Perl-$VSPHERESDKVER.$MARCH.tar.gz  >> ${LOGFILE} 2>&1
    cd vmware-vsphere-cli-distrib
    cecho " > Building vsphere sdk for perl" green
    perl Makefile.PL >> ${LOGFILE} 2>&1
    make >> ${LOGFILE} 2>&1
    cecho " > Installing vsphere sdk for perl" green
    make install >> ${LOGFILE} 2>&1

    cd $TMP
    cecho " > Getting check_esx3 plugin from op5" green
    wget $WGETPROXY $CHECK_ESX3_SCRIPT -O check_esx3.pl >> ${LOGFILE} 2>&1
    install -b -g $SKGROUP -o $SKUSER -m 0775 check_esx3.pl $LIBEXEC/check_esx3.pl

    cecho " > Getting check_disk_vcenter plugin" green
    wget $WGETPROXY $CHECK_DISK_VCENTER_SCRIPT -O check_disk_vcenter.pl >> ${LOGFILE} 2>&1
    install -b -g $SKGROUP -o $SKUSER -m 0775 check_disk_vcenter.pl $LIBEXEC/check_disk_vcenter.pl
    cecho " > Getting check_vmware_snaphosts.pl" green
    wget $WGETPROXY $CHECKVMWARESNAPSHOTS >> ${LOGFILE} 2>&1
    tar xzvf check_vmware_snapshots_0.13.tar.gz >> ${LOGFILE} 2>&1
    install -b -g $SKGROUP -o $SKUSER -m 0775 -t $LIBEXEC check_vmware_snapshots.pl >> ${LOGFILE} 2>&1
}

# NAGIOS-PLUGINS
function install_nagios-plugins(){
    cadre "Install nagios plugins" green

    cecho " > Installing prerequisites" green
    case $CODE in
        REDHAT|Fedora)
            installpkg pkg $NAGPLUGYUMPKG
            ;;
        DEBIAN)
            DEBIAN_FRONTEND=noninteractive apt-get -y install $NAGPLUGAPTPKG >> ${LOGFILE} 2>&1
            ;;
    esac

    cd $TMP
    if [ ! -f "nagios-plugins-$NAGPLUGVERS.tar.gz" ]
    then
        cecho " > Getting nagios-plugins archive" green
        wget $WGETPROXY $NAGPLUGBASEURI >> ${LOGFILE} 2>&1
    fi
    cecho " > Extract archive content " green
    rm -Rf nagios-plugins-$NAGPLUGVERS
    tar zxvf nagios-plugins-$NAGPLUGVERS.tar.gz >> ${LOGFILE} 2>&1
    cd nagios-plugins-$NAGPLUGVERS
    cecho " > Patching nagios-plugins" green
    patch -p0 < "$myscripts"/install.d/nagios-plugins-gets-patch.patch >> ${LOGFILE} 2>&1
    cecho " > Configure source tree" green
    ./configure --with-nagios-user=$SKUSER --with-nagios-group=$SKGROUP --enable-libtap --enable-extra-opts --prefix=$TARGET --enable-perl-modules >> ${LOGFILE} 2>&1
    cecho " > Building ...." green
    make >> ${LOGFILE} >> ${LOGFILE}  2>&1
    cecho " > Installing" green
    make install >> ${LOGFILE} >> ${LOGFILE}  2>&1
    #if [ $? -ne 0 ]
    #then
    #    cecho " > Error while installing nagios-plugins" red
    #    exit 2
    #fi
    install -b -g $SKGROUP -o $SKUSER -m 0775 contrib/check_mem.pl $LIBEXEC >> ${LOGFILE} 2>&1
}

# MANUBULON SNMP PLUGINS
function install_manubulon(){
    cadre "Install manubulon plugins" green

    cecho " > Installing prerequisites" green
    case $CODE in
        REDHAT|Fedora)
            installpkg pkg $MANUBULONYUMPKG
            ;;
        DEBIAN)
            installpkg pkg $MANUBULONAPTPKG
            ;;
    esac
    cd $TMP

    # check if utils.pm is there
    if [ ! -f $LIBEXEC/utils.pm ]
    then
        cecho " > Unable to find utils.pm. You should install nagios-plugins first (./shinken.sh -p nagios-plugins)" red
        exit 2
    fi

    archive=$(echo $MANUBULON | awk -F/ '{print $NF}')
    folder=nagios_plugins
    if [ ! -f "$archive" ]
    then
        cecho " > Getting manubulon archive" green
        wget $WGETPROXY $MANUBULON >> ${LOGFILE} 2>&1
    fi
    cecho " > Extract archive content " green
    if [ -d $folder ]
    then
        rm -Rf $folder
    fi
    tar zxvf $archive >> ${LOGFILE} 2>&1
    cd $folder
    cecho " > Relocate libs" green
    for s in $(ls -1 $TMP/$folder/*.pl)
    do
        cecho " => Processing $s" green
        sed -i "s#/usr/local/nagios/libexec#"$LIBEXEC"#g" $s
    done
    cecho " => Installing" green
    install -b -g $SKGROUP -o $SKUSER -m 0775 $TMP/$folder/*pl $LIBEXEC
}

# CHECK_WMI_PLUS
function install_check_wmi_plus(){
    cadre "Install check_wmi_plus" green

    cecho " > Installing prerequisites" green
    case $CODE in
        REDHAT|Fedora)
            installpkg pkg $WMICYUMPKG
            ;;
        DEBIAN)
            installpkg pkg $WMICAPTPKG
            ;;
    esac

    cd $TMP
    filename=$(echo $WMIC | awk -F"/" '{print $NF}')
    folder=$(echo $filename | sed -e "s/\.tar\.gz//g")
    if [ ! -f $filename ]
    then
        cecho " > Downloading wmic" green
        wget $WGETPROXY $WMIC >> ${LOGFILE} 2>&1
        if [ $? -ne 0 ]
        then
            cecho " > Error while downloading $VMIC" red
            exit 2
        fi
    fi
    if [ -d $folder ]
    then
        rm -Rf $folder
    fi
    cecho " > Extracting archive " green
    tar zxvf $filename >> ${LOGFILE} 2>&1
    cd $folder
    cecho " > Building wmic" green
    make >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while building wmic" red
        exit 2
    fi
    cecho " > Installing wmic" green
    install -b -g $SKGROUP -o $SKUSER -m 0775 Samba/source/bin/wmic $LIBEXEC
    cd $TMP
    cecho " > Downloading check_wmi_plus" green
    filename=$(echo $CHECKWMIPLUS | awk -F"/" '{print $NF}')
    folder=$(echo $filename | sed -e "s/\.tar\.gz//g")
    if [ ! -f "$filename" ]
    then
        wget $WGETPROXY $CHECKWMIPLUS >> ${LOGFILE} 2>&1
    fi
    cecho " > Extracting archive" green
    tar zxvf $filename >> ${LOGFILE} 2>&1
    cecho " > Installing plugin" green
    install -b -g $SKGROUP -o $SKUSER -m 0664 check_wmi_plus.conf.sample $LIBEXEC/check_wmi_plus.conf
    install -b -g $SKGROUP -o $SKUSER -m 0775 check_wmi_plus.pl $LIBEXEC/check_wmi_plus.pl
    install -g $SKGROUP -o $SKUSER -m 0775 -d $LIBEXEC/check_wmi_plus.d
    install -b -g $SKGROUP -o $SKUSER -m 0664 $TMP/check_wmi_plus.d/* $LIBEXEC/check_wmi_plus.d

    cecho " > configuring plugin" green
    sed -i "s#/usr/lib/nagios/plugins#"$LIBEXEC"#g" $LIBEXEC/check_wmi_plus.conf
    sed -i "s#/bin/wmic#"$LIBEXEC"/wmic#g" $LIBEXEC/check_wmi_plus.conf
    sed -i "s#/opt/nagios/bin/plugins#"$LIBEXEC"#g" $LIBEXEC/check_wmi_plus.pl
    sed -i "s#/usr/lib/nagios/plugins#"$LIBEXEC"#g" $LIBEXEC/check_wmi_plus.pl
}

# CHECK_ORACLE_HEALTH
function install_check_oracle_health(){
    cadre "Install nagios plugins" green

    if [ -z "$ORACLE_HOME" ]
    then
        cadre "WARNING YOU SHOULD INSTALL ORACLE INSTANT CLIENT FIRST!!!!" yellow
        cecho " > Download the oracle instant client there (basic AND sdk AND sqlplus): " yellow
        cecho " > 64 bits: http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html" yellow
        cecho " > 32 bits: http://www.oracle.com/technetwork/topics/linuxsoft-082809.html" yellow
        cecho " > Set the ORACLE_HOME environment variable (better to set it in the bashrc)" yellow
        cecho " > Set LD_LIBRARY_PATH to ORACLE_HOME (or better create a config file in /etc/ld.so.conf) then run ldconfig" yellow
        cecho " > press ENTER to continue or CTRL+C to abort" yellow
        exit 2
    fi

    cecho " > Installing prerequisites" green
    case $CODE in
        REDHAT|Fedora)
            installpkg pkg $CHECKORACLEHEALTHYUMPKG
            ;;
        DEBIAN)
            installpkg pkg $CHECKORACLEHEALTHAPTPKG
            ;;
    esac
    cecho " > Installing cpan prerequisites" green
    cd $TMP
    for m in $CHECKORACLEHEALTHCPAN
    do
        filename=$(echo $m | awk -F"/" '{print $NF}')
        if [ ! -f "$filename" ]
        then
            wget $WGETPROXY $m >> ${LOGFILE} 2>&1
            if [ $? -ne 0 ]
            then
                cecho " > Error while downloading $m" red
                exit 2
            fi
        fi
        tar zxvf $filename  >> ${LOGFILE} 2>&1
        cd $(echo $filename | sed -e "s/\.tar\.gz//g")
        perl Makefile.PL >> ${LOGFILE} 2>&1
        make >> ${LOGFILE} 2>&1
        if [ $? -ne 0 ]
        then
            cecho " > There was an error building module" red
            exit 2
        fi
        make install  >> ${LOGFILE} 2>&1
    done
    cd $TMP
    cecho " > Downloading check_oracle_health" green
    wget $WGETPROXY $CHECKORACLEHEALTH >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while downloading $filename" red
        exit 2
    fi
    cecho " > Extracting archive " green
    filename=$(echo $CHECKORACLEHEALTH | awk -F"/" '{print $NF}')
    tar zxvf $filename >> ${LOGFILE} 2>&1
    cd $(echo $filename | sed -e "s/\.tar\.gz//g")
    ./configure --prefix=$TARGET --with-nagios-user=$SKUSER --with-nagios-group=$SKGROUP --with-mymodules-dir=$LIBEXEC --with-mymodules-dyn-dir=$LIBEXEC --with-statefiles-dir=$TARGET/var$TMP >> ${LOGFILE} 2>&1
    cecho " > Building plugin" green
    make >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while building check_oracle_health module" red
        exit 2
    fi
    make check >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while building check_oracle_health module" red
        exit 2
    fi
    cecho " > Installing plugin" green
    make install >> ${LOGFILE} 2>&1
    mkdir -p ${VAR}${TMP} >> ${LOGFILE} 2>&1
}

# CHECK_DB2_HEALTH
function install_check_db2_health(){
    cadre "Install nagios plugins" green

    if [ -z "$DB2_HOME" ]
    then
        cadre "WARNING YOU SHOULD INSTALL DB2 DEV APP CLIENT!!!!" yellow
        cecho " > Download the db2 client see http://www.ibm.com/software/data/db2/udb/support: " yellow
        cecho " > Set the DB2_HOME environment variable (better to set it in the bashrc )" yellow
        cecho " > Follow any DB2 initialization prerequiresites" yellow
        cecho " > press ENTER to continue or CTRL+C to abort" yellow
        exit 2
    fi

    cecho " > Installing prerequisites" green
    case $CODE in
        REDHAT|Fedora)
            installpkg pkg $CHECKDB2HEALTHYUMPKG
            ;;
        DEBIAN)
            installpkg pkg $CHECKDB2HEALTHAPTPKG
            ;;
    esac
    cecho " > Installing cpan prerequisites" green
    cd $TMP
    for m in $CHECKDB2HEALTHCPAN
    do
        filename=$(echo $m | awk -F"/" '{print $NF}')
        if [ ! -f "$filename" ]
        then
            wget $WGETPROXY $m >> ${LOGFILE} 2>&1
            if [ $? -ne 0 ]
            then
                cecho " > Error while downloading $m" red
                exit 2
            fi
        fi
        tar zxvf $filename  >> ${LOGFILE} 2>&1
        cd $(echo $filename | sed -e "s/\.tar\.gz//g")
        perl Makefile.PL >> ${LOGFILE} 2>&1
        make >> ${LOGFILE} 2>&1
        if [ $? -ne 0 ]
        then
            cecho " > There was an error building module" red
            exit 2
        fi
        make install  >> ${LOGFILE} 2>&1
    done
    cd $TMP
    cecho " > Downloading check_db2_health" green
    wget $WGETPROXY $CHECKDB2HEALTH >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while downloading $filename" red
        exit 2
    fi
    cecho " > Extracting archive " green
    filename=$(echo $CHECKDB2HEALTH | awk -F"/" '{print $NF}')
    tar zxvf $filename >> ${LOGFILE} 2>&1
    cd $(echo $filename | sed -e "s/\.tar\.gz//g")
    ./configure --prefix=$TARGET --with-nagios-user=$SKUSER --with-nagios-group=$SKGROUP --with-mymodules-dir=$LIBEXEC --with-mymodules-dyn-dir=$LIBEXEC --with-statefiles-dir=$TARGET/var$TMP >> ${LOGFILE} 2>&1
    cecho " > Building plugin" green
    make >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while building check_db2_health module" red
        exit 2
    fi
    make check >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while building check_db2_health module" red
        exit 2
    fi
    cecho " > Installing plugin" green
    make install >> ${LOGFILE} 2>&1
    mkdir -p ${VAR}${TMP} >> ${LOGFILE} 2>&1
}

# CHECK_NETAPP2

function install_check_netapp2(){
    cadre "Install check_netapp2" green

    cecho " > Installing prerequisites" green
    case $CODE in
        REDHAT|Fedora)
            installpkg pkg $CHECKNETAPP2YUMPKGS
            ;;
        DEBIAN)
            installpkg pkg $CHECKNETAPP2APTPKGS
            ;;
    esac

    cd $TMP
    cecho " > Downloading check_netapp2" green
    wget $WGETPROXY -O check_netapp2 $CHECKNETAPP2 >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while downloading check_netapp2" red
        exit 2
    fi
    cecho " > Installing plugin" green
    # fuckin assholes that upload perl scripts edited with notepad or so
    perl -p -e 's/\r$//' < check_netapp2 > check_netapp2.pl
    install -b -g $SKGROUP -o $SKUSER -m 0775 check_netapp2.pl $LIBEXEC
    sed -i "s#/usr/local/nagios/libexec#"$LIBEXEC"#g" $LIBEXEC/check_netapp2.pl >> ${LOGFILE} 2>&1
}

    # CHECK_MEM
function install_check_mem(){
    cadre "Install check_mem" green
    cd $TMP
    if [ ! -f $TMP/check_mem.tar.gz ]
    then
        cecho " > Downloading check_mem" green
        wget -O check_mem.tar.gz $WGETPROXY $CHECKMEM >> ${LOGFILE} 2>&1
        if [ $? -ne 0 ]
        then
            cecho " > Error while downloading $filename" red
            exit 2
        fi
    else
        for f in $(ls -1 | grep "^justintime")
        do
            rm -Rf $f >> ${LOGFILE} 2>&1
        done
    fi
    cecho " > Extracting archive " green
    tar zxvf check_mem.tar.gz >> ${LOGFILE} 2>&1
    cecho " > Installing plugin" green
    folder=$(ls -1 | grep "^justintime")
    install -b -g $SKGROUP -o $SKUSER -m 0775 $folder/check_mem/check_mem.pl $LIBEXEC
}

# CHECK_MYSQL_HEALTH
function install_check_mysql_health(){
    cadre "Install check_mysql_health" green

    cecho " > Installing prerequisites" green
    case $CODE in
        REDHAT|Fedora)
            installpkg pkg $CHECKMYSQLHEALTHYUMPKG
            ;;
        DEBIAN)
            cecho " > Installing prerequisites" green
            installpkg pkg $CHECKMYSQLHEALTHAPTPKG
            ;;
    esac
    cd $TMP
    cecho " > Downloading check_mysql_health" green
    wget $WGETPROXY $CHECKMYSQLHEALTH >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while downloading $filename" red
        exit 2
    fi
    cecho " > Extracting archive " green
    filename=$(echo $CHECKMYSQLHEALTH | awk -F"/" '{print $NF}')
    tar zxvf $filename >> ${LOGFILE} 2>&1
    cd $(echo $filename | sed -e "s/\.tar\.gz//g")
    ./configure --prefix=$TARGET --with-nagios-user=$SKUSER --with-nagios-group=$SKGROUP --with-mymodules-dir=$LIBEXEC --with-mymodules-dyn-dir=$LIBEXEC --with-statefiles-dir=$TARGET/var$TMP >> ${LOGFILE} 2>&1
    cecho " > Building plugin" green
    make >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while building check_mysql_health module" red
        exit 2
    fi
    make check >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while building check_mysql_health module" red
        exit 2
    fi
    cecho " > Installing plugin" green
    make install >> ${LOGFILE} 2>&1
}

function install_check_snmp_bandwidth(){
    cadre "Install install_check_snmp_bandwidth" green

    cd $TMP

    filename=$(echo $CHECKSNMPBANDWIDTH | awk -F"/" '{print $NF}')
    script=$(echo $filename | sed -e "s/\.txt/\.sh/g")
    cecho " > Downloading check_snmp_bandwidth" green
    wget --no-check-certificate $WGETPROXY $CHECKSNMPBANDWIDTH >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while downloading $script" red
        exit 2
    fi

    cecho " > install check_snmp_bandwidth" green
    cp $filename $LIBEXEC/$script
    chmod +x $LIBEXEC/$script
    chown $SKUSER:$SKGROUP $LIBEXEC/$script

    filename=$(echo $CHECKSNMPBANDWIDTH | awk -F"/" '{print $NF}')
    if [ -d "$PNPPREFIX" ]
    then
        cecho " > Downloading check_snmp_bandwidth pnp template" green
        wget --no-check-certificate $WGETPROXY $CHECKSNMPBANDWIDTHPNP >> ${LOGFILE} 2>&1
        if [ $? -ne 0 ]
        then
            cecho " > Error while downloading $filename" red
            exit 2
        fi
    fi

    cecho " > install check_snmp_bandwidth pnp template" green
    install -b -g $SKGROUP -o $SKUSER -m 0775 $filename $LIBEXEC/$script
}

function install_check_netint(){
    cadre "Install install_check_netint" green

    cd $TMP

    filename="check_netint.pl"
    cecho " > Installing pre-requisites." green
    case $CODE in
        REDHAT|Fedora)
            installpkg pkg perl-Net-SNMP
            ;;
        DEBIAN)
            installpkg pkg libnet-snmp-perl
            ;;
    esac

    cecho " > Downloading check_netint" green
    wget $WGETPROXY $CHECKNETINT -O ${filename} >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " > Error while downloading $script" red
        exit 2
    fi

    cecho " > install check_netint" green
    install -b -g $SKGROUP -o $SKUSER -m 0775 -t $LIBEXEC check_netint.pl
}

function install_check_rsync(){
    cadre "Install the rsync check plugin" green

    cd $TMP
    cecho " > Getting check_rsync" green
    wget $WGETPROXY "$CHECKRSYNC" -O check_rsync >> ${LOGFILE} 2>&1
    if [ $? -ne 0 ]
    then
        cecho " -> Error while downloading $script" red
        exit 2
    fi

    cecho " > Installing check_rsync" green
    install -b -g $SKGROUP -o $SKUSER -m 0775 check_rsync $LIBEXEC/check_rsync

    cecho " > Configuring check_rsync" green
    sed -i "s#/usr/local/nagios/libexec#"$LIBEXEC"#g" $LIBEXEC/check_rsync
}

function nconf-import-packs(){
    manageparameters 'quiet'
    packs=$TARGET/etc/packs

    # cecho "
    #     Some tips that are not curently implemented: \n
    #      - add an attribute called password to class service-template \n
    #      - edit nagiosadmin contact and replace nagiosadmin by admin and fill the password as you want \n
    #      - add an attribute called check_command to class service-template \n
    #      - add an attribute called host_name to class service-template \n

    #      > press ENTER to continue
    # " red

    # read fake

    case $CODE in
        REDHAT|Fedora)
            NCONFTARGET=/var/www/html/nconf
            ;;
        DEBIAN)
            NCONFTARGET=/var/www/nconf
            ;;
        *)
            cecho " > Unsupported distro" red
            exit 2
            ;;
    esac


    objects="macros commands templates"
    excludekw="databases\|storage\|virtualization\|network\|servers\|microsoft\|sample\|printers\|environmental"

    for o in $objects
    do
        cecho " --> Processing $o" green
        for domain in $(find $packs -type f -name $o.cfg | grep -v "$excludekw")
        do
            cecho "  ----> Processing $(echo $domain | sed -e "s#$packs##g" -e "s#/#->#g" -e "s#->macros\.cfg##g")" green
            case $o in
                macros)
                    for line in $(cat $domain | grep -v "^#")
                    do
                        if [ -z "$(cat $TARGET/etc/resource.cfg | grep $line)" ]
                        then
                            cecho " ------> Adding macros: $line" green
                            echo $line >> $TARGET/etc/resource.cfg
                        fi
                    done
                    ;;
                commands)
                    # nconf nedd a directive named command_param_count. so we prepare a file to add this argument
                    if [ -f /tmp/commands.nconf ]
                    then
                        rm -f /tmp/commands.nconf
                    fi
                    while read -r line
                    do
                        if [ ! -z "$(echo $line | grep command_line)" ]
                        then
                            num=$(echo $line  | awk '/ARG/ { count++ } END { print count}')
                            if [ -z $(echo $num | sed -e 's/\s//g' -e '/^$/d' ) ]
                            then
                                num=0
                            fi
                            echo "   command_param_count $num" >> /tmp/commands.nconf
                        fi
                        echo $line >> /tmp/commands.nconf
                    done < $domain
                    $NCONFTARGET/bin/add_items_from_nagios.pl -c checkcommand -f /tmp/commands.nconf 2>&1 | grep "ERROR\|WARN"
                    ;;
                templates)
                    # macros are not imported!!!!
                    # may be defining static files and position them inside
                    $NCONFTARGET/bin/add_items_from_nagios.pl -x 4 -c host-template -f $domain 2>&1 | grep "ERROR\|WARN"
                    ;;
            esac
        done
    done

    # cecho "  ----> Processing services templates" green


    # for file in $(find $TARGET/etc/packs -type f | grep -v "$excludekw" | grep services | grep -v templates.cfg | grep -v commands.cfg | grep -v sample)
    # do
    #     # strange: nconf need a directive called name to import services template
    #     cp $file $file.nconf
    #     sed -i "s/service_description\(.*\)$/name \1\n   service_description \1/g" $file.nconf
    #     # what we need to do is to create first a service template and then an advanced service linked to the template
    #     $NCONFTARGET/bin/add_items_from_nagios.pl -x 4 -c service-template -f $file.nconf 2>&1 | grep "ERROR\|WARN"
    #     rm -f $file.nconf
    # done
}

#     ___                                          __          ____                                 _
#    /   |  _________ ___  ______ ___  ___  ____  / /______   / __ \_________  ________  __________(_)___  ____ _
#   / /| | / ___/ __ `/ / / / __ `__ \/ _ \/ __ \/ __/ ___/  / /_/ / ___/ __ \/ ___/ _ \/ ___/ ___/ / __ \/ __ `/
#  / ___ |/ /  / /_/ / /_/ / / / / / /  __/ / / / /_(__  )  / ____/ /  / /_/ / /__/  __(__  |__  ) / / / / /_/ /
# /_/  |_/_/   \__, /\__,_/_/ /_/ /_/\___/_/ /_/\__/____/  /_/   /_/   \____/\___/\___/____/____/_/_/ /_/\__, /
#             /____/                                                                                    /____/

function usage(){
echo "Usage: install -k | -i | -u [addonname] | -b | -r backupname | -l | -c | -e \"daemonslist\" | -p pluginname | -a addonname
    -k | --kill             Kill shinken
    -i | --install          Install shinken
    -u | --uninstall        Remove shinken and all of the addons. if an argument is specified then just remove the specified argument
    -b | --backup           Backup shinken configuration plugins and data
    -r | --restore          Restore shinken configuration plugins and data
    -l | --listbackups      List shinken backups
    -c | --compresslogs     Compress rotated logs
    -e | --enabledaemons    Which daemons to keep enabled at boot time. It's a quoted list of shinken daemons that should be enabled at startup.
                            Daemons are:
                                arbiter
                                scheduler
                                poller
                                broker
                                reactionner
                                receiver
    -p | --plugin           Install plugins. Argument should be one of the following:
                               check_esx3
                               nagios-plugins
                               check_logfiles
                               check_oracle_health
                               check_mysql_health
                               check_db2_health
                               capture_plugin
                               check_wmi_plus
                               check_mongodb
                               check_emc_clariion
                               check_nwc_health
                               manubulon (snmp plugins)
                               check_hpasm
                               check_netapp2
                               check_mem (local enhanced memory check plugin)
                               check_snmp_bandwidth (check bandwidth usage with snmp)
                               check_netint (enhanced version of check_snmp_int plugins)
                               check_IBM
                               check_IBM_DS
                               check_HPUX
                               check_rsync
    -a | --addon            Install addons. Argument should be one of the following:
                               pnp4nagios
                               graphite
                               multisite
                               nagvis
                               mongodb
                               nconf (experimental)
                               notify_by_xmpp
                               thruk
    --nconf-import-packs    Import shinken packs into nconf (experimental). This require nconf to be installed
    --register              Register on community (experimental)
    --enableeue             Enable application performance monitoring stack (experimental)
    -h | --help             Show help"
}

function trapmsg(){
    # signal was caught
    echo
    cecho "An interupt signal was caught." red
    cecho "This script will keep running to not leave the system in an inconsistent state." red
    echo
}

# remove last install log file
if [ -f ${LOGFILE} ]
then
    rm -f ${LOGFILE}
fi

# should we use a proxy for downloading archives
if [ $USEPROXY -eq 1 ]
then
    export WGETPROXY=" -Y on "
fi

# options may be followed by one colon to indicate they have a required argument

if ! options=$(getopt -u -o kiu::br:lce:p:a:h -l register,nconf-import-packs,kill,install,uninstall::,backup,restore:,listbackups,compresslogs,enabledaemons:,plugin:,addon:,enableeue,help -- "$@")
then
    # something went wrong, getopt will put out an error message for us
    exit 2
fi


set -- $options

# catch ctrl-c
trap trapmsg INT TERM

while [ $# -gt 0 ]
do
    case $1 in
        -p|--plugin)
            shift
            OPTARG=$1
            shift
            manageparameters  "quiet"
            cecho " > checking if shinken is installed in $TARGET" green
            if [ $(shinken_exist) -eq 0 ]
            then
                cecho " > You should install shinken first! " red
                exit 2
            fi
            if [ "$OPTARG" == "check_esx3" ]
            then
                install_check_esx3
                exit 0
            elif [ "$OPTARG" == "nagios-plugins" ]
            then
                install_nagios-plugins
                exit 0
            elif [ "$OPTARG" == "check_mem" ]
            then
                install_check_mem
                exit 0
            elif [ "$OPTARG" == "check_logfiles" ]
            then
                install_check_logfiles
                exit 0
            elif [ "$OPTARG" == "check_oracle_health" ]
            then
                install_check_oracle_health
                exit 0
            elif [ "$OPTARG" == "check_mysql_health" ]
            then
                install_check_mysql_health
                exit 0
            elif [ "$OPTARG" == "check_db2_health" ]
            then
                install_check_db2_health
                exit 0
            elif [ "$OPTARG" == "capture_plugin" ]
            then
                install_capture_plugin
                exit 0
            elif [ "$OPTARG" == "check_wmi_plus" ]
            then
                install_check_wmi_plus
                exit 0
            elif [ "$OPTARG" == "check_mongodb" ]
            then
                install_check_mongodb
                exit 0
            elif [ "$OPTARG" == "check_emc_clariion" ]
            then
                install_check_emc_clariion
                exit 0
            elif [ "$OPTARG" == "check_nwc_health" ]
            then
                install_check_nwc_health
                exit 0
            elif [ "$OPTARG" == "manubulon" ]
            then
                install_manubulon
                exit 0
            elif [ "$OPTARG" == "check_hpasm" ]
            then
                install_check_hpasm
                exit 0
            elif [ "$OPTARG" == "check_netapp2" ]
            then
                install_check_netapp2
                exit 0
            elif [ "$OPTARG" == "check_snmp_bandwidth" ]
            then
                install_check_snmp_bandwidth
                exit 0
            elif [ "$OPTARG" == "check_netint" ]
            then
                install_check_netint
                exit 0
            elif [ "$OPTARG" == "check_IBM" ]
            then
                install_IBM_plugins
                exit 0
            elif [ "$OPTARG" == "check_IBM_DS" ]
            then
                install_IBM_DS
                exit 0
            elif [ "$OPTARG" == "check_HPUX" ]
            then
                install_HPUX_plugins
                exit 0
            elif [ "$OPTARG" == "check_rsync" ]
            then
                install_check_rsync
                exit 0
            fi
            ;;
        -a|--addon)
            shift
            OPTARG=$1
            shift
            manageparameters  "quiet"
            cecho " > checking if shinken is installed in $TARGET" green
            if [ $(shinken_exist) -eq 0 ]
            then
                cecho " > You should install shinken first! " red
                exit 2
            fi
            if [ "$OPTARG" == "pnp4nagios" ]
            then
                install_pnp4nagios
                exit 0
            elif [ "$OPTARG" == "graphite" ]
            then
                install_graphite
                exit 0
            elif [ "$OPTARG" == "multisite" ]
            then
                install_multisite
                exit 0
            elif [ "$OPTARG" == "nagvis" ]
            then
                install_nagvis
                exit 0
            elif [ "$OPTARG" == "mongodb" ]
            then
                install_mongodb
                exit 0
            elif [ "$OPTARG" == "nconf" ]
            then
                #cecho " > Not implemented yet"
                install_nconf
                exit 0
            elif [ "$OPTARG" == "notify_by_xmpp" ]
            then
                install_notify_by_xmpp
                exit 0
            elif [ "$OPTARG" == "thruk" ]
            then
                install_thruk
                exit 0
            else
                cecho " > Unknown addon $OPTARG" red
            fi
            ;;
        -e|--enabledaemons)
            shift
            OPTARG=$(echo "$*" |sed 's/--//g')
            shift
            setdaemons "$OPTARG"
            exit 0
            ;;
        -p|--purgelogs)
            purgeSQLITE
            exit 0
            ;;
        -k|--kill)
            skill
            exit 0
            ;;
        -i|--install)
            FROMSRC=1
            sinstall
            exit 0
            ;;
        -u|--uninstall)
            # check if there is an optional argument
            # if so just uninstall the specified addon
            shift
            shift
            OPTARG=$1
            if [ -z "$OPTARG" ] || [ "$OPTARG" == "--" ]
            then
                cread " > Are you sure you want to remove shinken? (y|N)." yellow "n" "y n"
                if [ "$readvalue" == "y" ]
                then
                    remove
                else
                    cecho " > Aborting uninstallation" yellow
                fi
                exit 0
            fi

            cread " > Are you sure you want to remove $OPTARG? (y|N)." yellow "n" "y n"
            if [ "$readvalue" == "y" ]
            then
                case $OPTARG in
                    pnp4nagios)
                        uninstall_pnp4nagios
                        exit 0
                        ;;
                    graphite)
                        uninstall_graphite
                        exit 0
                        ;;
                    nagvis)
                        cecho " > Not implemented yet" red
                        exit 2
                        ;;
                    multisite)
                        cecho " > Not implemented yet" red
                        exit 2
                        ;;
                    nconf)
                        remove_nconf
                        exit 0
                        ;;
                    *)
                        cecho "Unknown addon $OPTARG" red
                        exit 2
                        ;;
                esac
            else
                cecho " > Aborting uninstallation" yellow
            fi

            exit 0
            ;;
        -b|--backup)
            backup
            exit 0
            ;;
        -r|--restore)
            shift
            OPTARG=$1
            shift
            restore $OPTARG
            exit 0
            ;;
        -l|--listbackups)
            backuplist
            exit 0
            ;;
        -c|--compresslogs)
            compresslogs
            exit 0
            ;;
        -h|--help)
            usage
            exit 0
            ;;
        --nconf-import-packs)
            nconf-import-packs
            exit 0
            ;;
        --register)
            register
            exit 0
            ;;
        --enableeue)
            install_cucumber
            exit 0
            ;;
        (--)
            shift
            break
            ;;
        (-*)
            cecho "$0: error - unrecognized option $1" red 1>&2
            exit 2
            ;;
        (*)
            break
            ;;
    esac
    shift
done
usage
exit 0
