#!/usr/bin/env bash

if [ "$TERM" == 'xterm-kitty' ]; then
   alias ssh='kitty +kitten ssh'
   alias diff='kitty +kitten diff'
fi

DEFAULT_RSYNC='--info=ALL --recursive --delay-updates --human-readable --links --hard-links --perms'

alias l='ls -l -v --all --human-readable --classify --group-directories-first' # -lvahF --group-directories-first
alias lt=='l --time-style=long-iso'
alias r='reset'
alias ..='cd ..'
alias refresh_bashrc='. ~/.bashrc' # alternatively: 'source ~/.bashrc'
alias rsync_default="rsync $DEFAULT_RSYNC --checksum"
# source /usr/share/bash-completion/completions/rsync
# complete -F _rsync rsync_default
alias rsync_backup="rsync $DEFAULT_RSYNC --checksum --times --group --owner --delete"
alias rsync_move="rsync $DEFAULT_RSYNC --checksum --remove-source-files"
alias rsync_update="rsync $DEFAULT_RSYNC --update --times"
alias rsync_copy="rsync $DEFAULT_RSYNC --ignore-times"
alias copy_link='cp --no-dereference --recursive --preserve=all --link' # not --force to make it optional
alias off='systemctl poweroff'
alias nnn='nnn -dHrR'
alias nn='n -dHrR'
alias c='clear'
alias sort_dirs_by_size='du --block-size=1K --human-readable --max-depth=1 | sort --human-numeric-sort --reverse' # 'du -kh --max-depth=1 | sort -hr'
alias sd='sudo ' # alias, so sudo can use aliases: "If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion."
alias mountdrive='udisksctl mount -b'
alias unmountdrive='udisksctl unmount -b'
alias fssizes='df -kh --output=size,used,avail,pcent,target | sort -hr'
alias dirsizes='du -kh --apparent-size --max-depth=1 | sort -hr'
# sed -ie 's/Beispiel/Ersetzung/' *.xml
# sed -Eie 's/(Beispiel)/\1Anhang/' *.xml
alias start_x11_vnc="x11vnc -many -display :0 -no6 -rfbport 5900 -auth /var/run/lightdm/root/:0 -rfbauth $HOME/.vnc/passwd"


rsyncLink() {
   source=$1;
   linkSource="$(realpath $source)";
   linkSource=${linkSource%/}; # cut off trailing "/"
   shift;
   dest=$1;
   shift;
   rsync $DEFAULT_RSYNC --one-file-system --link-dest=$linkSource $source $dest $*;
}


updateSystem() {
  executeAndNotify "doUpdateSystem $1" "system updated" "system update failed"
}


doUpdateSystem() {
   updatePackages $1;
   updateFlatpak $1;
}


updateFlatpak() {
   noconfirm=
   if [[ $1 == "-y" ]]; then
      noconfirm="--assumeyes";
   fi
   executeAndNotify "doUpdateFlatpak $noconfirm" "flatpaks updated" "flatpak update failed";
}


doUpdateFlatpak() {
   flatpak update $1;
   flatpak remove --unused $1;
}


# executes command and notifies upon failure
execute() {
   dir="$(pwd)";
   dir=${dir##*/};
   if [[ -n "$2" ]]; then
      errLog=$2;
   else
      errLog="execution of \"$1\" failed";
   fi

   if ! $1; then
      notifyError "$dir: $errLog";
      return 1;
   fi
}


# executes command and notifies upon success or failure
executeAndNotify () {
   dir="$(pwd)";
   dir=${dir##*/}; # cut off previous path (/path/to/foo.txt -> foo.txt)
   if [[ -n "$2" ]]; then
      winLog=$2;
   else
      winLog="execution of \"$1\" succeeded";
   fi

   if ! execute "$1" "$3"; then # arguments in quotes so they are interpreted as ONE argument each by execute()
      return 1;
   fi
   notifyInfo "$dir: $winLog";
}


# find file but don't print errors (e.g. can't access directory etc.)
finds () {
   find $* 2>/dev/null;
}


# sends a desktop-notification with an icon signalling an error
notifyError () {
   notify-send "$1" --icon=data-warning;
}


# sends a desktop-notification with an icon signalling a simple information
notifyInfo () {
   notify-send "$1" --icon=preferences-desktop-notification;
}