#!/bin/bash

export EMAIL='bernie@codewiz.org'
[[ "$TERM" == "xterm" ]] && export TERM=xterm-256color

nproc="$(nproc)"
export AUTOMAKE_JOBS="$nproc"
alias make="nice make -j$nproc"
unset nproc
alias cctags='ctags -R --languages="C++,C" --c++-kinds=+p --fields=+iaS --extra=+q --links=no --verbose --totals'
alias night='redshift -O 5000'
alias day='redshift -O 5500'
alias ta='tmux attach'

#alias slay='mpv --load-unsafe-playlists --playlist=https://www.slayradio.org/tune_in.php/128kbps/slayradio.128.m3u'
alias slay='mpv http://relay3.slayradio.org:8000/'
alias necta='mpv http://scenestream.io/necta128.ogg'
alias somasa='mpv https://soma.fm/secretagent.pls'
alias somags='mpv https://soma.fm/groovesalad.pls'
alias somass='mpv https://soma.fm/spacestation.pls'
alias somadz='mpv https://soma.fm/dronezone.pls'
alias somadg='mpv https://soma.fm/digitalis.pls'

alias pp='ps -afx -o pid,state,start_time,rss,size,%cpu,args'
alias p1='patch -p1 -g1 --dry-run'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'

if [[ -f /bin/exa ]]; then
  alias l='exa -l --git-ignore'
  alias ll='exa -la --icons --color-scale'
  #alias ll='exa -la --hyperlink --icons --color-scale --mounts'
  alias llrt='exa -la --icons --color-scale -snew'
  alias ls='exa'
elif [[ -f /bin/lsd ]]; then
  alias l='lsd -l'
  alias ll='lsd -Al --size=short'
  alias ls='lsd'
else
  alias l='ls -l'
  alias ll='ls -Alh'
  alias ls='/bin/ls --color=auto'
fi

x() { xdg-open "$@"; }
sw() { mv "$1" "$1.$$" && mv "$2" "$1" && mv "$1.$$" "$2"; }
psc() { ps xawf -eo pid,user,cgroup,args; }
#vim() { if [ -x /usr/bin/vimx ]; then /usr/bin/vimx "$@"; else /usr/bin/vim "$@"; fi; }
v()     { vi "$@"; }
vim() { echo "why not just vi?"; }
virc() { vi ~/.vimrc "$@"; }
nwiz() { vi ~/.config/nvim/plugin/nwiz.lua "$@"; }
rvim() { gvim --remote-silent "$@"; }
g()     { grep -Er --color=auto "$@"; }
grepi() { grep -Eri --color=auto "$@"; }
s()     { grep -ErnIr --exclude-dir=.git --exclude-dir=.deps --exclude-dir=.libs --exclude-dir=build --exclude-dir=target --exclude-dir=Debug --exclude-dir=Release --exclude=compile_commands.json --exclude='*.lst' --exclude='*.o' --exclude='*.d' "$@"; }
please() { nice ionice -c2 -n6 "$@"; }
bc() { /usr/bin/bc -l "$@"; }
memo()  { ( cd ~/memo && ${EDITOR:-vi} index.wiki '+set conceallevel=0' '+set wrap' "$@" && git commit -a -m "`hostname`" ) }
gmemo() { ( cd ~/memo && gvim          index.wiki '+set conceallevel=0' '+set wrap' "$@" && git commit -a -m "`hostname`" ) }
syncmemo() { ( cd ~/memo && git pull "$@" && git push ) }

apt() { sudo nice ionice -c 3 apt "$@"; }
dnf() { sudo nice ionice -c 3 dnf "$@"; }
dnftest() { sudo nice ionice -c 3 dnf --enablerepo=updates-testing --enablerepo=rpmfusion-nonfree-updates-testing --enablerepo=rpmfusion-free-updates-testing "$@"; }
dnfraw() { sudo nice ionice -c 3 dnf --disablerepo=updates --disablerepo=fedora --enablerepo=rawhide --enablerepo=rpmfusion-free-rawhide --enablerepo=rpmfusion-nonfree-rawhide "$@"; }
dnfdbg() { sudo nice ionice -c 3 dnf --enablerepo=rawhide-debuginfo "$@"; }
rpmqa() { rpm -qa --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' | sort; }
rpmsize() { rpm -q --queryformat='%{size}\n' "$@"; }
rget() { rsync -Pav --inplace "$@"; }
eat() { LD_PRELOAD=~/src/oss/libeatmydata-9/libeatmydata.so.1.0 "$@"; }
sc() { sudo systemctl "$@"; }

mp() { mpv "$@"; }

pw() { pwgen -s 14 "$@"; }
pub() { for f in "$@"; do chmod a+r "$f" && scp -r "$f" people.sugarlabs.org:public_html/ && echo "https://codewiz.org/pub/`basename "$f"`"; done; }
unpub() { for f in "$@"; do ssh people.sugarlabs.org rm "\"public_html/$f\""; done; }

tsh() { ssh "$@" tmux attach -d; }

[[ -S "$XDG_RUNTIME_DIR/ssh-agent.socket" ]] && export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/ssh-agent.socket" || :

