#!/bin/bash

hyfetch 2>/dev/null || true

# Quality of Life
#enable using sudo with functions
function Sudo {
  local firstArg=$1
  if [ $(type -t $firstArg) = function ];then
    shift && command sudo bash -c "$(declare -f $firstArg);$firstArg $*"
  elif [ $(type -t $firstArg) = alias ];then
    alias sudo='\sudo '
    eval "sudo $@"
  else
    command sudo "$@"
  fi
}
#find in history
alias h="history|grep "
#search files in current and sub dirs
#alias f="find . |grep "
find_file() {
  find . -name *"${1}"*
}
#find running processes
alias p="ps aux |grep "
#open file
alias o="open "
#find large files
#alias find_largest_files="du -h -x -s -- * | sort -r -h | head -20"
sort_filesizes() {
  [ -z ${1} ] && local dir="$(pwd)" || local dir="${1}"
  du -ah --max-depth=1 "$( realpath ${dir} )" | sort --human-numeric-sort
}
sort_filesizes-monitor() {
  [ -z ${1} ] && local dir="$(pwd)" || local dir="${1}"
  watch "du -ah --max-depth=1 $( realpath ${dir} ) | sort --human-numeric-sort"
}
#connect to android via adb as root
alias adb_root="adb shell -t exec run-as com.termux files/usr/bin/bash -c 'su -'"
#df -h without tmpfs
alias dfh="df -h | grep 'Use%\|/dev/'"
#dev environment toolbox with proper term env var
devpod(){
  podman container start debian-sid-dev
  podman exec --env POD_PATH=$(pwd | sed "s|${HOME}|/root|") -it debian-sid-dev /bin/bash
}
devpod-arm(){
  podman container start debian-sid-arm-dev
  podman exec --env POD_PATH=$(pwd | sed "s|${HOME}|/root|") -it debian-sid-arm-dev /bin/bash
}
simplewebserver(){
  screen python3 -m http.server
#  screen ssi-server
}
filecount(){
  [[ -z ${1} ]] && fc_targetdir=$(pwd) || fc_targetdir="${1}"
  find "${fc_targetdir}" -type f | wc -l
}
filecount-monitor(){
  [[ -z ${1} ]] && fc_targetdir=$(pwd) || fc_targetdir="${1}"
  watch "find ${fc_targetdir} -type f | wc -l"
}
#alias devbox-stable="TERM=xterm-256color toolbox enter debian-toolbox-12"
#alias devbox-testing="TERM=xterm-256color toolbox enter debian-toolbox-testing"
#git shortcut to quickly push all changes with comment
pushall() {
  git add .
  git commit -m "${1}"
  git push origin
  read -p "tag as well? (Y/n)" bool_tag
  [ -z ${bool_tag} ] && bool_tag="y"
  case "${bool_tag}" in
    [yY]|[yY][eE][sS])
      read -p "Tag version: " ver_tag
      git tag -a "${ver_tag}" -m "${ver_tag}"
      git push origin "${ver_tag}"
      ;;
  esac
}
#deb_canvas(){
#  dh_make -c custom --copyrightfile ~/Development/metapackages/pogmom_deb_template/licenses/oql -n -e "${GIT_AUTHOR_EMAIL}" -p pogmom-essentials_0.1 -C i
#}


alias quickdeb='dpkg-buildpackage -b -tc'
alias gituntracked='git ls-files . --exclude-standard --others'

#apt update summary
alias pm-update="sudo apt update;apt list --upgradable"
alias pm-upgrade="sudo apt update;sudo apt upgrade"

#User Env Vars
export DEBEMAIL="support@pogmom.me"
export DEBFULLNAME="Penelope Gwen"

#SSH Personal Machines
alias pogmommy-omen-328p1aa="ssh pogmommy-omen-328p1aa"
alias pogmommy-apple-a2337="ssh pogmommy-apple-a2337"
alias pogmommy-apple-a2337-vpn="ssh pogmommy-apple-a2337-vpn"
alias pogmommy-dell-p135g="ssh pogmommy-dell-p135g"

#Git env vars
GIT_AUTHOR_NAME="Penelope Gwen"
GIT_AUTHOR_EMAIL="penelope@pogmom.me"
GIT_EDITOR="/usr/bin/nano"

#Bash Prompt Customization
prompt_prefix=""
if [ -n "${SSH_CLIENT}" ] || [ -n "${SSH_TTY}" ]; then
  prompt_prefix="${prompt_prefix} [SSH] "
fi
if [[ "$HOSTNAME" == pod* ]];then
  prompt_prefix="${prompt_prefix} [POD] "
  mkdir -p "${HOME}/.devpod/$(uname -m)"
  export CARGO_HOME="${HOME}/.devpod/$(uname -m)/.cargo"
  export RUSTUP_HOME="${HOME}/.devpod/$(uname -m)/.rustup"
  cd ${POD_PATH}
fi

export prompt_prefix=`awk '{$1=$1};1' <<< "${prompt_prefix}"`
#PS1='\[\e[1m\e[95m\]${prompt_prefix}\[\e[m\]${debian_chroot:+($debian_chroot)}\[\e[01;32m\]\u@\h\[\e[00m\]:\[\e[01;34m\]\w\[\e[00m\]\n\$ '
PS1='\[\e[1m\e[95m\]${prompt_prefix}\[\e[m\]${debian_chroot:+($debian_chroot)}\[\e[01;32m\]\u@\h\[\e[00m\]:\[\e[01;34m\]$(pwd|sed "s|${HOME}|~|"|perl -pe "s/(?<!(^~))(\\/.*)(?=\\/.*\\/)/\\/…/s")\[\e[00m\]\n\$ '
#PS1='\[\e[1m\e[95m\]${prompt_prefix}\[\e[m\]${debian_chroot:+($debian_chroot)}\[\e[01;32m\]\u@\h\[\e[00m\]:\[\e[01;34m\]$(pwd|sed "s|${HOME}|~|"|perl -pe "s/(?<!(^~))(\\/.*)(?=\\/.*\\/)/\\/…/s"|xargs printf "\\e]2;%s — Alacritty\007")\[\e[00m\]\n\$ '

#Alacritty custom window title
if [ "$TERM" = alacritty ];then
  PROMPT_COMMAND='pwd|sed "s|${HOME}|~|"|perl -pe "s/(?<!(^~))(\/.*)(?=\/.*\/)/\/…/s"|xargs printf "\e]2;%s — Alacritty\007"'
fi

bind TAB:menu-complete
