#!/bin/bash

SSH_ENV="$HOME/.ssh/environment"
CLUS="/usr/local/bin/clus --config=/etc/clusrc.d/yandex-direct"
LIMTEST_LOCK_FILE="/var/www/ppc.yandex.ru/protected/monitor/limtest.locked"
alias direct-clus="$CLUS"
alias direct-db-config='zk-db-config -t dbtools_direct -c /etc/zk-delivery/ppc.cfg -n /direct/db-config.json'
alias direct-switchman-config='zk-db-config -t json -c /etc/zk-delivery/ppc.cfg -n /direct/switchman'
alias direct-sandbox-db-config='zk-db-config -c /etc/zk-delivery/sandbox-ppc.cfg -n /direct/sandbox-db-config.json'

complete -C "$CLUS --complete --" direct-clus

#function _start_agent {
#     echo "Initialising new SSH agent..."
#     /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
#     echo succeeded
#     chmod 600 "${SSH_ENV}"
#     . "${SSH_ENV}" > /dev/null
#     /usr/bin/ssh-add;
#}
#
## Source SSH settings, if applicable
#if [ -f "${SSH_ENV}" ]; then
#    . "${SSH_ENV}" > /dev/null
#    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
#        _start_agent;
#    }
#else
#    _start_agent;
#fi

function pause {
    perl -e 'use POSIX; $|++; $SIG{INT} = sub {print "\n"; exit}; my $t1 = time(); print "started at ".POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime)."\n"; while(1) {$e=time-$t1; printf "\relapsed: %02d:%02d:%02d", int($e/3600), int($e / 60) % 60, $e % 60; sleep 1}'
}
function direct-stop {
    $CLUS PERL webinterfaces stop
}
function direct-start {
    $CLUS PERL webinterfaces start
}

function set_clus_env {
    if [ -n "$FORCE" ]; then
        APT_GET_UPDATE=apt-get-update-force
    else
        APT_GET_UPDATE=apt-get-update
    fi
    if [ -n "$PARALLEL" ]; then
        LOCAL_CLUS="$CLUS --parallel --parallel-level=30"
    else
        LOCAL_CLUS="$CLUS"
    fi
    if [ -n "$CONFNEW" -a -n "$1" ]; then
        APT_GET_INSTALL="$1-confnew"
    elif [ -n "$1" ]; then
        APT_GET_INSTALL="$1"
    fi
}

function direct-up {
    if [ ! -n "$1" ]; then
        echo "Usage: direct-up VERSION";
        return 1;
    fi
    if [ -n "$2" ]; then
        hosts=$1
        version=$2
    else
        hosts=PERL
        version=$1
    fi
    set_clus_env apt-get-install-direct
    $CLUS --parallel --parallel-level=60 "$hosts" $APT_GET_UPDATE apt-get-install-d-direct "$version"
    $LOCAL_CLUS "$hosts" $APT_GET_INSTALL "$version" apache-reload nginx-reload js-templater-restart && date
}

function direct-up2 {
    if [ ! -n "$1" ]; then
        echo "Usage: direct-up2 [HOSTS] VERSION";
        return 1;
    fi
    if [ -n "$2" ]; then
        hosts=$1
        version=$2
    else
        hosts=PERL
        version=$1
    fi
    $CLUS --parallel --parallel-level=60 "$hosts" lexec 'rsync /usr/local/bin/direct-deploy.py %HOST:/tmp';
    $LOCAL_CLUS "$hosts" exec "python /tmp/direct-deploy.py direct '$version'"
}

function commander-up {
    if [ ! -n "$1" ]; then
        echo "Usage: commander-up VERSION";
        return 1;
    fi
    if [ -n "$2" ]; then
        hosts=$1
        version=$2
    else
        hosts=FRONTEND
        version=$1
    fi
    $CLUS $hosts apt-get-update apt-get-install-commander "$version" && date
}

function sandbox-up {
    if [ ! -n "$1" ]; then
        echo "Usage: sandbox-up VERSION";
        return 1;
    fi
    if [ -n "$2" ]; then
        hosts=$1
        version=$2
    else
        hosts=SANDBOX
        version=$1
    fi
    set_clus_env apt-get-install-sandbox
    $LOCAL_CLUS $hosts $APT_GET_UPDATE $APT_GET_INSTALL "$version" apache-reload fake-reload nginx-reload && date
}

function direct-up-all {
    tmux_session=$(echo "$@" | sed 's/[^a-zA-Z0-9]/_/g')
    tmux new-session -d -s "direct_$tmux_session" "bash -lc 'direct-up-parallel $@; exec bash -li'"
    tmux new-window -t "direct_$tmux_session" -n "sandbox.$tmux_session" "bash -lc 'PARALLEL=1 sandbox-up $@; exec bash -li'"
    tmux new-window -t "direct_$tmux_session" -n "new_limtest.$tmux_session" "bash -lc 'PARALLEL=1 limtest-up direct $@; exec bash -li'"
    tmux attach -t "direct_$tmux_session" \; select-window -t 0 \;
}
function direct-up-all2 {
    tmux_session=$(echo "$@" | sed 's/[^a-zA-Z0-9]/_/g')
    tmux new-session -d -s "direct_$tmux_session" "bash -lc 'direct-up-parallel2 $@; exec bash -li'"
    tmux new-window -t "direct_$tmux_session" -n "sandbox.$tmux_session" "bash -lc 'PARALLEL=1 sandbox-up $@; exec bash -li'"
    tmux attach -t "direct_$tmux_session" \; select-window -t 0 \;
}

function dna-up-all {
    version=$1
    if [ -z "$version" ]; then
        echo 'error: no version, usage: dna-up-all <version>'
        return 1
    fi

    shift

    is_downgrade_allowed=false
    while [[ $# -gt 0 ]]; do
        case $1 in -d|--downgrade)
            is_downgrade_allowed=true
            ;;
        esac
        shift
    done

    if $is_downgrade_allowed; then
        APT_GET_UPDATE=apt-get-update-force 
        APT_GET_INSTALL=apt-get-install-force-yes
    else
        APT_GET_UPDATE=apt-get-update
        APT_GET_INSTALL=apt-get-install
    fi
        
    tmux_session=$(printf %s "$version" | sed 's/[^a-zA-Z0-9]/_/g')
    tmux new-session -d -s "dna_$tmux_session" "set -x; $CLUS --parallel --parallel-level=60 FRONTEND $APT_GET_UPDATE $APT_GET_INSTALL yandex-direct-dna='$version'; exec bash -i"
    tmux new-window -t "dna_$tmux_session" -n "new_limtest.$tmux_session" "set -x; /usr/local/bin/limtest-up dna '$version'; exec bash -i"
    tmux attach -t "dna_$tmux_session" \; select-window -t 0 \;
}

function sandbox-up-confnew {
    PARALLEL=1 CONFNEW=1 sandbox-up $@
}
function direct-up-confnew {
    PARALLEL=1 CONFNEW=1 direct-up $@
}
function direct-up-parallel {
    PARALLEL=1 direct-up $*
}
function direct-up-parallel2 {
    PARALLEL=1 direct-up2 $*
}
function direct-pause {
    direct-stop
    pause
    direct-start
}
function direct-policy {
    sudo apt-get update
    apt-cache policy yandex-direct | less -F -X
}

function direct-limtest-lock {
    echo "This command is deprecated and working only for old limtest; use limtest-up"
    if [ ${#*} -ne 2 ] || [ "$1" != "limtest1" -a "$1" != "limtest2" ]; then
        echo "Usage: direct-limtest-lock limtest[12] VERSION";
        return 1;
    fi
    host="$1-direct"
    $CLUS "$host" exec "sudo rm -f $LIMTEST_LOCK_FILE"
    direct-up "$host" "$2"
    $CLUS "$host" exec "date >$LIMTEST_LOCK_FILE"
    $CLUS "$host" exec "sudo chown www-data $LIMTEST_LOCK_FILE"
}
function direct-limtest-unlock {
    echo "This command is deprecated and working only for old limtest; use limtest-up"
    if [ ${#*} -ne 1 ] || [ "$1" != "limtest1" -a "$1" != "limtest2" ]; then
        echo "Usage: direct-limtest-unlock limtest[12]";
        return 1;
    fi
    host="$1-direct"
    $CLUS "$host" exec "sudo rm $LIMTEST_LOCK_FILE"
    # get current version of direct
    version=`dpkg -l yandex-direct|awk '\$2=="yandex-direct" {print \$3}'`
    direct-up "$host" $version
}
