#!/usr/bin/env sh

if [ $# -eq 0 ]; then
	cd "$HOME"/Documents/repos/cronyakatsuki.xyz || exit 1
	nvim
	exit 0
else
	cd "$HOME"/Documents/repos/cronyakatsuki.xyz || exit 1
fi

open_file() {
	cd "$HOME"/Documents/repos/cronyakatsuki.xyz || exit 1
	if [ -f "content/blog/${1}.md" ]; then
		nvim "content/blog/${1}.md"
	else
		hugo new "content/blog/${1}.md"
		nvim "content/blog/${1}.md"
	fi
}

publish() {
	cd "$HOME"/Documents/repos/cronyakatsuki.xyz || exit 1
	hugo
	rsync -rP --delete public/ server:/var/www/site
	git add .
	git commit -m "Site update"
	git push
}

remove_file() {
	cd "$HOME"/Documents/repos/cronyakatsuki.xyz || exit 1
	if [ -f "content/blog/${1}.md" ]; then
		rm "content/blog/${1}.md"
	else
		echo "File didn't exist"
	fi
}

if [ "$1" = "pub" ]; then
	publish
	exit 0
elif [ $# -eq 1 ]; then
	open_file "$1"
	exit 0
elif [ $# -ne 2 ]; then
	echo "You need to provide action and file name."
	exit 1
fi

case "$1" in
rm)
	remove_file "$2"
	exit 0
	;;
*)
	exit 1
	;;
esac