################################
#                              #
#   MON GLORIEUX PROFIL ZSH    #
#                              #
################################


source /home/mb/.config/nnn.sh


#  VARIABLES
################################


# locales
export LANG="en_US.UTF-8"

# default bins
export EDITOR="nano"
export PAGER="less"

# zsh history
export HISTSIZE=1000000000
export SAVEHIST=1000000000
setopt EXTENDED_HISTORY

# opt
RUSTFLAGS="-C opt-level=2 -C target-cpu=native"

# color def
export COL_BLACK="\033[0;30m"
export COL_RED="\033[0;31m"
export COL_GREEN="\033[0;32m"
export COL_ORANGE="\033[0;33m"
export COL_BLUE="\033[0;34m"
export COL_PURPLE="\033[0;35m"
export COL_CYAN="\033[0;36m"
export COL_DGRAY="\033[0;37m"
export COL_DGRAY="\033[1;30m"
export COL_LRED="\033[1;31m"
export COL_LGREEN="\033[1;32m"
export COL_YELLOW="\033[1;33m"
export COL_LBLUE="\033[1;34m"
export COL_LPURPLE="\033[1;35m"
export COL_LCYAN="\033[1;36m"
export COL_WHITE="\033[1;37m"
export COL_RESET="\033[0m"


#  ALIASES
################################


# shorthands
alias ll="ls -l"
alias la="ls -al"
alias l=ls
alias c=cat
alias g=grep
alias gv="g -v"
alias e=echo
alias t=tee
alias f=find
alias h=head
alias s=sed
alias x=xargs
alias tl=tail
alias mk=make
alias ca=cargo

alias gs='git status'
alias ga='git add'
alias ga.='git add .'
alias gc='git commit'
alias gcm='git commit -m'
alias gp='git push'
alias gf='git fetch --all'
alias gl='git log --all'
alias gc2='gc -m "$(gl --max-count=1 --oneline | /bin/cut --delimiter ' ' --fields 2-) 2"'

alias "..."="../.."
alias "...."="../../.."
alias "....."="../../../.."

# edit default commands
alias ls=lsd
alias bat="/usr/bin/bat --theme=ansi"
alias cat="bat --paging=never --style=snip"
alias cut=tuc
alias make="/usr/bin/make -j32"
alias mek=make
alias code="codium"
alias untar="tar -xvf"
alias paru="unset CARGO_TARGET_DIR; paru"
alias pandoc="pandoc --pdf-engine=tectonic"

alias gcc_repl="tcc -o /tmp/out -run -"
alias cargo_repl="evcxr"

alias py="python3"
alias ts="deno"
alias rs="cargo_repl"
alias c="gcc_repl"

# benchmark commands : hyperfine
# html into png      : html3png
# json processor     : jq
# restart cinnamon   : Alt + F2, R
# cheat engine       : GameConqueror


#  COMMANDS
################################


alias cl="echo -e '\0033\0143'; clear" #tput clear
alias ddg="w3m duckduckgo.com"
alias used-ports="sudo lsof -nP -iTCP"
alias lorem="echo 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'"
alias tartiflette="tar -cvaf"
alias tartifliste="tar -tvf"
alias détartre="tar -xvf"
alias say="festival --tts"
alias chmox="chmod a+x"

chmax() {
	find "$1" | grep '\.sh$' | while read f
	do chmox "$f"
	done
}

playtube () {
	SESS_COOKIES="/tmp/cook.txt"
	RES=$(youtube-dl -g --cookies  $SESS_COOKIES --default-search "ytsearch" "$1")
	mplayer -cookies -cookies-file $SESS_COOKIES "$RES" 
}

forked() {
	$@ > /dev/null 2>&1 &!
}

alias marktext="forked /usr/bin/marktext"
alias thunar="forked /usr/bin/thunar"
alias idea="forked /usr/bin/idea"

beep() {
	count=${1:-'1'};
	for i in $(seq $count)
	do echo -ne '\007' && sleep 0.1s
	done
}

force() {
	command="$@"
	echo $COL_ORANGE"[force] trying $COL_WHITE$command$COL_RESET"
	until sh -c "$command"
	do
		echo $COL_ORANGE"[force] retrying $COL_WHITE$command$COL_RESET"
		sleep 0.5s
	done
}

pause() {
	DELAY=$1
	echo ""
	while [ $DELAY -gt 0 ]
	do
		echo "\033[F"$COL_PURPLE"[pause] sleeping $COL_WHITE$DELAY$COL_PURPLE s$COL_RESET "
		sleep 1s
		DELAY=$(( $DELAY - 1 ))
	done
}

function loop() {
	cmd=$@;
	while true
	do
		echo "$COL_YELLOW""[loop] running '$cmd'""$COL_RESET";
		sh -c "$cmd";
		sleep 0.5s;
	done
}

function tldr() {
	/usr/bin/tldr $@ || ( /usr/bin/tldr --update && /usr/bin/tldr $@ ; )
}

alias les_pates="pause $(( 8 * 60 )); beep 10"

js() {
	echo "$@" | ansi2txt | deno repl -Aq
}

fuckify()  {
	dd if=/dev/urandom of=$1 bs=$2 count=1
}

# tout est cassé
alias reparer_qt="paru -S --noconfirm --redownload --rebuild qt5-styleplugins"
alias reparer_discord="/home/mb/.local/scripts/reparer_discord.ts"
alias reparer_opentablet="systemctl --user daemon-reload; systemctl --user enable opentabletdriver --now; paru -S opentabletdriver --rebuild --noconfirm; systemctl --user restart opentabletdriver; systemctl --user daemon-reload"
alias reparer_gtk="sudo pacman -Rndd xdg-desktop-portal xdg-desktop-portal-gtk; sudo pacman -Rndd xdg-desktop-portal"
alias restart-cinnamon="pkill -HUP -f 'cinnamon --replace'"


#  KEY BINDS
################################


if type bind > /dev/null
then
	bind '^K' 'ls'
	bind '^E' 'thunar .'
	bind '^T' 'kitty'
fi


#  REST
################################


alias box-drawing-char="/home/mb/.local/scripts/box-drawing-char.sh"
print-colors() {
	for i in {0..255}
	do print -Pn "%K{$i}  %k%F{$i}${(l:3::0:)i}%f " ${${(M)$((i%6)):#3}:+$'\n'}
	done
}

upload() {
	rsync -avzhP $@
}

pi-tag() {
	identifier=$(cat ./pi.ron | grep "identifier:" | cut -d ":" -f 2 | cut -d "\"" -f 2);
	commit_msg="pi - "$(date +"%d/%m/%Y %H:%M");
	tag_name="$identifier""pi"$(date +"%s");
	echo $COL_BLUE"[pi-tag] committing ...$COL_RESET";
	git add . &&
	git commit -m "$commit_msg" &&
	echo $COL_BLUE"[pi-tag] tagging ...$COL_RESET" &&
	git tag -a "$tag_name" -m "$commit_msg" &&
	echo $COL_BLUE"[pi-tag] tagged $COL_WHITE$tag_name$COL_RESET";
}

col_show() {
	for col in "COL_BLACK" "COL_RED" "COL_GREEN" "COL_ORANGE" "COL_BLUE" "COL_PURPLE" "COL_CYAN" "COL_DGRAY" "COL_DGRAY" "COL_LRED" "COL_LGREEN" "COL_YELLOW" "COL_LBLUE" "COL_LPURPLE" "COL_LCYAN" "COL_WHITE"
	do
		eval val="\$$col"
		echo "\$$col :$val Sint recusandae vero nihil. $COL_RESET"
	done
	echo "\$COL_RESET : closing tag";
}

tmpath() {
	script="
const options = [
	'truc',
	'machin',
	'chouette',
	'bidule'
];
function take_random(arr: string[]) {
	const s = Math.floor(Math.random() * arr.length);
	const [result] = arr.splice(s, 1);
	return result;
}
const result = [take_random(options)];
while (options.length > 0 && (Math.random() > 0.3))
	result.push(take_random(options));
console.log(result.join('_'));
	";
	name=$(echo "$script" | deno run -);
	echo "/tmp/$name";
}

discordify() {
	input="$1";
	output=$input".discord.webm";
	length=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$input");
	factor="44000.0";
	rate=$(( $factor / $length ))K;
	echo $COL_BLUE"[discordify] input: $COL_WHITE'$input'$COL_BLUE output: $COL_WHITE'$output'$COL_BLUE length: $COL_WHITE$length$COL_BLUE rate: $COL_WHITE$rate$COL_RESET";
	ffmpeg -i "$input" -threads 8 -b "$rate" "$output";
}

destroy() {
	total=0;
	for arg in $@
	do
		for f in $(find "$arg")
		do total=$(( $total + 1 ));
		done
	done

	i=0
	for arg in $@
	do
		for f in $(find "$arg")
		do
			i=$(( $i + 1 ));
			if [ -f "$f" ]
			then
				echo "[destroy] ( $i / $total ) shreding '$f'"
				shred -n10 -z -u "$f"
			fi
		done
		echo "[destroy] removing '$arg'"
		rm -fr "$arg"
	done
}

diffc() {
	cmd1="$1";
	cmd2="$2";

	sh -c "$cmd1" > /tmp/cmd1;
	sh -c "$cmd2" > /tmp/cmd2;

	diff /tmp/cmd1 /tmp/cmd2;
}

html2pdf() {
	if [ $# -lt 2 ]
	then echo "usage: html2pdf <html> <pdf>" && return 1
	fi
	chromium --headless --disable-gpu --run-all-compositor-stages-before-draw --no-pdf-header-footer --print-to-pdf="$2" "$1"
}

wait_file() {
	until [ -f "$1" ]
	do sleep 0.5s
	done
}


alias sshpinotes="TERM=xterm-color ssh ubuntu@162.19.64.118"
alias mrustc="MRUSTC_TARGET_VER=1.54 /home/mb/Projects/mrustc/bin/mrustc -L /home/mb/Projects/mrustc/output-1.54.0"
