#!/bin/sh

# Use neovim for vim if present.
[ -x "$(command -v nvim)" ] && alias vim="nvim" vimdiff="nvim -d"

# add sudo prefix to some commands
# for command in mount umount sv 'pacman -S' 'pacman -Syu' 'pacman -Rns' 'pacman -Rcns' 'pacman -Sc' updatedb su shutdown poweroff reboot ; do
for command in mount umount sv 'pacman -S' 'pacman -Syu' 'pacman -Rns' 'pacman -Rcns' 'pacman -Sc' updatedb su ; do
	alias $command="sudo $command"
done; unset command

# applications and scripts
alias r="ranger"
alias nvim='env -u VIMINIT nvim'
alias v='env -u VIMINIT nvim'
alias vi="nvim"
alias vim="nvim"
alias p="pacman"
alias py="python"
# alias python="/usr/bin/python3"
alias up="topgrade"
alias fe="fastfetch"
alias fetch="fastfetch"
alias am="addmovie"
alias mif="make_info_file"
alias mt='tree --timefmt "%Y_%m_%d - %H:%M:%S" > tree.txt'
alias mm='tree --timefmt "%Y_%m_%d - %H:%M:%S" > tree.txt && make_info_file'
alias mov="moviedescription"
alias speakertest="speaker-test -t wav -c 6"
alias soundtest="speaker-test -t wav -c 6"
alias setclip="xclip -selection c"
alias getclip="xclip -selection c -o"
alias tetris="autoload -Uz tetriscurses && tetriscurses"
alias wrandr="wlr-randr"
alias calendar="cal -w"
alias kalender="cal -w"
alias kalenderwoche="cal -w 2023"
alias kw="cal -w 2023"
alias lg="ll | grep "
alias ytcast="ytcast -d 192.168.2.106"
alias duf="duf -hide-fs tmpfs,efivarfs,devtmpfs"
alias lsenv="printenv"
alias create_md5="find -type f \( -not -name "md5sum.txt" \) -exec md5sum '{}' \; > md5sum.txt"
alias mksfv="cksfv -R . > checksums.sfv"
alias mks="cksfv -R . > checksums.sfv"
alias cm="find -type f \( -not -name "md5sum.txt" \) -exec md5sum '{}' \; > md5sum.txt"
alias screensaver="hyprctl keyword exec 'mpv --no-audio --loop -fs --osc=no --osd-bar=no --config=no ~/.local/share/screensavers/bad_apple.mkv'"
alias ss="hyprctl keyword exec 'mpv --no-audio --loop -fs --osc=no --osd-bar=no --config=no ~/.local/share/screensavers/bad_apple.mkv'"
alias please="sudo"
alias scr="hyprctl keyword exec scrcpy --video-codec=h265 -m1920 --max-fps=90"
alias mpvo="hyprctl keyword exec mpv "
alias pride="cli-pride-flags "

alias hdr-mpv="ENABLE_HDR_WSI=1 mpv --vo=gpu-next --target-colorspace-hint --gpu-api=vulkan --gpu-context=waylandvk"

# video showcase
alias 24fps="mpv ~/.local/share/sample-vids/24fps.mkv"

# change dir and list fi
function cs () { cd "$@" && exa -la }
alias z="cs"
alias y="cs"

# Vim-like
alias :q='exit'
alias :wq='exit'

# line count
alias lc='wc -l'

# fzf scripts
alias yay-fzf='yay -Slq | fzf --multi --preview "yay -Si {1}" | xargs -ro yay -S'

# check to see if system booted on legacy bios mode or uefi
alias bios-or-uefi='[[ -d /sys/firmware/efi ]] && echo "UEFI" || echo "BIOS"'

# move files to trash
trash () { mv $1 ~/.local/share/Trash/files/ }

# restart a program
refresh () {
	killall $1
	$1 &
}
alias restart='refresh'

# archive extraction - usage: ex <file>
ex ()
{
  if [ -f $1 ] ; then
    case $1 in
      *.tar.bz2)   tar xjf $1   ;;
      *.tar.gz)    tar xzf $1   ;;
      *.bz2)       bunzip2 $1   ;;
      *.rar)       unrar x $1   ;;
      *.gz)        gunzip $1    ;;
      *.tar)       tar xf $1    ;;
      *.tbz2)      tar xjf $1   ;;
      *.tgz)       tar xzf $1   ;;
      *.zip)       unzip $1     ;;
      *.Z)         uncompress $1;;
      *.7z)        7z x $1      ;;
      *.deb)       ar x $1      ;;
      *.tar.xz)    tar xf $1    ;;
      *.tar.zst)   unzstd $1    ;;
      *)           echo "'$1' cannot be extracted via ex()" ;;
    esac
  else
    echo "'$1' is not a valid file"
  fi
}

# flatpak
alias f="flatpak install --user"
alias fl="flatpak list"
alias fs="flatpak search"
alias fup="flatpak uninstall --unused && flatpak update"
alias fu="flatpak uninstall --delete-data"
alias fuu="flatpak uninstall --unused"
alias fclean="echo -n '~/.local/share/flatpak: ' && dus ~/.local/share/flatpak/ && flatpak repair --user && echo -n '~/.local/share/flatpak: ' && dus ~/.local/share/flatpak/"
alias fg="flatpak list --app --columns=name,application,size | column -c20 -ts $'\t' | sort | fzf --layout=reverse"
alias fq="fg"
alias frun="fg | awk -v str=\"$str\" '\$1 ~ str { print \$2 }' | xargs -I{} hyprctl dispatch exec flatpak run {}"   # fzf flatpak list and run it
alias fgu="fg | awk '{ print \$2 }' | xargs -I{} flatpak uninstall --delete-data -y '{}'"                           # fzf flatpak list and run it
alias fr="fgu"
alias fsize="flatpak list --app --columns=size,name,application | sort -g | column -ts $'\t'"

# pacman
alias pu="sudo pacman -Rcns"
alias pi="pacman -Slq | fzf --multi --preview 'cat <(pacman -Si {1}) <(pacman -Fl {1} | awk \"{print \$2}\")' | xargs -ro sudo pacman -S"
alias pss='pacsearch'
alias pl='pacman -Qq | fzf --preview "pacman -Qil {}" --layout=reverse --bind "enter:execute(pacman -Qil {} | less)"'
alias pq='pacman -Qq | fzf --preview "pacman -Qil {}" --layout=reverse --bind "enter:execute(pacman -Qil {} | less)"'
alias pqtd='pacman -Qtdq'
alias orphans='pacman -Qtdq'
alias orphans-fzf='pacman -Qtdq | fzf --preview "pacman -Qil {}" --layout=reverse --bind "enter:execute(pacman -Qil {} | less)"'
alias orphans-explicit="pacman -Qtdq | fzf --multi --preview 'cat <(pacman -Qi {1}) <(pacman -Fl {1} | awk \"{print \$2}\")' | xargs -ro sudo pacman -D --asexplicit"
alias orphans-remove="pacman -Qtdq | fzf --multi --preview 'cat <(pacman -Qi {1}) <(pacman -Fl {1} | awk \"{print \$2}\")' | xargs -ro sudo pacman -Rcns"
alias pqe='pacman -Qqe | sort -r'
alias explicit='pacman -Qqe | sort -r'
alias pqef='pacman -Qqe | fzf --preview "pacman -Qil {}" --layout=reverse --bind "enter:execute(pacman -Qil {} | less)"'
alias explicit-fzf='pacman -Qqe | fzf --preview "pacman -Qil {}" --layout=reverse --bind "enter:execute(pacman -Qil {} | less)"'
alias explicit-remove="pacman -Qetq | fzf --multi --preview 'cat <(pacman -Qi {1}) <(pacman -Fl {1} | awk \"{print \$2}\")' | xargs -ro sudo pacman -Rcns"
alias unneeded="pacman -Qqd | pacman -Rsu --print - | sed 's/-[0-9].*//g' | sort -r"
alias unneeded-remove="pacman -Qqd | pacman -Rsu --print - | sed 's/-[0-9].*//g' | sort | fzf --multi --preview 'cat <(pacman -Qi {1}) <(pacman -Fl {1} | awk \"{print \$2}\")' | xargs -ro sudo pacman -Rcns"
alias un="pacman -Qqd | pacman -Rsu --print - | sed 's/-[0-9].*//g' | sort -r"
alias pdust="pacman_sizes && echo && echo "sum:" && expac -H M '%m' | awk '{sum += \$1}END {print sum, \$2}'"
alias wn='pactree -r'
alias whoneeds='pactree -r'
alias pacsyu='sudo pacman -Syyu'                    # update only standard pkgs
alias yaysua="yay -Sua --noconfirm"                 # update only AUR pkgs
alias yaysyu="yay -Syu --noconfirm"                 # update standard pkgs and AUR pkgs
alias unlock="sudo rm /var/lib/pacman/db.lck"       # remove pacman lock
alias cleanup='sudo pacman -Rns $(pacman -Qtdq)'    # remove orphaned packages
alias pcu="paccache -ruk0"                          # remove cache of uninstalled packages
alias pacsize="expac -H M '%m\t%n' | sort -h"
alias pacdust="du -sh /var/cache/pacman/pkg/"
alias pcdust="du -sh /var/cache/pacman/pkg/"
alias pacmancache="du -sh /var/cache/pacman/pkg/"
alias pacprovides='pacman -Qo'
alias whoprovides='pacman -Qo'
alias wp='pacman -Qo'
alias ww='whatwants'
alias pqi='pacman -Qi'
alias pacdiff='pacdiff -s'

# appman
alias appsearch="appman list | grep"
alias apps="appman list | grep"

# networking
alias getports='sudo lsof -i -P -n | grep LISTEN'
alias ipof='host -t a'
alias dnslookup="dig"

# useful short commands that once were bash scripts:
alias anf="echo '„“'"

# Verbosity and settings that you pretty much just always are going to want.
alias cp="cp -iv"
alias mv="mv -iv"
alias rm="rm -vI"
alias bc="bc -ql"
alias mkd="mkdir -pv"
alias ffmpeg="ffmpeg -hide_banner"

# Colorize commands when possible.
alias grep="grep --color=auto"
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias diff="diff --color=auto"
alias ccat="highlight --out-format=ansi"
alias ip="ip -color=auto"

# shortened commands
alias ka="killall"
alias g="git"
alias sdn="shutdown -h now"
alias q="exit"
alias sc="dinitctl"
# alias sc="systemctl"
# alias j="journalctl -xe"

# fuzzy sarch of history
alias h="history | cut -c 8- | sort | uniq | fzf | tr '\\n' ' ' | xclip -selection c"

## get fastest mirrors
alias mirror="sudo reflector -f 30 -l 30 --number 10 --verbose --save /etc/pacman.d/mirrorlist"
alias mirrord="sudo reflector --latest 50 --number 20 --sort delay --save /etc/pacman.d/mirrorlist"
alias mirrors="sudo reflector --latest 50 --number 20 --sort score --save /etc/pacman.d/mirrorlist"
alias mirrora="sudo reflector --latest 50 --number 20 --sort age --save /etc/pacman.d/mirrorlist"

# kdeconnect
alias send_url_note9="kdeconnect-cli --name "note9" --share"
alias sendnote9="kdeconnect-cli --name "note9" --share"
alias send="kdeconnect-cli --name "note9" --share"

# ls
alias l='~/.local/bin/eza -x --group-directories-first --almost-all --hyperlink --icons --git'
alias ls='l'
alias ll='~/.local/bin/eza --long --header --hyperlink --icons --git -la --group-directories-first --group --time-style="+%a %d %b %Y %H:%M:%S"'
alias 'ls -la'='~/.local/bin/eza --long --header --hyperlink --icons --git -la --group-directories-first --group --time-style="+%a %d %b %Y %H:%M:%S"'
alias eza='~/.local/bin/exa --long --header --hyperlink --icons --git -la --group-directories-first --group --time-style="+%a %d %b %Y %H:%M:%S"'
alias lll='ls -l'
alias la='ls -la'

alias t='colorls --tree'
alias tt='tree'

# ncdu
alias ncdu='ncdu --si' # use powers of 1000 instead of 1024
alias n='ncdu'

# adding flags
alias cp="cp -i"                                                # confirm before overwriting something
alias df='df -h -H -x tmpfs -x efivarfs -x devtmpfs -T -x vfat -x fuse.python -x fuse.dwarfs -x fuse.squashfuse -x fuse.signal' # human-readable sizes + sizes in powers of 1000 (e.g., 1.1G)
alias dfa='/bin/df -h -H -T'                                    # human-readable sizes + sizes in powers of 1000 (e.g., 1.1G)
alias dff='/bin/df -h -H -T'                                    # human-readable sizes + sizes in powers of 1000 (e.g., 1.1G)
alias free='free --si -h'                                       # human-readable + show sizes in in powers of 1000

# disk free
alias watchdf="watch -n5 -t df -h -H -x tmpfs -x efivarfs -x devtmpfs -T -x vfat -x fuse.python -x fuse.dwarfs -x fuse.squashfuse -x fuse.signal"
alias wdf='watch -n5 -t "df -h -H -x tmpfs -x efivarfs -x devtmpfs -T -x vfat -x fuse.python -x fuse.dwarfs -x fuse.squashfuse -x fuse.signal -x fuse.libreoffice"'

# replace cat with bat
alias cat='bat --style=plain'
alias c='bat --style=plain'

# dust flags (rust replacement of du, way faster):
    # -o si/b/kb/kib/mb/mib/gb/gib (si - prints sizes in powers of 1000. Others print size in that format).
    # -s (apparent-size - shows the length of the file as opposed to the amount of disk space it uses)
    # -c (No colors [monochrome])
    # -b (Do not show percentages or draw ASCII bars)
    # -r (reverse order of output)
alias d='dust'
alias ld='dust -d1'         # basically ls with sizes, no folder tree
alias du='dust'             # replace du with dust
alias dust='dust -o si -r'  # human-readable + powers of 1000 + app
alias ds="dus"              # tipp: use dus to only show sum, replaces "du -sh" (script in ~/.local/bin)

# youtube-dl / yt-dlp
alias yta-best="yt-dlp --extract-audio --audio-format best "
alias yta-flac="yt-dlp --extract-audio --audio-format flac "
alias yta-m4a="yt-dlp --extract-audio --audio-format m4a "
alias yta-mp3="yt-dlp --extract-audio --audio-format mp3 "
alias ytamp3="yt-dlp --extract-audio --audio-format mp3 "
alias yta-opus="yt-dlp --extract-audio --audio-format opus "
alias yta-vorbis="yt-dlp --extract-audio --audio-format vorbis "
alias yta-wav="yt-dlp --extract-audio --audio-format wav "
alias ytv-best="yt-dlp -f bestvideo+bestaudio "
alias yt-archive="cd /mnt/pi/mnt/music/data/youtube/ && ytdlp "
alias yt-archive="cd /mnt/pi/mnt/music/data/youtube/ && ytdlp "
alias yta="cd /mnt/ssd/vids/youtube/ && ytdlp "
alias fuckgoogle="cd /mnt/ssd/vids/youtube/ && ytdlp "
alias ytf="cd /mnt/ssd/vids/youtube/ && ytdlp --file"

# spawn a quick http server with python
alias pserver="python3 -m http.server 9000"
alias phttp="python3 -m http.server 9000"
alias webserver="python3 -m http.server 9000"
alias httpserver="python3 -m http.server 9000"

# restart waybar
alias restartwaybar="pkill waybar; hyprctl dispatch exec waybar"

# monitor control, for example for nightfury
alias monoff="xset dpms force off"              # turns off monitor/display
alias mon="monitor"                             # currently just for brigtness
alias m="monitor"                               # currently just for brigtness
alias mb="monitor"                              # currently just for brigtness

# check system/hardware stuff
alias lsgpu="lspci -k | grep -A 2 -E '(VGA|3D)'"
alias lsmesa="glxinfo -B | grep Version:"
alias mesaver="glxinfo -B | grep Version:"
alias updategrub="sudo grub-mkconfig -o /boot/grub/grub.cfg"
alias catdns="cat /etc/resolv.conf"
alias dnscat="cat /etc/resolv.conf"

alias ssh="TERM=xterm-256color ssh"             # fix input on remote systems with alacritty (ssh-sessions)

# hyprland alias
alias starthyprland="sh ~/.config/hypr/initrc"
alias hl="sh ~/.config/hypr/initrc"
alias windowclass='watch -n 0.1 "hyprctl -j activewindow | jq -r \".class\""'
alias getwindowclass='watch -n 0.1 "hyprctl -j activewindow | jq -r \".class\""'

# dinit / loginctl
alias reboot="loginctl reboot"
alias efi="loginctl reboot --firmware-setup"
# alias services="systemctl list-units --type=service --all"
# alias services-loaded="systemctl list-units --type=service"
# alias services_loaded="systemctl list-units --type=service"

# systemd
# alias reboot="systemctl reboot" # use systemd to reboot, requiring no password
# alias services="systemctl list-units --type=service --all"
# alias services-loaded="systemctl list-units --type=service"
# alias services_loaded="systemctl list-units --type=service"

# hardware
alias bl="backlight"

# battery
alias lsbattery="upower -i /org/freedesktop/UPower/devices/battery_BAT0"
alias lsb="upower -i /org/freedesktop/UPower/devices/battery_BAT0"

# gpu nightfury:
alias gpu_powercap="cat /sys/class/drm/card1/device/hwmon/hwmon7/power1_cap"
alias gpu_bar="sudo dmesg | grep BAR="

# $HOME ~/ cleanup:
alias wget='wget --hsts-file="$XDG_DATA_HOME/wget-hsts"'
alias yarn='yarn --use-yarnrc "$XDG_CONFIG_HOME/yarn/config"'
alias cotp="cotp -d ~/.local/share/cotp/db.cotp"

# refresh all aliases
alias ref="source ${XDG_CONFIG_HOME:-$HOME/.config}/shell/shortcutrc ; source ${XDG_CONFIG_HOME:-$HOME/.config}/shell/zshnameddirrc ; source ${XDG_CONFIG_HOME:-$HOME/.config}/shell/aliasrc"
