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

bashstyle_history () {
    case "${1}" in
        -D )
            local history_lines_matching
            local internal_counter
            local history_command

            shift
            history_command="${*}"

            if [ -n "${history_command}" ]; then
                history_lines_matching=$(builtin history | gawk -v pattern="^ .*[0-9]  ${history_command}" '$0 ~ pattern{print $1}')
                if [ -z "${history_lines_matching}" ]; then
                    echo "$(eval_gettext "no history entry matching ${history_command}")"
                else
                    internal_counter=0
                    for line in ${history_lines_matching}; do
                        builtin history -d $((line-internal_counter))
                        builtin history -w
                        internal_counter=$((internal_counter+1))
                    done
                fi
            else
                echo "$(eval_gettext "no command to delete given!")"
            fi
        ;;

        -g )
            shift
            builtin history | grep "${@}"
        ;;

        * )
            builtin history "${@}"
        ;;
    esac
}

if bt "$(ini_get history_isolate)"; then
    dbg_msg "$(eval_gettext "BashStyle-NG Setting:")" "$(eval_gettext "History Isolation")" "$(eval_gettext "On")"
    dbg_log unset HISTFILE
    dbg_log set +o history
    dbg_log enable -n history
else
    if [ -z "${BSNG_TMP_HISTFILE+x}" ]; then
        readonly BSNG_TMP_HISTFILE="${HISTFILE}.tmp.${BSNG_SESSION_TIME}_${BASH_SESSION_PID}"
        export BSNG_TMP_HISTFILE
        readonly BSNG_LOCKFILE="/run/user/${UID}/bashstyle-history-sync.lock"
        export BSNG_LOCKFILE
    fi

    erasehistorydups () {
        gawk '/^#/{if (x)print x;x="";}{x=(!x)?$0:x"HISTDILIMITER"$0;}END{print x;}' "${HISTFILE}" | \
            tac | gawk -F'HISTDILIMITER' '!x[$2]++' | \
            tac | sed -e 's/HISTDILIMITER/\n/g' > "${BSNG_TMP_HISTFILE}"
    }

    ignorehistorydups () {
        gawk '/^#/{if (x)print x;x="";}{x=(!x)?$0:x"HISTDILIMITER"$0;}END{print x;}' "${HISTFILE}" | \
            gawk -F'HISTDILIMITER' '!x[$2]++' | \
            sed -e 's/HISTDILIMITER/\n/g' > "${BSNG_TMP_HISTFILE}"
    }

    ignorehistoryspc () {
        gawk '/^#/{if (x)print x;x="";}{x=(!x)?$0:x"HISTDILIMITER"$0;}END{print x;}' "${HISTFILE}" | \
            sed -e '/HISTDILIMITER /d' > "${BSNG_TMP_HISTFILE}"
    }

    ignorehistoryboth () {
        gawk '/^#/{if (x)print x;x="";}{x=(!x)?$0:x"HISTDILIMITER"$0;}END{print x;}' "${HISTFILE}" | \
            gawk -F'HISTDILIMITER' '!x[$2]++' | \
            sed -e 's/HISTDILIMITER/\n/g' > "${BSNG_TMP_HISTFILE}"

        gawk '/^#/{if (x)print x;x="";}{x=(!x)?$0:x"HISTDILIMITER"$0;}END{print x;}' "${BSNG_TMP_HISTFILE}" | \
            sed -e '/HISTDILIMITER /d' > "${BSNG_TMP_HISTFILE}.tmp" && \
            mv "${BSNG_TMP_HISTFILE}.tmp" "${BSNG_TMP_HISTFILE}"
    }

    bashstyle_history_sync () {
        if [[ ! ${lastcommand} == history* ]]; then
            exec 9>>"${BSNG_LOCKFILE}"
            flock -x 9 || return 1

            builtin history -a

            case ${HISTCONTROL} in
                erasedups ) erasehistorydups ;;
                ignoredups) ignorehistorydups ;;
                ignorespace) ignorehistorspc ;;
                ignoreboth) ignorehistoryboth ;;
            esac

            if [ -s "${BSNG_TMP_HISTFILE}" ]; then
                mv "${BSNG_TMP_HISTFILE}" "${HISTFILE}"
            fi

            builtin history -c
            builtin history -r

            flock -u 9
            exec 9>&-
        fi
    }
fi
