# -*- shell-script -*-

################################################################################

function §registry.add {

    local mode="$bashrun_mode"
    local widfile="$bashrun_cache_home/${mode}-wid"
    local pidfile="$bashrun_cache_home/${mode}-pid"
    
    # remove stale wid and pid files
    if [[ -f "$widfile" ]]; then	
	if [[ ! "$(brwctl "$(<$widfile)" class? 2>/dev/null)" =~ bashrun2-$mode ]];
	then
	    rm "$widfile"
	    rm "$pidfile"
	else
	    return 1
	fi
    fi 
    
    if [[ ! -f "$pidfile" ]]; then	
	printf '%s' "$$" > "$pidfile"
    fi

    if [[ ! -f "$widfile" ]]; then	
	printf '%s' "$WINDOWID" > "$widfile"
	return 0
    fi
    return 1
}

function §registry.remove {

    local widfile="$bashrun_cache_home/${bashrun_mode:-terminal}-wid"
    local pidfile="$bashrun_cache_home/${bashrun_mode:-terminal}-pid"

    if [[ -f "$pidfile" ]]; then
	rm "$pidfile"
    fi

    if [[ -f "$widfile" ]]; then
	rm "$widfile"
    fi

}


