#!/bin/bash

# Variables
blog_dir=~/repos/blog-cms

check() {
	if [ "$EUID" -ne 0 ]
	then
		echo "Please run as root"
		exit
	fi
}

confirm() {
	confirmvar=""
	while [[ "$confirmvar" == "" ]]
	do
		read -p "y/n/q: " confirmvar
		if [ "${confirmvar}" == "y" ]
		then
			echo true
		elif [ "${confirmvar}" == "n" ]
		then
			echo false
		fi
	done
}

newpost() {
	c=false
	while [[ ${c} == false ]]
	do
		read -p "Title: " title
		echo -e "Subject 1:\n\t0 - no subject\n\t1 - Programming\n\t2 - Pentesting\n\t3 - Personal"
		sub1=$(subjectselect)
		echo -e "Subject 1:\n\t0 - no subject\n\t1 - Programming\n\t2 - Pentesting\n\t3 - Personal"
		sub2=$(subjectselect)
		echo -e "Title: ${title}\nSubject 1: ${sub1}\nSubject 2: ${sub2}\n\nAre you sure?\n"
		c=$(confirm)
	done
	dirname=$(echo ${title} | sed 's/\ /_/g')
	mkdir -p ${blog_dir}/posts/${dirname}/static
	cd ${blog_dir}/posts/${dirname}
	echo ${title} >> title
	echo ${sub1} >> .sub1
	echo ${sub2} >> .sub2
	touch {intro.html,body.html}
  pwd
	ls -al
}

subjectselect() {
	read -p "0/1/2/3: " tmpvar
	if [ "$tmpvar" == "1" ]
	then
		echo "Programming"
	elif [ "$tmpvar" == "2" ]
	then
		echo "Pentesting"
	elif [ "$tmpvar" == "3" ]
	then
		echo "Personal"
	elif [ "$tmpvar" == "0" ]
	then
		echo ""
	fi
}

listandReturn() { printf "Listing contents of %s.\\n" "$1"
	ls -rc "$1" | awk -F '/' '{print $NF}' | nl
	read -erp "Pick an entry by number to $2, or press ctrl-c to cancel. " number
	chosen="$(ls -rc "$1" | nl | grep -w  "$number" | awk '{print $2}')"
basefile="$(basename "$chosen")" && base="${basefile%.*}" ;}

upload() {

	#check

	listandReturn ${blog_dir}/posts
	#cd ${blog_dir}/posts/${basefile}

	titlevar=$(cat ${blog_dir}/posts/${basefile}/title)
	subvar1=$(cat ${blog_dir}/posts/${basefile}/.sub1)
	subvar2=$(cat ${blog_dir}/posts/${basefile}/.sub2)

	echo "Add intro pic?"
	if [[ $(confirm) == true ]]
	then
		listandReturn ${blog_dir}/posts/${basefile}/static/
		picvar=${basefile}
	fi

	echo -n Mysql password:
	read -s sqlpassword

	scp ${blog_dir}/posts/${basefile}/{intro.html,body.html} root@tovijaeschke.xyz:/var/lib/mysql-files/
	scp ${blog_dir}/posts/${basefile}/static/* root@tovijaeschke.xyz:/usr/share/nginx/personal/static/

	if [[ "$picvar" == "" ]]
	then
		ssh root@tovijaeschke.xyz "chown mysql:mysql /var/lib/mysql-files/* &&
			chmod go+rw /var/lib/mysql-files/* &&
			mysql -u root -p${sqlpassword} -D PersonalWebsite -e \"INSERT INTO Posts (subject,subject2,title,intro,body) VALUES ('${subvar1}', '${subvar2}', '${titlevar}', LOAD_FILE('/var/lib/mysql-files/intro.html'), LOAD_FILE('/var/lib/mysql-files/body.html'));\""
	else
		ssh root@tovijaeschke.xyz "chown mysql:mysql /var/lib/mysql-files/* &&
			chmod go+rw /var/lib/mysql-files/* &&
			mysql -u root -p${sqlpassword} -D PersonalWebsite -e \"INSERT INTO Posts (subject,subject2,title,pic,intro,body) VALUES ('${subvar1}', '${subvar2}', '${titlevar}', '${picvar}', LOAD_FILE('/var/lib/mysql-files/intro.html'), LOAD_FILE('/var/lib/mysql-files/body.html'));\""
	fi
}

preview() {
	mkdir -p /tmp/preview
	cp -r ${blog_dir}/.preview/* /tmp/preview/

	listandReturn ${blog_dir}/posts

	title=$(cat ${blog_dir}/posts/${basefile}/title | tr -d '\n')
	intro=$(cat ${blog_dir}/posts/${basefile}/intro.html | tr -d '\n')
	body=$(cat ${blog_dir}/posts/${basefile}/body.html | tr -d '\n')

	cp -r ${blog_dir}/posts/${basefile}/static/ /tmp/preview

	sed -i .bak 's|TITLE_PREVIEW|'"${title}"'|g' /tmp/preview/post.html
	sed -i .bak 's|INTRO_PREVIEW|'"${intro}"'|g' /tmp/preview/post.html
	sed -i .bak 's|BODY_PREVIEW|'"${body}"'|g' /tmp/preview/post.html

  if [[ $(uname) == "Linux" ]]; then
	  firefox /tmp/preview/post.html
  else
    open -a firefox -g /tmp/preview/post.html
  fi
}

deletedraft() {
	echo "Which draft would you like to delete?"
	listandReturn ${blog_dir}/posts
	echo "Are you sure you want to delete \"$(cat ${blog_dir}/posts/${basefile}/title)?\""
	c=$(confirm)
	if [ ${c} != false ]
	then
		rm -rf ${blog_dir}/posts/${basefile}
	fi
}

deletepost() {
	echo -n Mysql password:
	read -s sqlpassword

	echo -e "\nWhich post would you like to delete?"
	ssh root@tovijaeschke.xyz "mysql -u root -p${sqlpassword} -D PersonalWebsite -B --disable-column-names -e \"SELECT id,title FROM Posts;\""
	read -p "\nWhich post would you like to delete?" post
	ssh root@tovijaeschke.xyz "mysql -u root -p${sqlpassword} -D PersonalWebsite -e \"DELETE FROM Posts WHERE id=${post};\" && \
		post=\$(mysql -B -u root -p${sqlpassword} -D PersonalWebsite --disable-column-names -e \"SELECT MAX(id) FROM Posts;\") && \
		mysql -u root -p${sqlpassword} -D PersonalWebsite -e \"ALTER TABLE Posts AUTO_INCREMENT=\$post;\""
}

helpmsg() {
	printf "\nBLOG UPLOAD SCRIPT\n\n\tn  - new draft\n\tp  - preview post\n\tu  - upload post\n\tdd - delete draft\n\tdp - delete post\n\th  - help message\n\n"
}

case "$1" in
	n*) newpost ;;
	p*) preview ;;
	u*) upload ;;
	dd*) deletedraft ;;
	dp*) deletepost ;;
	h*) helpmsg ;;
	*) helpmsg ;;
esac
