#!/bin/bash

LANG=C

_repo=$1
_tobranch=$2
_package=$3
_version=$4
_frombranch=$5
_skip_reindex=$6

_logfile="/logs/current-dmove-console.log"
_search_url="http://localhost:1488/api/v1/search"

usage()
{
    echo "Usage dmove <repo> <tobranch> <package> <version> [frombranch]";
    echo "      tobranch, frombranch - unstable | testing | stable | prestable";
    echo "      repo - reponame (common, yandex-precise, verstka, etc)"
    echo
    exit 1;
}

dmove()
{
    echo $(date +"%Y-%m-%d %H:%M:%S"): "Moving package in Cacus"
    cacus dmove ${_repo} ${_tobranch} ${_package} ${_version} ${_frombranch} ${_skip_reindex}

    if [ $? -eq 0 ]; then
        echo $(date +"%Y-%m-%d %H:%M:%S"): "Dmove successfully complete"
        echo "$(date +%s) $(date): $SUDO_USER : ${_repo} ${_package} ${_version} ${_frombranch} -> ${_tobranch}" >> ${_logfile}
    else
        echo
        echo "$(date +"%Y-%m-%d %H:%M:%S"): ERROR: Oh, I found necessary package moment ago, but it just disappeared."
        echo "$(date +"%Y-%m-%d %H:%M:%S"):        Maybe another dmove?!"
        exit 34
    fi
}

function detect_package {
    _branch="$1"

     _url=$(echo "${_search_url}?pkg=${_package}&ver=${_version}&repo=${_repo}&env=${_branch}&strict=true&human=false" | sed 's@+@%2B@g')
     curl -f -s "${_url}" | grep '"source": "' > /dev/null

    _return=${PIPESTATUS[0]}

    if [ "${_branch}" == "${_frombranch}" ]; then
        if [ ${_return} -ne 0 ]; then
            echo -e "\n$(date +"%Y-%m-%d %H:%M:%S"): WARNING: Package ${_package}=${_version} not found in ${_branch}: ${_return}"
            return 1
        fi
    elif [ "${_branch}" == "${_tobranch}" ]; then
        if [ ${_return} -eq 0 ]; then
            echo -e "$(date +"%Y-%m-%d %H:%M:%S"): SUCCESS: Package ${_package}=${_version} already in ${_branch}\n"
            exit 0
        else
            echo -e "$(date +"%Y-%m-%d %H:%M:%S"): ERROR:   Package ${_package}=${_version} not found in ${_branch} too: ${_return}\n"
            exit 1
        fi
    fi
}

if [[ ("$(id -u)" != "0") && ("$USER" != "loadbase") ]]; then
    echo -e "\n$(date +"%Y-%m-%d %H:%M:%S"): ERROR: Only root cat dmove files.\n"
    exit 1;
fi

if [ "x${_package}" == "x" ]; then
    echo -e "\n$(date +"%Y-%m-%d %H:%M:%S"): ERROR: Package not defined.\n";
    usage
fi

if [ "x${_version}" == "x" ]; then
    echo -e "\n$(date +"%Y-%m-%d %H:%M:%S"): ERROR: Version not defined.\n";
    usage
fi

if [ "x${_frombranch}" == "x${_tobranch}" ]; then
    echo -e "\n$(date +"%Y-%m-%d %H:%M:%S"): ERROR: Destination cannot match source.\n";
    usage
fi

if [ "x${_frombranch}" == "x" ]; then
    if [ "x${_tobranch}" == "xstable" ]; then
        _frombranch=testing
    elif [ "x${_tobranch}" == "xtesting" ]; then
        _frombranch=unstable
    else
        echo -e "\n$(date +"%Y-%m-%d %H:%M:%S"): ERROR: Source Repo not defined.\n";
        usage
    fi

    echo -e "\n$(date +"%Y-%m-%d %H:%M:%S"): WARNING: Source repository not defined, using ${_frombranch} by default"
fi

detect_package ${_frombranch}

if [ $? -ne 0 ]; then
    detect_package ${_tobranch}
fi

dmove
