#!/usr/bin/env bash
DEST=${1:-/etc/skel}
ALIAS_DIR=".zsh_aliases.d"
mkdir -p "${DEST}/${ALIAS_DIR}"

ALIAS_FILE=${DEST}/.zsh_aliases
cat >>"${ALIAS_FILE}" <<-EOF

	# Enable \${ALIAS_DIR} to keep aliases separate from env variables
	if [ -d ~/${ALIAS_DIR} ]; then
	  for f (~/${ALIAS_DIR}/**/*(N.))  . \$f
	fi
EOF

# tell bash to check the next word after the alias (i.e sudo) by adding a space to the end of the alias value.
echo "# tell bash to check the next word after the alias (i.e sudo) by adding a space to the end of the alias value." >"${DEST}/${ALIAS_DIR}/002_sudo.zsh"
echo 'alias sudo="sudo "' >>"${DEST}/${ALIAS_DIR}"/002_sudo.zsh

ALIAS_FILE="${DEST}/${ALIAS_DIR}/003_py-aliases.zsh"
cat >"${ALIAS_FILE}" <<-'EOF'
	_py_version() {
	  PY_VERSIONS=(2 3)

	  LINE=(${(s: :)history[$HISTCMD]})
	  COMMAND=${LINE[1]}
	  ARGS=${(j: :)LINE[2,-1]}

	  while (( 1 )) {
	    read -s -k1 VERSION"?$COMMAND [${PY_VERSIONS[1]}]: "
	    if [[ $VERSION == $'\n' ]]; then
	      VERSION=${PY_VERSIONS[1]}
/var/tmp/automate/020_zsh_aliases.d.sh	      break
	    elif (( ${PY_VERSIONS[(I)$VERSION]} )); then
	      echo $VERSION
	      break
	    fi
	    echo "$VERSION is not supported (${(j:, :)PY_VERSIONS})"
	  }

	  if [[ -x $(which $COMMAND$VERSION) ]]; then
	    COMMAND=$COMMAND$VERSION
	  else
	    COMMAND="python$VERSION $(which -a $COMMAND | grep -v aliased | head -n 1)"
	  fi
	  eval $COMMAND $ARGS
	}

	# versioned python commands
	# alias python="_py_version"
	# alias pip="_py_version"
	# alias pydoc="_py_version"
	# alias idle="_py_version"
	# alias ipython="_py_version"
	# alias jupyter="_py_version"
EOF

ALIAS_FILE="${DEST}/${ALIAS_DIR}/003_transfer.zsh"
cat >"${ALIAS_FILE}" <<-'EOF'
	#
	# Defines transfer alias and provides easy command line file and folder sharing.
	#
	# Authors:
	#   Remco Verhoef <remco@dutchcoders.io>
	#

	curl --version 2>&1 > /dev/null
	if [ $? -ne 0 ]; then
	  echo "Could not find curl."
	  return 1
	fi

	transfer() { 
	    # check arguments
	    if [ $# -eq 0 ]; 
	    then
		echo -e "No arguments specified.\n\nUsage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"
		return 1
	    fi

	    # get temporarily filename, output is written to this file show progress can be showed
	    tmpfile=$( mktemp -t transferXXX )
	    
	    # upload stdin or file
	    file=$1

	    if tty -s; TH

	    then 
		basefile=$(basename "$file" | sed -e 's/[^a-zA-Z0-9._-]/-/g') 

		if [ ! -e $file ];
		then
		    echo "File $file doesn't exists."
		    return 1
		fi

		if [ -d $file ];
		then
		    # zip directory and transfer
		    zipfile=$( mktemp -t transferXXX.zip )
		    cd $(dirname $file) && zip -r -q - $(basename $file) >> $zipfile
		    curl --progress-bar --upload-file "$zipfile" "https://transfer.sh/$basefile.zip" >> $tmpfile
		    rm -f $zipfile
		else
		    # transfer file
		    curl --progress-bar --upload-file "$file" "https://transfer.sh/$basefile" >> $tmpfile
		fi
	    else 
		# transfer pipe
		curl --progress-bar --upload-file "-" "https://transfer.sh/$file" >> $tmpfile
	    fi
	   
	    # cat output link
	    cat $tmpfile
	    
	    # log file link
	    APPLICATION="${0##*/}"
	    RIGHTNOW="$(date)"
	    EXPIRES="$(date -d "+14 days")"
	    echo -e "$(cat "$tmpfile") - uploaded $RIGHTNOW - expires $EXPIRES\n" >> ~/$APPLICATION.log
	    echo "\n\nSee ~/$APPLICATION.log for all transfers.\n"

	    # cleanup
	    rm -f $tmpfile
	}
EOF

# Check that these don't clash with 020_grc.sh
ALIAS_FILE="${DEST}/${ALIAS_DIR}/003_local.zsh"
cat >"${ALIAS_FILE}" <<-'EOF'
	alias baobab='dbus-run-session baobab'
	alias bc='bc -lq'
	alias leech="wget -e robots=off -c -r --level=0 -nc -np --random-wait"
	alias less="less -R"
	#alias mpv='mpv --ao=alsa --force-window -af "crossfeed=strength=0.2:range=0.5:slope=0.5:level_in=0.9:level_out=1:block_size=1024"'
	#alias mpv='mpv --ao=pipewire --pipewire-buffer=47 --force-window'
	alias odc='~/bin/odysee-dl_low.sh hls-215'
	alias pastebinit='pastebinit -b paste.debian.net'
	alias plocate='plocate --existing --basename --ignore-case'
	alias shred='ionice -c3 /usr/bin/shred -fuzv'
  alias tree='tree -CAFa -I "CVS|*.package|.svn|.git" --dirsfirst'
	alias tsleech='torsocks leech'
	alias tsmpv='torsocks mpv'
	alias tspastebinit='torsocks pastebinit -b paste.debian.net'
	alias tswget='torsocks wget'
	alias tsyoutube-dl='torsocks yt-dlp'
	alias tsy='tsy-dlp'
	alias tsy-dlp='torsocks yt-dlp'
	alias wget="wget --content-disposition -c -U 'User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0'"
	alias wipe='ionice -c3 /usr/bin/wipe -l1 -v -r'
	alias wtmpv='webtorrent --mpv --blocklist https://dbl.oisd.nl/ --upload-limit 20'
	alias youtube-dl='yt-dlp --downloader=aria2c'
	alias ytmpv='torsocks mpv --ytdl-format=18'
	alias yt-dlp='yt-dlp --downloader=aria2c'
	alias ytfzf='ytfzf --ytdl-pref=18'
EOF

ALIAS_FILE="${DEST}/${ALIAS_DIR}/001_editor.zsh"
cat >"${ALIAS_FILE}" <<EOF
if [ -x /usr/bin/nvim ]; then
  alias vi="/usr/bin/nvim"
  alias vim="/usr/bin/nvim"
  export EDITOR="/usr/bin/nvim"
fi
EOF

ALIAS_FILE="${DEST}/${ALIAS_DIR}/004_iotop.zsh"
cat >"${ALIAS_FILE}" <<-'EOF'
	alias iotop='bash -c "sudo sysctl kernel.task_delayacct=1 && sudo iotop ; sudo sysctl kernel.task_delayacct=0"'
EOF

ALIAS_FILE="${DEST}/${ALIAS_DIR}/004_docker_path.zsh"
cat <<-EOF | tee "${ALIAS_FILE}"
	export PATH=$PATH:/usr/libexec/docker/cli-plugins
EOF

ALIAS_FILE="${DEST}/${ALIAS_DIR}/004_cargo-path.zsh"
cat <<-EOF | tee "${ALIAS_FILE}"
	export PATH=$PATH:${HOME}/.cargo/bin
EOF
