# shellcheck shell=bash
# aliases.sh — modular aliases, stow-friendly
# Repo path: bashrc/.config/bash/aliases.sh -> stows to ~/.config/bash/aliases.sh
# Sourced from ~/.bashrc via: source_shell_file "${XDG_CONFIG_HOME:-$HOME/.config}/bash/aliases.sh"
# Ubuntu-style visibility: `ls` hides dotfiles; `la` (grid) and `ll` (long) show them.

[[ $- != *i* ]] && return 0

# Fallback if sourced standalone (normally provided by .bashrc)
declare -F has_tool >/dev/null 2>&1 || has_tool() { command -v "$1" >/dev/null 2>&1; }

#######################################################
# CORE ALIASES
#######################################################
# To temporarily bypass an alias, we precede the command with a \
# EG: the ls command is aliased, but to use the normal ls command you would type \ls

# Safety aliases
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
alias mkdir='mkdir -p'

# Enhanced commands
alias da='date "+%Y-%m-%d %A %T %Z"'
if [[ "$OSTYPE" == darwin* ]]; then
    alias ps='ps aux'
else
    alias ps='ps auxf'
fi
alias ping='ping -c 10'
alias less='less -R'
alias cls='clear'

# Do NOT alias grep->rg: flags differ (`grep -r PAT dir` vs `rg PAT dir`,
# `rg -r` means --replace). Keep GNU grep for scripts/helpers.
# To have colors for grep, egrep and zgrep
alias grep='grep --color=auto'

# Ripgrep with smart case (lowercase patterns match case-insensitively)
if has_tool rg; then
    alias rg='rg --smart-case --color=auto'
fi

# Navigation
alias home='cd ~'
alias cd..='cd ..'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'

# cd into the old directory
alias bd='cd "$OLDPWD"'

#######################################################
# CONDITIONAL TOOL ALIASES
#######################################################
# Ubuntu-style file visibility: `ls` hides dotfiles, `la`/`ll` show them.
if has_tool eza; then
    alias ls='eza --classify --color=auto --icons --group-directories-first'
    alias ll='eza --icons --long --all --group-directories-first --total-size --git --header --time-style=relative'
    alias la='eza -a --color=auto --group-directories-first'
else
    if [[ "$OSTYPE" == darwin* ]]; then
        alias ls='\ls -FhG'
        alias ll='\ls -alFhG'
        alias la='\ls -AhG'
    else
        alias ls='\ls -Fh --color=auto'
        alias ll='\ls -alFh --color=auto'
        alias la='\ls -Ah --color=auto'
    fi
fi

# Alias's for multiple directory listing commands
if [[ "$OSTYPE" == darwin* ]]; then
    alias lx='\ls -lhG'               # macOS: no -X flag, basic list
    alias lk='\ls -lSrhG'             # sort by size
    alias lc='\ls -ltcrhG'            # sort by change time
    alias lu='\ls -lturhG'            # sort by access time
    alias lr='\ls -lRhG'              # recursive ls
    alias lt='\ls -ltrhG'             # sort by date
    alias lm='\ls -alhG | more'       # pipe through 'more'
    alias lw='\ls -xAhG'              # wide listing format
    alias labc='\ls -lapG'            # alphabetical sort
    alias lf="\ls -lG | grep -v '^d'" # files only
    alias ldir="\ls -lG | grep '^d'"  # directories only
    alias lla='\ls -AlG'              # List and Hidden Files
    alias las='\ls -AG'               # Hidden Files
    alias lls='\ls -lG'               # List
else
    alias lx='\ls -lXBh --color=auto'             # sort by extension
    alias lk='\ls -lSrh --color=auto'             # sort by size
    alias lc='\ls -ltcrh --color=auto'            # sort by change time
    alias lu='\ls -lturh --color=auto'            # sort by access time
    alias lr='\ls -lRh --color=auto'              # recursive ls
    alias lt='\ls -ltrh --color=auto'             # sort by date
    alias lm='\ls -alh --color=auto | more'       # pipe through 'more'
    alias lw='\ls -xAh --color=auto'              # wide listing format
    alias labc='\ls -lap --color=auto'            # alphabetical sort
    alias lf="\ls -l --color=auto | grep -v '^d'" # files only
    alias ldir="\ls -l --color=auto | grep '^d'"  # directories only
    alias lla='\ls -Al --color=auto'              # List and Hidden Files
    alias las='\ls -A --color=auto'               # Hidden Files
    alias lls='\ls -l --color=auto'               # List
fi

# Tree alias: prefer tree command, fall back to eza --tree
if has_tool tree; then
    if tree --dirsfirst --help &>/dev/null 2>&1; then
        alias tree='tree -CAhF --dirsfirst'
    else
        alias tree='tree -CAhF'
    fi
    alias treed='tree -CAFd'
elif has_tool eza; then
    alias tree='eza --tree --icons=auto --color=auto'
fi

# Better cat.
# WARNING: `bat` adds headers/formatting vs plain `cat`, so never use it in scripts.
# `--paging=never` keeps it pipe-friendly.
if has_tool bat; then
    alias cat='bat --paging=never'
elif has_tool batcat; then
    alias cat='batcat --paging=never'
fi

#######################################################
# SYSTEM & UTILITY ALIASES
#######################################################
# Set the default editor
alias spico='sudo pico'
alias snano='sudo nano'

if has_tool nvim; then
    alias vim='nvim'
fi

# Remove a directory and all files
alias rmd='\rm -rvf'

# alias chmod commands
alias mx='chmod a+x'
alias 000='chmod -R 000'
alias 644='chmod -R 644'
alias 666='chmod -R 666'
alias 755='chmod -R 755'
alias 777='chmod -R 777'

# Search command line history
alias h="history | grep "

# Search running processes
alias p="ps aux | grep "
alias topcpu="/bin/ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10"

# Search files in the current folder
alias f="find . | grep "

# Show open ports
if [[ "$OSTYPE" == darwin* ]]; then
    alias openports='lsof -i -P -n | grep LISTEN'
else
    alias openports='netstat -nape --inet'
fi

# Alias's for safe and forced reboots
alias rebootsafe='sudo shutdown -r now'
if [[ "$OSTYPE" == darwin* ]]; then
    alias rebootforce='sudo shutdown -r now'
else
    alias rebootforce='sudo shutdown -r -n now'
fi
alias shutdownsafe='sudo shutdown -h now'

# Alias's to show disk space and space used in a folder
alias diskspace="du -S | sort -n -r |more"
alias folders='du -h -d 1'
alias folderssort='find . -maxdepth 1 -type d -print0 | xargs -0 du -sk | sort -rn'
alias mountedinfo='df -hT'

# Alias's for archives
alias mktar='tar -cvf'
alias mkbz2='tar -cvjf'
alias mkgz='tar -cvzf'
alias untar='tar -xvf'
alias unbz2='tar -xvjf'
alias ungz='tar -xvzf'

# Show all logs in /var/log
alias logs="sudo find /var/log -type f -exec file {} \; | grep 'text' | cut -d' ' -f1 | sed -e's/:$//g' | grep -v '[0-9]$' | xargs tail -f"

# alias to cleanup unused docker containers, images, networks, and volumes
alias docker-clean=' \
  docker container prune -f ; \
  docker image prune -f ; \
  docker network prune -f ; \
  docker volume prune -f '
