#!/bin/zsh

export vfat_mount_flags="-o rw,uid=1000,gid=1000,utf8,iocharset=utf8,codepage=866"

function take-first-part-media(){
    if [ $# != 3 ]; then
        echo "Executed commans: ffmpeg -i \"\$1\" -t \"\$3\" -c copy \"\$2\""
    else
        ffmpeg -i "$1" -t "$3" -c copy "$2"
    fi
}

function take-second-part-media(){
    if [ $# != 3 ]; then
        echo "Executed commans: ffmpeg -i \"\$1\" -ss \"\$3\" -c copy \"\$2\""
    else
        ffmpeg -i "$1" -ss "$3" -c copy "$2"
    fi
}

alias ghci="ghci -Wno-x-partial"

function nmrescan {
    nmcli device wifi rescan
}

function homeREBUILD () {
    home-manager switch
}

function nixZSHELL(){
    nix-shell --command zsh
}

docx_to_pdf () {
    if [ -z "$1" ]; then
        echo "docx_to_pdf <document path>" 1>&2
    else
        libreoffice --headless --convert-to pdf "$1"
    fi
}

download-audio(){
    if (( 2 <= $# && $# <= 3 )) ; then
        if (( $# > 2)); then
            local CODE=$3
        elif [ -z ${1##*soundcloud*} ]; then
            local CODE="hls_mp3_128"
        else
            local CODE="ba*"
        fi
        #yt-dlp -F "$1"
        echo "Using code $CODE"
        yt-dlp --extract-audio --format $CODE "$1" --audio-format mp3 --output "$2.%(ext)s" --verbose --no-playlist
    else
        echo "Wrong!!!!!"
    fi
}

convert_cmus_to_lfsl() {
  local src_dir="$HOME/.config/cmus/playlists"
  local dst_dir="$HOME/Music/playlists"

  mkdir -p "$dst_dir"

  for src in "$src_dir"/*; do
    [ -f "$src" ] || continue
    local base=$(basename "$src")
    local out="$dst_dir/${base%.*}.lfsl"

    printf 'Converting %s -> %s\n' "$src" "$out"
    # Empty or (re)create output file
    : > "$out"

    while IFS= read -r line; do
      # Only process non-empty lines
      [ -z "$line" ] && continue

      if [[ "$line" == "$HOME/Music/"* ]]; then
        # Strip the prefix and append
        echo "${line#"$HOME/Music/"}" >> "$out"
      else
        # Print warning for anything else
        printf 'Warning: skipping "%s" (not under $HOME/Music)\n' "$line" >&2
      fi
    done < "$src"
  done
}


download-video (){
    yt-dlp -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4" -o "%(title)s.mp4" $1
}

export amsterdam_server_ip="77.238.231.168"
export home_server_ip="81.200.28.227"

toamsterdamssh(){
    ssh -p 22 "root@${amsterdam_server_ip}"
}

tomyservssh(){
    ssh -p 22 "root@${home_server_ip}"
}

alias ls="ls -A --color=auto"
alias tree="tree -a -C"
alias grep="grep --colour=always"
alias cp="cp -r"
alias ascend="sudo -i zsh -l"
alias pm="pulsemixer"
alias ip="ip -color"
alias sisyphus="mpv --loop-file=inf"
alias COPY="xclip -i -f -selection primary | xclip -i -selection clipboard"
alias vgmch="valgrind --tool=memcheck"
alias vgmch-full="valgrind --leak-check=full --tool=memcheck"
alias pero="sqlite3 -cmd '.explain on' -box"
alias smatree="tree -I '.git|.idea|target|cmake-build-debug|.venv'"
alias df="df -h"
alias du="du -sh"

function ff (){
	if [ -n "$FF_MAIN_PROFILE" ]; then
		firefox -P "$FF_MAIN_PROFILE" $@
	else
		echo "Env var FF_MAIN_PROFILE not set"
	fi
}

function bctl {
    sudo rfkill unblock all
    bluetoothctl
}

function nixREPL {
    nix repl --expr 'let pkgs = import <nixpkgs> {}; lib = pkgs.lib; in pkgs'
}

function nixosREBUILD {
    sudo nixos-rebuild switch
}

function unblockall {
    sudo rfkill unblock all
}


function vibecheck { ping 8.8.8.8 }

function git-config-cred-store(){ git config credential.helper store }

function ffsilent { ffmpeg -i "$1" -c copy -an "${1%.*}-nosound.${1#*.}" }

function reSource { source /etc/zshrc.local }

function shitdown { sudo shutdown -h now }

function vpnup {
	if [ -n "$WG_VPN_NAME" ]; then
		wg-quick up "$WG_VPN_NAME"
	else
		echo "Env var WG_VPN_NAME is not set"
	fi
}
function vpndown {
	if [ -n "$WG_VPN_NAME" ]; then
		wg-quick down "$WG_VPN_NAME"
	else
		echo "Env var WG_VPN_NAME is not set"
	fi
}

function lockscreen-xfcre { xfce4-screensaver-command -l }

function logout-xfce { xfce4-session-logout --logout }

function cmusclear {
    rm ~/.config/cmus/lib.pl
}

# Whenever you feel bad, just open this pdf and your mood will be ruined completely
function showmekeyboard {
    xkbprint :0 -color -nkg 2 -o - | ps2pdf - - | zathura -
}

function tmuxzsh {
    tmux new-session zsh
}

function cdnoita {
    cd ~/.local/share/Steam/steamapps/compatdata/881100/pfx/drive_c/users/steamuser/AppData/LocalLow/Nolla_Games_Noita/
}

function start_recording_nosound {
    if [ -z "$1" ]; then
        echo "No output file specified"
    else   
        ffmpeg -f x11grab -framerate 30 -i $DISPLAY -c:v libx264 -preset ultrafast -crf 18 "$1"
    fi 
}

function wmtest-with-xephyr {
    xinit ~/.xinitrc -- /usr/bin/Xephyr :1 -ac -screen 1024x768 -br -noreset
}

alias ru-setxkbmap="setxkbmap -layout \"us,ru\""

function get-real-x {
    setxkbmap -layout "us,ru" -option "grp:win_space_toggle"
    xset r rate 250 30
    xset b off
    xset s off -dpms
} 

function nixBUILDTHIS {
    nix-build -E 'with import <nixpkgs> {}; callPackage ./default.nix {}'
}

function nixPREFETCHTHIS {
    nix-prefetch-url --unpack "file://$(realpath $1)" 2>&1 1>/dev/null | tail -n 1
}

function win_r_compile {
    dmenuc.py ~/.config/suckless_desktop/win_r.dmenu ~/.config/suckless_desktop/win_r_out/ init_shell.sh
}

alias cdslcfg="cd ~/.config/suckless_desktop"
alias cdnocfg="cd /etc/nixos"
alias cdhmcfg="cd ~/.config/home-manager"

function gitaddacommit {
    git status
    git add -A
    git commit -m "$1"
    git push
}

if [ -z "$SSH_AUTH_SOCK" ]; then
    eval "$(ssh-agent -s)"
fi
