#!/bin/bash
#########################################################
#                           #
# This is BashStyle-NG                  #
#                           #
# Licensed under GNU GENERAL PUBLIC LICENSE v3      #
#                           #
# Copyright Christopher Roy Bratušek            #
#                           #
#########################################################

host=${HOSTNAME/.*}

counter=0

_equinox_git_base_len=8
_show_editmode=$(ini_get show_editmode)

_equinox_counter () {
    if [[ ${counter} -eq 0 ]]; then
        counter=4
    else    counter=$((counter+2))
    fi
}

if [[ $(ini_get equinox_systemload) == True ]]; then

_equinox_counter
_equinox_sysload_pos=${counter}

_equinox_systemload () {
    echo -en "\033[${_equinox_sysload_pos};$((COLUMNS-29))H"
    echo -en "${ecolor_separator}(${ecolor_font} system-load: $(systemkit load1)${ecolor_separator} )────────┤"
}

fi

if [[ $(ini_get equinox_cpuload) == True ]]; then

_equinox_counter
_equinox_cpuload_pos=${counter}

_equinox_cpuload () {
    echo -en "\033[${_equinox_cpuload_pos};$((COLUMNS-29))H"
    echo -en "${ecolor_separator}(${ecolor_font} cpu-load: $(systemkit cpuload)${ecolor_separator} )────────────┤"
}

fi

if [[ $(ini_get equinox_ram) == True ]]; then

_equinox_counter
_equinox_ram_pos=${counter}

_equinox_ram () {
    echo -en "\033[${_equinox_ram_pos};$((COLUMNS-29))H"
    echo -en "${ecolor_separator}(${ecolor_font} ram: ${ecolor_separator}$(systemkit usedram)m${ecolor_font} / ${ecolor_separator}$(systemkit freeram)m )─────┤"
}

fi

if [[ $(ini_get equinox_proc) == True ]]; then

_equinox_counter
_equinox_proc_pos=${counter}

_equinox_proc () {
    echo -en "\033[${_equinox_proc_pos};$((COLUMNS-29))H"
    echo -en "${ecolor_separator}(${ecolor_font} processes:${ecolor_ps} $(systemkit processes) ${ecolor_separator})──────────┤"
}

fi

if [[ $(ini_get equinox_lastcmd) == True ]]; then

_equinox_counter
_equinox_lastcmd_pos=${counter}

_equinox_lastcmd () {
    echo -en "\033[${_equinox_lastcmd_pos};$((COLUMNS-29))H"
    case ${lastexit} in
        0)
            echo -en "${ecolor_separator}( ${egreen}${return_good}: ${lastcommandprintable} ${ecolor_separator})─┤"
        ;;
        141)
            # pressing 'q' in 'git log' results in ${lastexit} -eq 141, but it's not actually a failure (...)
            echo -en "${ecolor_separator}( ${eyellow}${return_other}: ${lastcommandprintable} ${ecolor_separator})─┤"
        ;;
        *)
            echo -en "${ecolor_separator}( ${ered}${return_bad}: ${lastcommandprintable} ${ecolor_separator})─┤"
        ;;
    esac
}

fi

if [[ $(ini_get equinox_git) == True ]]; then

_equinox_counter
_equinox_git_pos=${counter}

_equinox_git () {
    echo -en "\033[${_equinox_git_pos};$((COLUMNS-29))H"
    _branch=$(gitkit branch)
    _revision=$(gitkit revision)
    _action=$(gitkit action)
    let _fill=$((29 - ${_equinox_git_base_len} - ${#_branch} - ${#_revision} - ${#_action}))
    if [ "${_fill}" -lt 0 ]; then
        _branch=${branch:0:_fill}
    else
        git_fill=" "
        while [ "${_fill}" -gt "0" ]
        do
            git_fill="${git_fill} "
            let _fill=${_fill}-1
        done
    fi
    echo -en "${ecolor_separator}( ${egreen}${_branch}${ecolor_font}:${eyellow}${_revision}${ecolor_font}:${ered}${_action}${git_fill} ${ecolor_separator})─┤"
}

fi

# currently counter holds the line number of the last prompt part
# to draw. We need four more lines to make equinox properly work
counter=$((counter+4))

[[ ! ${dirchar} ]] && dirchar="/"
[[ ! ${trunc_symbol} ]] && trunc_symbol="«"
[[ ! ${trunc_length} ]] && trunc_length=1

function pre_prompt {

newPWD="${PWD}"

let promptsize=$(echo -n "--( $(whoami) @ ${host} )--( ${PWD} )-----" | wc -c | tr -d " ")

if check bt ${_show_editmode}; then
    _prompt_prefix=' '
    let fillsize=COLUMNS-${promptsize}-1
else
    _prompt_prefix=''
    let fillsize=COLUMNS-${promptsize}
fi
fill=""
while [ "${fillsize}" -gt "0" ]
do
    fill="${fill}─"
    let fillsize=${fillsize}-1
done

if [ "${fillsize}" -lt "0" ]
then
    let cutt=${trunc_length}-${fillsize}
    xPWD="${trunc_symbol}${PWD:${cutt}}"
    newPWD="${xPWD//\//${ecolor_separator}${dirchar}${ecolor_wdir}}"
else
    newPWD="${PWD//\//${ecolor_separator}${dirchar}${ecolor_wdir}}"
fi

_newPWD () {
    echo -e "${newPWD}"
}

echo -en "\033[2;$((COLUMNS-29))H"
echo -en "${ecolor_separator}(${ecolor_time} $(date +%H:%M)${ecolor_separator} :${ecolor_date} $(date '+%a, %d %b %y')${ecolor_separator} )────┐"
echo -en "\033[3;${COLUMNS}H${ecolor_separator}│"

local i=${LINES}

if [[ ${i} -ge ${counter} ]]; then
    while [ ${i} -ge 4 ]
    do
        case ${i} in
            ${_equinox_sysload_pos} )   _equinox_systemload ;;
            ${_equinox_cpuload_pos} )   _equinox_cpuload    ;;
            ${_equinox_ram_pos} )       _equinox_ram        ;;
            ${_equinox_proc_pos} )      _equinox_proc       ;;
            ${_equinox_lastcmd_pos} )   _equinox_lastcmd    ;;
            ${_equinox_git_pos} )       _equinox_git        ;;
            * )
                echo -en "\033[$((i));${COLUMNS}H${ecolor_separator}│"
            ;;
        esac
        let i=${i}-1
    done
else
    while [ ${i} -ge 4 ]
    do
        echo -en "\033[$((i));${COLUMNS}H${ecolor_separator}│"
        let i=${i}-1
    done
fi

let prompt_line=${LINES}-1
}

PROMPT_COMMAND=pre_prompt

if [[ $(ini_get equinox_uptime) == True ]]; then

PS1="\[\033[\${prompt_line};0H\]\n\
${color_separator}\${_prompt_prefix}┌─( ${color_user}\u ${color_font}@ ${color_host}\h \
${color_separator})─\${fill}─( ${color_wdir}\$(_newPWD)\
${color_separator} )────┘\n\
${color_separator}└─(${color_uptime} uptime: \$(systemkit uptime)\
${color_font} :${EQUINOX_1_COLOR}\$(EQUINOX_1)${color_font} \
\$(showuser) ${color_separator})·>${color_font} "

else

PS1="\[\033[\${prompt_line};0H\]\n\
${color_separator}\${_prompt_prefix}┌─( ${color_user}\u ${color_font}@ ${color_host}\h \
${color_separator})─\${fill}─( ${color_wdir}\$(_newPWD)\
${color_separator} )────┘\n\
${color_separator}└─(${EQUINOX_1_COLOR}\$(EQUINOX_1)${color_font} \
\$(showuser) ${color_separator})·>${color_font} "

fi

