echo "Debian: $(cat /etc/debian_version)"

alias tt='zellij'
alias tls='zellij list-sessions'
alias t='zellij attach'
alias tat='zellij attach'
alias tlr='zellij delete-all-sessions'

alias cr='cd /opt/rust && ls'
alias cg='cargo'
alias cgb='cargo build --release'
alias cgbt='cargo build'
alias rc='rustc'

alias gc='git clone'
alias ga='git add'

function gcm() {
  git commit -m "$1"
}

alias gp='git commit -m "手动提交" && git push'

yt() {
	yt-dlp -o "%(epoch>%Y-%m-%d_%H-%M-%S)s.%(ext)s" "$1"
}

alias g='gemini'
gr() {
  if [ -n "$1" ]; then
    gemini --resume "$1"
  else
      gemini --resume
  fi
}
alias gl='gemini --list-sessions'

alias cx='codex'
alias cl='claude'
alias clc='claude -c'
alias clt='cd /opt/tmp && claude'
alias nhs='micro ~/.bash_history'

alias op='openlist'
alias ct='cd /opt/tmp'
alias nsh='micro ~/.ssh/config'
alias las='echo $SSH_CLIENT'
alias dd='exit'
alias csh='cd /opt/data/oplist/share && ls'
alias tal='tar -tvf'
tax() {
    if [ $# -ne 2 ]; then
        echo "Usage: tax <archive.tar.*> <destination_directory>"
        return 1
    fi

    local archive="$1"
    local destination="$2"

    case "$archive" in
        *.tar)
            tar -xvf "$archive" -C "$destination"
            ;;
        *.tar.gz|*.tgz)
            tar -xzvf "$archive" -C "$destination"
            ;;
        *.tar.bz2|*.tbz2)
            tar -xjvf "$archive" -C "$destination"
            ;;
        *.tar.xz|*.txz)
            tar -xJvf "$archive" -C "$destination"
            ;;
        *)
            echo "Unsupported archive format: $archive"
            return 1
            ;;
    esac
}

tac() {
    if [ $# -lt 2 ]; then
        echo "Usage: tac <archive.tar.*> <file_or_directory1> [<file_or_directory2> ...]"
        return 1
    fi

    local archive="$1"
    shift  # 移除第一个参数，剩下的都是要压缩的文件或目录

    case "$archive" in
        *.tar)
            tar -cvf "$archive" "$@"
            ;;
        *.tar.gz|*.tgz)
            tar -czvf "$archive" "$@"
            ;;
        *.tar.bz2|*.tbz2)
            tar -cjvf "$archive" "$@"
            ;;
        *.tar.xz|*.txz)
            tar -cJvf "$archive" "$@"
            ;;
        *)
            echo "Unsupported archive format: $archive"
            return 1
            ;;
    esac
}

alias csv='cd /etc/systemd/system/ && ls'
alias hs='history'
alias lg='journalctl -xeu'
coa() {
    conda activate $1
}

cod() {
    conda deactivate
}

coc() {
    if [ -z "$1" ]; then
        echo "Usage: cac <env_name> [python_version]"
        return 1
    fi

    if [ -z "$2" ]; then
        echo "y" | conda create --name "$1"
    else
        echo "y" | conda create --name "$1" python="$2"
    fi
}

alias col='conda env list'

cor() {
    conda env remove --name $1 --yes
}

alias py='python'
uvc() {
  uv venv --python $1
}
alias uva='source .venv/bin/activate'

alias sts='systemctl status'
alias sdr='systemctl daemon-reload'

function str() {
  systemctl start "$1" && systemctl status "$1"
}

function stp() {
  systemctl stop "$1" && systemctl status "$1"
}

function rstr() {
  systemctl restart "$1" && sleep 3 && systemctl status "$1"
}

alias enable='systemctl enable'
alias disable='systemctl disable'
alias sdr='systemctl daemon-reload'
alias a='aria2c -c -s4 -x4'
alias pon="export ALL_PROXY=http://localhost:2080 https_proxy=http://localhost:2080 http_proxy=http://localhost:2080 && curl cip.cc"
alias pof="unset ALL_PROXY http_proxy https_proxy && curl cip.cc"
alias d='docker'
alias ds='docker stop'
alias dst='docker stats'
alias dlg='docker logs'
alias dp='docker ps'
alias dbp='echo "y" | docker builder prune'
alias dpa='docker ps -a'
alias dcu='docker compose up'
alias dcud='docker compose up -d'
alias dcd='docker compose down'
alias dcp='docker compose pull'
alias dcr='docker compose restart'
alias dim='docker images'
alias dvl='docker volume ls'
alias dpr='echo "y" | docker image prune && docker images'
alias docker-compose='docker compose'
alias dip="docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'"

dpwd() {
  docker run --rm authelia/authelia:latest authelia crypto hash generate argon2 --password '$1'
}

alias cdk="cd /opt/dk && ls"

dt() {
    docker run --rm -it $1 sh
}

dbd() {
    docker build -t git.nvme.cc/adminroot/$1 .
    docker images
}

alias dvr='docker volume rm -f'
alias dfv='docker system df -v'

function ua() {
    ufw allow $1 && ufw status
}
function ud() {
    ufw delete allow $1 && ufw status
}
alias us='ufw status'
function ura() {
    ufw route allow $1 && ufw status
}
function urd() {
    ufw route delete allow $1 && ufw status
}

alias w='wget --content-disposition'
alias h='htop'

c() {
    if [ -z "$1" ]; then
        cd ~
    else
        cd "$1"
    fi
    ls
}

alias cdd='cd ../ && ls'
shopt -s autocd
alias ce='cd /etc && ls'
alias co='cd /opt && ls'
alias dfh='df -h | grep -v '/var/lib/container' | grep -vE '^tmpfs' | grep -vE '^overlay''
alias sr='source /etc/profile && source ~/.bashrc'
alias nn='nano'
alias nnd='micro compose.yml'
alias dush='du -sh'

function ddt() {
  echo "输入测速文件大小(MB) [默认: 2048]: "
  read -t 30 -p "> " testfile_size
  if [ $? -ne 0 ] || [ -z "$testfile_size" ]; then
    testfile_size=2048
    echo -e "\n使用默认值: ${testfile_size}MB"
  fi
  echo "-------------"
  echo "写入速度如下:"
  time dd if=/dev/zero of=./testfile bs=1M count=${testfile_size} oflag=direct
  echo "-------------"
  echo "读取速度如下:"
  time dd if=./testfile of=/dev/null bs=1M
  rm ./testfile
  echo "-------------"
  echo "测试文件已删除"
}

function din() {
  docker exec -it "$1" /bin/sh
}

function dinb() {
  docker exec -it "$1" /bin/bash
}

portcheck() {
    lsof -i :$1
}
alias p='portcheck'

countfiles() {
    dir="${1:-.}"
    find "$dir" -type f | rev | cut -d"." -f1 | rev | sort | uniq -c
}
alias lst=countfiles

lsd() {
    du -h --max-depth=1 $1 | sort -h
}

findfile() {
    find . -type f -name "*.$1" -exec du -h '{}' + | sort -h
}
alias f='findfile'

alias nb='micro ~/.bashrc'
alias na='micro ~/.alias.txt'
alias nau='rm ~/.alias.txt && wget https://git.nvme.cc/adminroot/setup/raw/branch/main/src/alias.txt -O ~/.alias.txt'
alias clog='sudo journalctl --vacuum-time=1d'
alias mk='mkdir -p'

bak() {
    cp "$1" "$1.bak"
}

alias l='ls --color=auto'
alias s='ls --color=auto'

alias cc='cbm'

alias v2='v2ray'

alias ary='apt remove -y'
alias auy='apt update -y'
alias aiy='apt install -y'
alias agy='apt upgrade -y'
alias ash='apt search'

alias pir='pip install -r'

function pir-cn() {
  pip install -r $1 -i https://mirrors.ustc.edu.cn/pypi/web/simple
}

alias rm='rm -r'
alias cp='cp -r'

alias 7za='7z a -m0=Copy'
function 7zx() {
  local archive="$1"
  local outdir="${2:-.}"

  if [[ ! -f "$archive" ]]; then
    echo "❌ 文件不存在: $archive" >&2
    return 1
  fi

  mkdir -p "$outdir"

  local lower_name="${archive,,}"  # 全小写处理后缀名，提升兼容性

  # 支持的 .tar.* 格式（多层压缩）
  local tar_pattern='\.(tar\.xz|txz|tar\.gz|tgz|tar\.bz2|tbz2|tar\.zst|tzst)$'

  # 支持的直接压缩格式
  local direct_pattern='\.(zip|7z|rar)$'

  if [[ "$lower_name" =~ $tar_pattern ]]; then
    7z x -so "$archive" | 7z x -si -ttar -o"$outdir"
  elif [[ "$lower_name" =~ $direct_pattern ]]; then
    7z x "$archive" -o"$outdir"
  else
    echo "⚠️ 不支持的格式: $archive" >&2
    return 2
  fi
}

function 7zxd() {
  local password="$1"
  local files=()

  while IFS= read -r -d '' f; do
    files+=("$f")
  done < <(find . -maxdepth 1 -type f \( \
    -iname '*.zip' -o -iname '*.7z' -o -iname '*.rar' -o \
    -iname '*.tar.gz' -o -iname '*.tgz' -o -iname '*.tar.bz2' -o -iname '*.tbz2' -o \
    -iname '*.tar.xz' -o -iname '*.txz' -o -iname '*.tar.zst' -o -iname '*.tzst' -o \
    -iname '*.part1.rar' -o -iname '*.001' \
    \) -print0)

  if [[ ${#files[@]} -eq 0 ]]; then
    echo "📂 当前目录下无可解压的压缩包"
    return 0
  fi

  for file in "${files[@]}"; do
    echo "📦 正在解压: $file"
    local base="${file%.*}"

    if [[ -n "$password" ]]; then
      7z x "$file" -p"$password" -aoa -y
    else
      7z x "$file" -aoa -y
    fi

    if [[ $? -eq 0 ]]; then
      echo "✅ 解压成功，删除源文件..."

      local prefix="${file%%.*}"
      find . -maxdepth 1 -type f -regex "./$prefix\\..*" -delete
    else
      echo "❌ 解压失败: $file"
    fi
  done
}

mtm() {
    find $1 -type f \( -iname "*.mp4" -o -iname "*.avi" -o -iname "*.mov" \) -exec mv {} $2 \;
}

alias ip4='curl cip.cc -4'
alias ip6='curl ip.sb -6'

alias nnc='micro /opt/dk/caddy/conf/Caddyfile && docker compose -f /opt/dk/caddy/compose.yml exec caddy caddy reload --config /etc/caddy/Caddyfile'
alias csr='docker compose -f /opt/dk/caddy/compose.yml exec caddy caddy reload --config /etc/caddy/Caddyfile'
alias cap='caddy hash-password'

alias psr='systemctl restart php8.2-fpm && systemctl status php8.2-fpm'

alias ll='ls -a'
