#!/bin/sh

# Create a new directory and enter it
mkd() {
	mkdir -p "$@" && cd "$_"
}

# Change working directory to the top-most Finder window location
cdf() { # short for `cdfinder`
	cd "$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')"
}

# Determine size of a file or total size of a directory
fs() {
	if du -b /dev/null >/dev/null 2>&1; then
		local arg=-sbh
	else
		local arg=-sh
	fi
	if [[ -n "$@" ]]; then
		du $arg -- "$@"
	else
		du $arg .[^.]* ./*
	fi
}

# Create a data URL from a file
dataurl() {
	local mimeType=$(file -b --mime-type "$1")
	if [[ $mimeType == text/* ]]; then
		mimeType="${mimeType};charset=utf-8"
	fi
	echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')"
}

# Start an HTTP server from a directory, optionally specifying the port
server() {
	local port="${1:-8000}"
	sleep 1 && open "http://localhost:${port}/" &
	# Set the default Content-Type to `text/plain` instead of `application/octet-stream`
	# And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
	python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
}

# `v` with no arguments opens the current directory in Vim, otherwise opens the
# given location
v() {
	if [ $# -eq 0 ]; then
		vim .
	else
		vim "$@"
	fi
}

# `o` with no arguments opens the current directory, otherwise opens the given
# location
o() {
	if [ $# -eq 0 ]; then
		open .
	else
		open "$@"
	fi
}

# `tre` is a shorthand for `tree`, embracing the power of eza (via `eza -T`),
# while showing hidden files and ignoring some directories
#
# The depth of recursion can be set via a parameter, e.g. `tre 2`, `tre 4`, etc.
tre() {
	eza -T -a -L=${1:-1} -I='node_modules|bower_components|.git'
}

# Find and fetch weather for any given location, defaulting to Munich, Germany
wttr() {
	curl -4 wttr.in/${1:-munich}
}

# A quick approximation of self-written LOC in a directory across languages,
# ignoring things like node_modules, .nuxt, package-lock.json, etc. by default
loc() {
	tokei -e=package-lock.json --sort=lines | bat --style=plain # tokei ignores node_modules and hidden dirs by default
}

# Extract archives - use: extract <file>
# Based on https://github.com/paulirish/dotfiles/blob/master/.functions#L101
extract() {
	if [ -f "$1" ]; then
		local filename=$(basename "$1")
		local foldername="${filename%%.*}"
		local fullpath=$(perl -e 'use Cwd "abs_path";print abs_path(shift)' "$1")
		local didfolderexist=false
		if [ -d "$foldername" ]; then
			didfolderexist=true
			read -p "$foldername already exists, do you want to overwrite it? (y/n) " -n 1
			echo
			if [[ $REPLY =~ ^[Nn]$ ]]; then
				return
			fi
		fi
		mkdir -p "$foldername" && cd "$foldername"
		case $1 in
		*.tar.bz2) tar xjf "$fullpath" ;;
		*.tar.gz) tar xzf "$fullpath" ;;
		*.tar.xz) tar Jxvf "$fullpath" ;;
		*.tar.Z) tar xzf "$fullpath" ;;
		*.tar) tar xf "$fullpath" ;;
		*.taz) tar xzf "$fullpath" ;;
		*.tb2) tar xjf "$fullpath" ;;
		*.tbz) tar xjf "$fullpath" ;;
		*.tbz2) tar xjf "$fullpath" ;;
		*.tgz) tar xzf "$fullpath" ;;
		*.txz) tar Jxvf "$fullpath" ;;
		*.zip) unzip "$fullpath" ;;
		*) echo "'$1' cannot be extracted via extract()" && cd .. && ! $didfolderexist && rm -r "$foldername" ;;
		esac
	else
		echo "'$1' is not a valid file"
	fi
}

# Delete all screenshots in ~/Desktop (safely, e.g. using `trash` instead of `rm`)
deletescreenshots() {
	find ~/Desktop -type f -name "Screen Shot*" -exec trash {} \;
}

# Clear clipboard contents
clearclipboard() {
	pbcopy </dev/null
}
