#!/bin/sh
# vim: ft=sh:sw=2:ts=2:sts=2:fdm=marker:fdl=0:noml:
# https://github.com/drduh/config/blob/main/zshrc

umask 077
autoload -U colors && colors
autoload -U compinit && compinit

bindkey "^[[1;5C" vi-forward-word
bindkey "^[[1;3C" forward-word
bindkey "^[[1;5D" vi-backward-word
bindkey "^[[1;3D" backward-word

# shellcheck disable=SC2155
export today="$(date +%F)"
# shellcheck disable=SC2155
export ts="$(date +%s)"
# export PS1="%{$fg[red]%}%h %{$fg[yellow]%}%~ %{$reset_color%}% "
# export SPROMPT="$fg[red]%R$reset_color did you mean $fg[green]%r?$reset_color "
# shellcheck disable=SC2155
export NETWORK="$(ip a | sed -n '/state UP/ s/.: //p' | sed 's/:.*//g')"
# shellcheck disable=SC2155
export ROOT="$(command -v sudo || command -v doas)"
export HISTFILE="${HOME}/.histfile"
export HISTSIZE=200
export SAVEHIST="${HISTSIZE}"

#export http_proxy="127.0.0.1:8118"
#export https_proxy="127.0.0.1:8118"
#export http_proxy="http://127.0.0.1:8118"
#export https_proxy="http://127.0.0.1:8118"

LANG="en_US.UTF-8"
export LC_ADDRESS="${LANG}"
export LC_ALL="${LANG}"
export LC_COLLATE="${LANG}"
export LC_CTYPE="${LANG}"
export LC_IDENTIFICATION="${LANG}"
export LC_MEASUREMENT="${LANG}"
export LC_MESSAGES="${LANG}"
export LC_MONETARY="${LANG}"
export LC_NAME="${LANG}"
export LC_PAPER="${LANG}"
export LC_TELEPHONE="${LANG}"
export LC_TIME="${LANG}"
export LESSCHARSET="utf-8"
export LESSHISTFILE=-
export LESSSECURE=1
#export TZ="UTC"

setopt alwaystoend
setopt autocd
setopt autopushd
setopt completeinword
setopt correct
setopt extendedglob
setopt extendedhistory
setopt histignorealldups
setopt histignorespace
setopt histreduceblanks
setopt listtypes
setopt nobeep
setopt nocaseglob
setopt noclobber
setopt nullglob

zstyle ":completion:*" auto-description "specify %d"
zstyle ":completion:*" cache-path "${HOME}/.zsh_cache"
zstyle ":completion:*" completer _expand _complete _correct _approximate
zstyle ":completion:*" file-sort modification reverse
zstyle ":completion:*" format "completing %d"
zstyle ":completion:*" group-name ""
zstyle ":completion:*" hosts off
zstyle ":completion:*" list-colors "=(#b) #([0-9]#)*=36=31"
zstyle ":completion:*" menu select=long-list select=0
zstyle ":completion:*" use-cache on
zstyle ":completion:*" verbose yes
zstyle ":completion:*:kill:*" command "ps -u ${USER} -o pid,%cpu,tty,cputime,cmd"

# alias -g G="| grep -i"
# alias -g H="| head"
# alias -g L="| less"
# alias -g S="| sort"
# alias -g T="| tail"
# alias -g U="| uniq"
# alias -g V="| vim -"
# alias -g XC="| xclip -i"
# alias -g XP="| xclip -o"

alias l="ls -ltrhsa"
alias ls="ls -lha"
alias lo="libreoffice"
alias logs="journalctl --follow"
alias t="tail -f"
alias v="vim -p"
alias cat="cat -t"
alias mv="mv -i"
alias rebootfw="systemctl reboot --firmware-setup"
alias rm="rm -i"
alias audio="pgrep pulseaudio||pulseaudio &;pacmd list-sinks|egrep '\*|card:'"
audio_set() { pacmd set-default-sink "${1}"; }
alias bim="vim"
alias bios='"${ROOT}" dmidecode -s bios-version'
alias cr="firejail --dbus-user=none chromium --enable-unveil --incognito --no-referrers --no-pings --no-experiments --disable-translate --dns-prefetch-disable --disable-background-mode --no-first-run --no-default-browser-check --ssl-version-min=tls1.2"
alias d="du -hd1 ."
alias dif="diff"
alias dimages="docker images --format '{{.Repository}}-{{.Tag}}' | sort"
alias dmsg='"${ROOT}" dmesg -wHT'
alias et="exiftool"
alias feh="feh --auto-rotate --auto-zoom --draw-filename --recursive --scale-down --image-bg black --verbose"
alias fre="free -h"
# alias ftb="firejail --profile=firejailed-tor-browser ${HOME}/Browser/start-tor-browser"
alias gitreset="git reset --hard origin/main"
alias goinit="go mod init main && go mod tidy"
alias gorun="go run ."
alias gp="for r in */.git ; do ( cd \$r/.. && git pull ; ) ; done"
alias grep="grep --color --text"
alias grepv="grep --invert-match"
alias mailo="virsh --connect qemu:///system start 'openbsd' && sleep 10 && ssh -Y virtual -t thunderbird && virsh --connect qemu:///system shutdown 'openbsd'"
mnt() { ${ROOT} mount -o uid=1000 "$1"; }
alias off='"${ROOT}" shutdown -h now'
alias ollama="docker exec ollama ollama"
alias oath="oathtool --totp=sha1 --base32"
# shellcheck disable=SC2312
oathcode_a() { oath "$(cat "${HOME}/secrets/a")"; }
oathcode_b() { oath "$(cat "${HOME}/secrets/b")"; }
alias oathcode_c="ykman oath accounts code c"
alias oathcode_d="ykman oath accounts code d"
alias p="python3"
alias pkg-info="apt-cache show"
alias rebootfw="systemctl reboot --firmware-setup"
alias resize_view="xrandr --output Virtual1 --mode 1600x1200"
alias sb="mokutil --sb-state"
alias setcap_net="${ROOT} setcap cap_net_raw=eip ${1}"
alias slow_startup="systemd-analyze blame"
alias start_ollama="docker run --network=host -d --gpus=all -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama"
alias start_webui="docker run --network=host -d -p 3000:8080 --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-webui:latest"
alias tb="thunderbird --ProfileManager --no-remote"
alias test_sound="aplay -d 0 /usr/share/sounds/alsa/Front_Center.wav"
alias update="sudo apt update && sudo apt -y upgrade"
alias utc="date -u '+%Y.%m.%dT%H:%M'"
alias vm="virt-manager"
alias vnc_start="x0vncserver -passwordfile ~/.vnc/passwd -display :0"
alias vnc_tv="ssh -Y tv -t remmina -c /home/media/.local/share/remmina/group_vnc_quick-connect_127-0-0-1-5900.remmina"
alias wifi="${ROOT} wpa_cli status"
alias x="startx"
alias yt="youtube-dl --restrict-filenames --no-overwrites --write-info-json --write-thumbnail --no-call-home --force-ipv4 --format 'best[height<=720]'"
alias yt_max="youtube-dl --restrict-filenames --no-overwrites --write-info-json --write-thumbnail --no-call-home --force-ipv4"

alias x230_read_bot="flashrom -c 'MX25L6406E/MX25L6408E' -p linux_spi:dev=/dev/spidev0.0,spispeed=512 -r bottom.rom.${today}"
alias x230_read_top="flashrom -c 'MX25L3206E/MX25L3208E' -p linux_spi:dev=/dev/spidev0.0,spispeed=512 -r top.rom.${today}"
alias x230_write_bot="flashrom -c 'MX25L6406E/MX25L6408E' -p linux_spi:dev=/dev/spidev0.0,spispeed=512 -w coreboot-bottom.rom"
alias x230_write_top="flashrom -c 'MX25L3206E/MX25L3208E' -p linux_spi:dev=/dev/spidev0.0,spispeed=512 -w coreboot-top.rom"

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

# shellcheck disable=SC2154,SC2250,SC2312

days_until() {
  echo $(( ($(date -d "${1}" +%s) - $(date +%s)) / 86400 ))
}

# dedupe() {
#   time find "${@}" ! -empty -type f -exec md5sum {} + | \
#     sort | uniq -w32 -dD
# }
#
# dedupe_fast() {
#   time find "${@}" ! -empty -type f -exec crc32 {} + | \
#     sort | uniq -w8 -D
# }

dns_cf() {
  curl -H "Accept: application/dns-json" \
    "https://1.1.1.1/dns-query?name=${1}"
}

domain() {  # truncate to top level domain
  awk -S -F "." '!/^\s*$/{print ( $(NF-1)"."$(NF) )}' "${1}"
}

gas() {  # get CIDRs for AS number
  whois -h whois.radb.net '!g'"${1}"
}

get_goes() {
  # shellcheck disable=SC2312
  curl -s -o "geo.${1}.$(date -u '+%Y%m%dT%H:%M').jpg" \
    "https://cdn.star.nesdis.noaa.gov/GOES18/ABI/SECTOR/${1}/GEOCOLOR/latest.jpg"
}

adbpkg() {
  for p in $(adb shell pm list package|awk -S -F "package:" '{print $2}'); \
    do printf '%s' "${p}: "
      adb shell dumpsys package "${p}" | \
        grep -i versionname | \
          awk -S -F "=" '{print $2}'
  done > adb.pkg."${today}"
}

audit_getcap() {
  getcap -r "${1:=.}" 2>/dev/null
}

audit_setuid() {
  find "${1:=.}" -type f -perm -04000 -ls 2> /dev/null
}

backup() {
  cp -i "${1}" "${1}.${ts}"
}

# cert() {
#   cn="${1:-${ts}}"
#   expire="${2:-8}"
#   openssl req -new \
#     -newkey rsa:4096 -nodes \
#     -subj "/CN=${cn}" \
#     -x509 -sha512 -days "${expire}" \
#     -keyout "s.${cn}.pem" -out "s.${cn}.crt"
#   openssl x509 -in "s.${cn}.crt" -noout \
#     -subject -issuer -dates -serial
#   for ft in \-sha1 \-sha256 \-sha512 ; do \
#     openssl x509 -in "s.${cn}.crt" -noout \
#     -fingerprint ${ft} | tr -d ":" ; done
# }
#
# cidr() {
#   whois -h "whois.arin.net" -- \
#   "n + $(curl -s https://icanhazip.com/)" | \
#     grep "CIDR:"
# }
#
# colours() {
#   for i in {001..255} ; do \
#     printf "\x1b[38;5;${i}m${i}\n" | \
#     tr "\n" " " ; done | fold -w 255
# }
#
# convert_epoch() {
#   date -d "@${1}"
# }
#
# convert_secs() {
#   ((h=${1}/3600)) ; ((m=(${1}%3600)/60)) ; ((s=${1}%60))
#   printf "%02d:%02d:%02d\n" ${h} ${m} ${s}
# }
#
# dump_arp() {
#   ${ROOT} tcpdump -eni ${NETWORK} -w arp-${ts}.pcap \
#     "ether proto 0x0806"
# }
#
# dump_icmp() {
#   ${ROOT} tcpdump -ni ${NETWORK} -w icmp-${ts}.pcap \
#     "icmp"
# }
#
# dump_pflog() {
#   ${ROOT} tcpdump -ni pflog0 -w pflog-${ts}.pcap \
#     "not icmp6 and not host ff02::16 and not host ff02::d"
# }
#
# dump_syn() {
#   ${ROOT} tcpdump -ni ${NETWORK} -w syn-${ts}.pcap \
#     "tcp[13] & 2 != 0"
# }
#
# dump_udp() {
#   ${ROOT} tcpdump -ni ${NETWORK} -w udp-${ts}.pcap \
#     "udp and not port 443"
# }
#
# dump_dns() {
#   tshark -i ${NETWORK} -Y "dns.flags.response == 1" -Tfields \
#     -e frame.time_delta -e dns.qry.name -e dns.a -Eseparator=,
# }
#
# dump_http() {
#   tshark -i ${NETWORK} -Y "http.request or http.response" -Tfields \
#     -e ip.dst -e http.request.full_uri -e http.request.method \
#       -e http.response.code -e http.response.phrase -Eseparator=,
# }
#
# dump_ssl() {
#   tshark -i ${NETWORK} -Y "ssl.handshake.certificate" -Tfields \
#     -e ip.src -e x509sat.uTF8String -e x509sat.printableString \
#       -e x509sat.universalString -e x509sat.IA5String \
#         -e x509sat.teletexString -Eseparator=,
# }
#
# e() {
#   printf "%s entropy bits in %s characters at %s length\n" \
#     $(awk -S -v c="${1}" -v l="${2}" "BEGIN { print log(c^l)/log(2) }") \
#       "${1}" "${2}"
# }
#
# encrypt() {  # list preferred id last
#   output="${HOME}/$(basename ${1}).${today}.enc"
#   gpg --armor --encrypt \
#     --output "${output}" \
#     -r "0xFF00000000000000" \
#     -r "yubikey@example" \
#     "${1}" && echo "${1} -> ${output}"
# }
#
# f() {
#   find . -iname "*${1}*"
# }
#
# fail() {
#   tput setaf 1 ; printf "ERROR: %s\n" "${1}" ; tput sgr0
# }
#
# fd() {
#   find . -iname "*${1}*" -type d
# }
#
# firefox_history() {
#   sqlite3 "${1:=places.sqlite}" ".mode line" \
#     "select title, url, datetime(last_visit_date/1000000, 'unixepoch') \
#     as visit from moz_places order by last_visit_date desc;"
# }
#
# length() {
#   awk -S -v len="${1:=80}" 'length($0) > len'
# }
#
# lock() {
#   date
#   xhost >/dev/null 2>&1 || return
#   (sleep 1 ; slock) 2>/dev/null &
#   sleep 1 ; systemctl suspend
# }
#
# md() {
#   mkdir -p "${1:-${today}}" && cd "${1:-${today}}"
# }
#
# mem() {
#   ps -eo rss,pid,euser,args:100 --sort "%mem" | grep -i "${@}" | \
#     awk -S '{printf $1/1024 "MB"; $1=""; print }'
# }
#
# # function myip {
# #   curl -s "https://icanhazip.com/" || \
# #     dig @resolver1.opendns.com ANY myip.opendns.com +short }
#
# # function pass {
# #   LC_ALL=C tr -dc "A-Z2-9" < /dev/urandom | \
# #     tr -d "IOUS5" | \
# #     fold  -w  ${PASS_FOLD:-4} | \
# #     paste -sd ${PASS_DELIM:--} - | \
# #     head  -c  ${PASS_LENGTH:-29}
# #   echo }
#
# # function pdf {
# #   mupdf -r 180 -C FDF6E3 "${1}" }
#
# # function percent_diff {
# #   printf "%s%% diff between %s and %s\n" \
# #     $(awk -S -v x="${1}" -v y="${2}" "BEGIN { print (y-x)/x * 100 }") \
# #       "${1}" "${2}" }
#
# # function png2jpg {
# #   for png in $(find . -type f -name "*.png") ; do
# #     image="${png%.*}"
# #     convert "${image}.png" "${image}.jpg" ; done }
#
# newline_to_comma() {
#   sed -z 's/\n/,/g' ${1}
# }
#
# nonlocal() {
#   grep -E -ve "^#|^255.255.255.255|^127.|^0.|^::1|^ff..::|^fe80::" "${1}" | \
#     grep -E -e "[1,2]|::"
# }
#
# nxdomains() {
#   for x in $(${ROOT} grep NXDOMAIN /var/log/dnsmasq | \
#     awk -S '{print $6}' | sort | uniq) ; do printf "0.0.0.0 $x\n" ; done
# }
#
# rand() {
#   for item in \
#     '[:digit:]' '[:upper:]' \
#     '[:xdigit:]' '[:alnum:]' '[:graph:]' ; do \
#       tr -dc "${item}" < /dev/urandom | \
#       fold -w 64 | head -n 3 | \
#       sed "-es/./ /"{1..64..16} ; done
# }
#
# rand_mac() {
#   openssl rand -hex 6 | sed "s/\(..\)/\1:/g; s/.$//"
# }
#
# rand_pass() {
#   LC_ALL=C tr -dc 'A-Z1-9' < /dev/urandom | \
#     tr -d "1IOS5U7T" | fold -w 30 | head -n10 | \
#     sed "-es/./ /"{1..26..5} | cut -c2- | tr " " "-"
# }
#
# resize_ff() {
#   xdotool windowsize \
#     $(xdotool search --name firefox | tail -n1) 1366 768
# }
#
# reveal() {
#   output=$(echo "${1}" | rev | cut -c16- | rev)
#   gpg --decrypt --output ${output} "${1}" \
#     && echo "${1} -> ${output}"
# }
#
# rs() {
#   rsync --verbose --archive --human-readable \
#         --progress --stats --ipv4 --compress \
#         --log-file=$(mktemp) "${@}"
# }
#
# secret() {
#   output="${HOME}/$(basename ${1}).${today}.enc"
#   gpg --armor --symmetric \
#       --output ${output} \
#       "${1}" && echo "${1} -> ${output}"
# }
#
# sort_ip() {
#   sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n "${@}"
# }
#
# sort_length() {
#   awk -S '{print length, $0}' | sort -n | cut -d " " -f2-
# }
#
# srl() {
#   doas cu -r -s 115200 -l cuaU0 2>/dev/null || \
#     sudo minicom -D /dev/ttyUSB0 2>/dev/null || \
#       printf "serial console disconnected\n"
# }
#
# top_size() {
#   du -hd1 . | awk '{printf "%s %08.2f\t%s\n",
#     index("KMG", substr($1, length($1))),
#     substr($1, 0, length($1)-1), $0}' |
#   sort -r | cut -f2,3
# }
#
# username() {  # "username 8" - generate 8 usernames
#   for i in {1..${1}} ; do
#   printf "%s%s\n" \
#     "$(awk -S 'length > 2 && length < 12 {print(tolower($0))}' \
#     /usr/share/dict/words | grep -v "'" | sort -R | head -n2 | \
#     tr "\n" "_" | iconv -f utf-8 -t ascii//TRANSLIT)" \
#     "$(tr -dc "[:digit:]" < /dev/urandom | fold -w 4 | head -1)"
#   done
# }
#
# vpn() {
#   ssh -C -N -L 5555:127.0.0.1:8118 vpn
# }
#
# # zshaddhistory() {
# #   whence "${${(z)1}[1]}" >| /dev/null || return 1
# #   local line cmd
# #   line=${1%%$'\n'}
# #   cmd=${line%% *}
# #   [[ ${#line} -ge 5 \
# #     && ${cmd} != (apm|apt-cache|b|base64|bzip2|cal|calc|cat|cd|chmod|convert|cp|curl|cvs|date|df|dig|dimages|disklabel|dmesg|doas|download|du|e|egrep|enc|ent|et|exiftool|f|fdisk|feh|ffplay|file|firejail|gimp|git|gpg|grep|hdiutil|head|hostname|ifconfig|kill|less|libreoffice|lp|ls|mail|make|man|mem|mkdir|mnt|mount|mpv|mv|myip|nc|nvtop|openssl|patch|pdf|pdfinfo|pgrep|ping|pkg_info|pkill|proc|ps|pylint|rand|rand_pass|rm|rsync|scp|screenfetch|scrot|set|sha256|secret|sort|srm|ssh|ssh-keygen|startx|stat|strip|sudo|sysctl|tar|tmux|top|umount|uname|unzip|upload|uptime|useradd|vlc|vi|vim|wc|wget|which|whoami|whois|wireshark|xclip|xxd|ykman|yt|./pwd.sh|./purse.sh)
# #   ]]
# # }
#
# zzz() {
#   /usr/sbin/zzz 2>/dev/null || \
#     systemctl suspend
# }
#
# path() {
#   if [[ -d "${1}" ]] ; then
#     if [[ -z "${PATH}" ]] ; then
#       export PATH="${1}"
#     else
#       export PATH="${PATH}:${1}"
#     fi
#   fi
# }
#
# #export PATH=""
# #path "/usr/local/sbin"
# #path "/usr/local/bin"
# #path "/usr/sbin"
# #path "/usr/bin"
# #path "/sbin"
# #path "/bin"
# #path "/usr/local/go/bin"
# ##path "/usr/games"
# ##path "/usr/X11R6/bin"
#
# #export PASS_DELIM="-"
# #export PASS_FOLD=6
# #export PASS_LENGTH=34
#
# #export PWDSH_CLIP="xclip"
# #export PWDSH_CLIP_ARGS="-i -selection clipboard"
# #export PWDSH_DEST="screen"
# #export PWDSH_TIME=5
# #export PWDSH_COMMENT="pwd.sh ${HOST} ${today}"
# #export PWDSH_DAILY=1
# #export PWDSH_COPY=1
# #export PWDSH_ECHO="*"
# #export PWDSH_LEN=20
# #export PWDSH_SAFE="safe"
# #export PWDSH_INDEX="pwd.index"
# #export PWDSH_BACKUP="pwd.${HOST}.${today}.${ts}.tar"
# #export PWDSH_CHARS="A-Z0-9!?"
# #export PWDSH_PEPPER="${HOME}/pwd.pepper"
#
# #export PURSE_CLIP=${PWDSH_CLIP}
# #export PURSE_CLIP_ARGS=${PWDSH_CLIP_ARGS}
# #export PURSE_DEST=${PWDSH_DEST}
# #export PURSE_TIME=${PWDSH_TIME}
# #export PURSE_COMMENT="purse ${HOST} ${today}"
# #export PURSE_DAILY=${PWDSH_DAILY}
# #export PURSE_COPY=${PWDSH_COPY}
# #export PURSE_ECHO=${PWDSH_ECHO}
# #export PURSE_LEN=${PWDSH_LEN}
# #export PURSE_SAFE=${PWDSH_SAFE}
# #export PURSE_INDEX="purse.index"
# #export PURSE_BACKUP="purse.${HOST}.${today}.${ts}.tar"
# #export PURSE_CHARS=${PWDSH_CHARS}
# #export PURSE_ENCIX=1
#
# #export HOMEBREW_CASK_OPTS=--require-sha
# #export HOMEBREW_NO_ANALYTICS=1
# #export HOMEBREW_NO_AUTO_UPDATE=1
# #export HOMEBREW_NO_INSECURE_REDIRECT=1
#
# #export VAULT_ADDR="http://127.0.0.1:8200"
# #export VAULT_ADDR="https://vault.local:8200"
#
# #export GNUPGHOME="${HOME}/.gnupg"
# #export GPG_TTY="$(tty)"
# #export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)
# #gpgconf --launch gpg-agent
# #gpg-connect-agent updatestartuptty /bye >/dev/null
#
# # https://github.com/drduh/gone
# export gone_proto="http"
# export gone_host="localhost"
# #export gone_host="192.168.1.1"
# export gone_port="8080"
# export gone_auth="mySecret"
# export gone_header="X-Auth"
# export gone_server="${gone_proto}://${gone_host}:${gone_port}"
# export gone_cmd="curl -s -H \"${gone_header}: ${gone_auth}\" ${gone_server}"
#
# # alias gone_list='"${gone_cmd-}"/list | jq'
# # alias gone_stat='"${gone_cmd-}"/heartbeat | jq'
# # alias gone_static='"${gone_cmd-}"/static | jq'
# # alias gone_random='"${gone_cmd-}"/random/'
#
# gone_put() {
#   curl -s -F "file=@${1}" -F "downloads=${2:-3}" -F "duration=${3:-20m}" \
#     -H "${gone_header}: ${gone_auth}" "${gone_server}/upload" | jq
# }
#
# gone_get() {
#   curl -s -H "${gone_header}: ${gone_auth}" \
#     "${gone_server}/download/${1}"
# }
#
# gone_msg() {
#   curl -s -H "${gone_header}: ${gone_auth}" \
#     -F "message=${1}" "${gone_server}/msg"
# }
