#!/bin/bash
set -a
#LC_ALL=C
#unset LC_CTYPE
#unset LC_MESSAGES
#unset LANG

# debug_pythonpath() {
# 	echo >&2 "$* PYTHONPATH=$PYTHONPATH"
# }

# We do NOT want CWD in the PYTHONPATH or PERL5LIB
PYTHONPATH=${PYTHONPATH#:}
PERL5LIB=${PERL5LIB#:}

export TERMINAL="xt"

export ANDROID_HOME=$HOME/Android/Sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools
# export JAVA_OPTS="-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee"

PATH="$HOME/.local/bin:$HOME/local/x:$HOME/x:$HOME/x/$HOSTNAME:$HOME/x/$(/usr/bin/arch):$HOME/my/x:/usr/local/go/bin:/opt/rust/bin:$PATH:$HOME/go/bin:$HOME/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/usr/X11/bin:$HOME/soft/bin:$HOME/soft/flutter/bin:/snap/bin:/opt/swift/usr/bin:$HOME/soft-ai/stable-diffusion-webui/extensions/_unprompted:$HOME/my/enc/x"
PATH="$PATH:.nvm/versions/node/v20.11.0/bin"

PLAN9=/usr/local/plan9 export PLAN9
PATH=$PATH:$PLAN9/bin export PATH
LD_LIBRARY_PATH="$HOME/soft/lib:/usr/local/cuda/lib64"
HISTSIZE=10000000
HISTFILESIZE=10000000
HISTIGNORE="Z:. Zai:Zai"
HISTTIMEFORMAT="%F %T "
PERL5LIB_=$HOME/soft/lib
MANPATH=/usr/local/man:/usr/local/share/man:/usr/share/man:$HOME/soft/share/man
PERL_CPANM_OPT="--local-lib=$HOME/soft"
EDITOR=nvim
vv=vncviewer
if [ -n "$PS1" ]; then
	:
fi

. $HOME/local/profile

export ENV=$HOME/.rc
export LANG="en_AU.UTF-8"
export LANGUAGE="en_AU:en"
export LC_MESSAGES="en_AU.UTF-8"
export XTERM_LOCALE="en_AU.UTF-8"
export WINDOW_MANAGER=dwm
unset LESS
#export LC_ALL=C
# if [ -n "$PS1" ]; then sl; fi

if [ -n "$PS1" ]; then
	export PS1='# '
	export PS2='#;'
#	if [ -n "$BASH_VERSION" ]; then
	prompt_status() { if [ $? = 0 ]; then echo '# '; else echo -e '#!'; fi; }
	export PS1='$(prompt_status)'
	# export PROMPT_COMMAND="history -a;${PROMPT_COMMAND/history -a;/}"   # breaks X window system?!

	# export PROMPT_COMMAND=('history -a' 'name-terminal-nicely')
	export PROMPT_COMMAND="history -a; name-terminal-nicely"

	n() {
		export TERMINAL_TITLE="$*"
		name-terminal-nicely
	}

#	preexec() { nt "$1"; }
# 	preexec() {
# 		if [ -n "$TERMINAL_TITLE" ]; then
# 			nt "$TERMINAL_TITLE"
# 		else
# 			nt "$1"
# 		fi
# 	}

# 	. bash-preexec.sh

	alias hist='history -n'
#	smiley() { if [ $? = 0 ]; then echo '^_^'; else echo '0_o'; fi; }
#	export PS1='$(smiley) \u@\h:\w> '
#		export PS1='$(smiley) '
#	fi
	if [ -z "$TERMNAME" ]; then
		TERMNAME=$HOSTNAME
		export TERMNAME
	fi
	PATH="$HOME/x:/usr/local/bin:$PATH" nt "$TERMNAME"   # PATH avoids putty / SUSE error
	alias jup=lab
fi

. ~/local/rc

if [ -n "$PS1" ]; then
	stty stop ''
	if type shopt >/dev/null; then
		shopt -s histappend
		shopt -s checkjobs 2>/dev/null
	fi
	# if need aliases put them here
	alias rm=move-rubbish
	alias RM='command rm'
#	alias vi=gvi
	alias ls='ls --color=auto'
	alias diff='diff --color=auto'
	alias ..='cd ..'
	cdp() { cd "`p "${1:-.}"`"; }
	cdup() { cdp; cd ..; }
	cdd() { cd "$(dirname "$1")"; }
	cddp() { cdd "$(p "$1")"; }
	cdw() { cd "$(dirname "$(readlink -f "$(wich "$1")")")"; }
	alias cd.=cdp
	alias cd..=cdup
	killjobs() { eval "`jobs | perl -ne '/(\d+)/ && print "kill %$1\n"'`"; }
	kj() { confirm killjobs && killjobs; }
	if type complete >/dev/null; then
		complete -r
	fi
#	~/music.sh
	alias aqualung='aqualung -o pulse -N0 -E'
	alias ci=messy
	an() { command an "$@" | less; }

	alias d=daemonize

	reset() {
		if [ -z "$TTY_RESET" ]; then
			TTY_RESET=`command reset 2>&1`
		fi
		printf "%s" "$TTY_RESET"
	}

	alias top="top -c -o '%MEM'"
	alias eb="exec bash"
	alias bat="batcat --style header,grid,changes --tabs 8"
	alias gm=git-mod

	alias disown="disown -r"
fi

export PNDSDK="$HOME/pandora-dev/arm-2011.09"

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$("$HOME/mambaforge/bin/conda" 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "$HOME/mambaforge/etc/profile.d/conda.sh" ]; then
        . "$HOME/mambaforge/etc/profile.d/conda.sh"
    else
        export PATH="$HOME/mambaforge/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

if [ -n "$PS1" ]; then
	stty -ixon
fi

export DENO_INSTALL="$HOME/.deno"
export PATH="$DENO_INSTALL/bin:$PATH"

# complete -r
if type shopt >/dev/null; then
	shopt -u progcomp
fi
set +a
# export NVM_DIR="$HOME/.nvm"
# [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
#[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

if [ "$SHELL" = /bin/bash ]; then
	set -a
fi

if [ -n "$BASH_VERSION" ]; then
# The next line updates PATH for the Google Cloud SDK.
if [ -f "$HOME/google-cloud-sdk/path.bash.inc" ]; then . "$HOME/google-cloud-sdk/path.bash.inc"; fi
fi

# The next line enables shell command completion for gcloud.
#if [ -f "$HOME/google-cloud-sdk/completion.bash.inc" ]; then . "$HOME/google-cloud-sdk/completion.bash.inc"; fi

if [ -L ~/tmp -a ! -d ~/tmp/ ]; then
	command rm -f ~/tmp
	ln -s `mktemp -d --tmpdir "$USER.XXXXXXXXXX"` ~/tmp
fi

# this is bad but I cannot fix it the right way
# how about 24-bit truecolor?
if [ "$TERM" = xterm ]; then
	TERM="xterm-256color"
fi
if [ "$TERM" = screen ]; then
	TERM="screen.xterm-256color"
fi

if [ -n "$PS1" ]; then
	eval `dircolors`
fi

if [ -n "$TERMINAL_TITLE" ]; then
	nt "$TERMINAL_TITLE"
fi
# export VOLTA_HOME="$HOME/.volta"
# export PATH="$VOLTA_HOME/bin:$PATH"

if [ -n "$WINDOWID" -a -z "$STY" ]; then
	qe xdotool windowactivate "$WINDOWID"
fi

# pnpm
export PNPM_HOME="$HOME/.local/share/pnpm"
export PATH="$PNPM_HOME:$PATH"
# pnpm end

alias i3lock='i3lock -c 000000'

command rm -f ~/BluejeansHelper.log

# hack
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
	# export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus
	export DBUS_SESSION_BUS_ADDRESS=/run/user/$UID/bus
fi

. ~/allemande/env.sh
. ~/my/env.sh
. ~/my/ai.env
. ~/my/secrets/openexchangerates-app-id.sh
. ~/my/secrets/github-repo-token.txt
. ~/my/secrets/env.sh
. ~/my/enc/x/rc 2>/dev/null

export CUDA_HOME=/usr/local/cuda
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64"

if [ -n "$VIRTUAL_ENV" ]; then
	PATH="$VIRTUAL_ENV/bin:$PATH"
fi


if [ -n "$PS1" ]; then
	export PS1='# '
	export PS2='#;'
	export PS1='$(prompt_status)'
fi

nh() {
	if [ "${#@}" -eq 0 ]; then
		unset HISTFILE
		export PS1='X$(prompt_status)' NO_HISTORY=1
		VIM_PRIVATE=1
		alias vi='vi -i NONE'
#		alias vi='vi -i "$HOME/my/enc/.private.shada"'
	else
		history -d -1
		"$@"
	fi
}

if [ "$NO_HISTORY" = 1 ]; then nh; fi

# umask 0007
. "$HOME/.cargo/env"

## notes tool

NOTES=$HOME/notes

alias idea='note -I'
alias todo='note -T'
alias bug='note -X'
alias pdev='note -P'
alias bookmark='note -B'
alias crazy='note -C'
alias naughty='note -Z'

alias ww='jobs; command ww'

# This clears the HISTCONTROL environment variable, so all commands are saved.
HISTCONTROL=

# This enables the histverify shell option, so I can review before execution. Also saves the line to history which is good.
shopt -s histverify

if pidof Xorg >/dev/null; then
	: ${DISPLAY:=:0}
fi

. path-uniq
