#!/bin/bash

#Author:  Matthew Carter
#File:    ahungry_scripter.sh
#Date:    20121013
#Purpose: Assist with installation of vimprobable2 user scripts

VER="1.0.2"
SCRIPTFILE='./scripts.js'

#We only want to work in the directory itself so lets cd there
cd $(dirname $0)

#Make the scripts.constant directory if it is missing
[ ! -d ./scripts.constant ] && mkdir ./scripts.constant

#Pull in fresh copy of this script from server
update() {
	self=$(basename $0)

	[ -e ./$self.1 ] && rm ./$self.1

	echo "Refreshing this script via latest version at http://ahungry.com"

	wget 'http://ahungry.com/vp2_scripter/ahungry_scripter.sh'
	if [ ! -e ./$self.1 ]; then
		echo "Failed to retrieve file from server, keeping current version of script" 
		exit 1
	fi

	echo "Backing up old version to ./ahungry_scripter.sh.bak"
	cp ./$self ./$self.bak

	mv ./$self.1 ./$self
	chmod +x ./$self

	[ -e ./$self ] && echo "Updated ./$self script file to $($0 -v) successfully!"
	echo
}

puller() {
	[[ -e ./scripts.available.tar.gz ]] && rm ./scripts.available.tar.gz
	wget 'http://ahungry.com/vp2_scripter/scripts.available.tar.gz'
	[[ ! -e ./scripts.available.tar.gz ]] && echo "Problem pulling in new scripts, using old scripts.available" && return

	echo "Backing up old ./scripts.available to ./scripts.available.bak"
	[[ -d ./scripts.available.bak ]] && rm -fr ./scripts.available.bak
	[[ -d ./scripts.available ]] && mv ./scripts.available ./scripts.available.bak
	echo "Installing new files..."
	tar xzvf ./scripts.available.tar.gz && rm ./scripts.available.tar.gz
	echo
}

pusher() {
	if [ ! -e "$1" ]; then
	  	echo "File missing! Cancelling push." 
		exit 1
	fi

	echo "Pushing $1 to http://ahungry.com..."
	MSG="$(curl -s -F data=@$1 'http://ahungry.com/vp2_scripter/upload.php')"

	[ -z "$MSG" ] && echo "Failure pushing file" && exit 1

	echo "$MSG"
	echo
}

wizard() {
	if [ ! -d ./scripts.available ]; then
		echo "You need to run ./ahungry_scripter.sh pull first!"
		exit 1
	fi

	echo "Clearing old symlinks in scripts.enabled"
	[[ -d ./scripts.enabled ]] && rm -fr ./scripts.enabled 
	mkdir ./scripts.enabled

	echo "Including scripts.constant files..."
	for fn in $(ls ./scripts.constant); do
		ln -s ../scripts.constant/$fn ./scripts.enabled/$fn
		echo "Including scripts.constant/$fn" 
	done

	for fn in $(ls ./scripts.available); do
		bname=$fn
		fn="./scripts.available/$fn"

		[[ -e ./scripts.enabled/$bname ]] && continue

		if [[ ! "$yn" =~ a.* ]]; then
			echo
			grep '@ahungry' $fn
		  	echo -en "Include $bname (yes/no/all)? "
		fi

		while : ; do
			[[ ! "$yn" =~ a.* ]] && read yn
			case $yn in
				y*)
					ln -s .$fn ./scripts.enabled/$bname
					break
					;;
				a*)
					echo "Including $bname"
					ln -s .$fn ./scripts.enabled/$bname
					break
					;;
				n*)
					break
					;;
				*)
					echo -n "Please only input (yes/no/all)"
					;;
			esac
		done;
	done;
	echo
}

applier() {
	echo "Backing up old $SCRIPTFILE to $SCRIPTFILE.bak"
	[ -e $SCRIPTFILE ] && cp $SCRIPTFILE $SCRIPTFILE.bak

	echo -e "Generating new $SCRIPTFILE\nMake sure to restart vimprobable2 for changes to take effect!"
	echo "//Generated on " $(date) " by ahungry_scripter (http://ahungry.com)" > $SCRIPTFILE
	echo "window.onload=loader();" >> $SCRIPTFILE
	echo "function loader() {" >> $SCRIPTFILE

	for fn in $(ls ./scripts.enabled); do
		echo -e "\tahungry_${fn:0:-3}();" >> $SCRIPTFILE
	done

	echo "}" >> $SCRIPTFILE

	for fn in $(ls ./scripts.enabled); do
		echo -e "\n//************BEGIN SCRIPT ${fn:0:-3}**************" >> $SCRIPTFILE
		echo -e "function ahungry_${fn:0:-3}() {" >> $SCRIPTFILE
		cat ./scripts.enabled/$fn >> $SCRIPTFILE
		echo "}" >> $SCRIPTFILE
		echo -e "//************END SCRIPT ${fn:0:-3}**************" >> $SCRIPTFILE
	done
	echo
}

show_help() {
	echo -e "ahungry_scripter.sh from http://ahungry.com/"
	echo -e "Version: $VER"
	echo
	echo -e "Usage: ./ahungry_scripter.sh [-][ah] command\n"

	echo "Options:"
	printf "%10s\tAuto-generate scripts.js with all\n" "-a"
	echo -e "\t\t  scripts available (refreshes scripts.available"
	echo -e "\t\t  from server first, includes scripts.constant) - also performs script update."
	printf "%10s\tDisplay this help\n" "-h"

	echo -e "\nCommands"
	printf "%10s\tDisplay this help\n" "help"
	printf "%10s\tDisplay standard help and pseudo-man page\n" "about"
	printf "%10s\tPull in new scripts.available from http://ahungry.com\n" "pull"
	printf "%10s\tApply current scripts.enabled to scripts.js\n" "apply"
	printf "%10s\tPick and choose which scripts to add to scripts.enabled and scripts.js\n" "wizard"
	printf "%10s\tUpdate this script to the latest version (expect more features soon!)\n" "update"
	printf "%10s\tPush your own file to the central server\n" "push"
	echo -e "\t\t\tPLEASE NOTE IF YOU WRAP YOUR JAVASCRIPT IN A FUNCTION \n\t\tWRAPPER IT WILL NOT WORK DUE TO THIS PROGRAMS OWN \n\t\tWRAPPING - JUST WRITE THE JAVASCRIPT IGNORING SCOPE AND THIS WILL DO THE REST"
}

show_verbose_help() {
	echo -e "\nSynopsis:"
	echo -e "\tThis utility relies on scripts.available, scripts.enabled,"
	echo -e "\tand scripts.constant to work.  It will handle directory creation."
	echo -e "\tThe utility will combine the javascript files from" 
	echo -e "\tscripts.enabled and place them in scripts.js for use with vimprobable2."

	echo -e "\nscripts.available:"
	echo -e "\tThis directory is refreshed from http://ahungry.com," 
	echo -e "\tyou should NOT put your own scripts in here."

	echo -e "\nscripts.enabled:"
	echo -e "\tThis directory is emptied when running wizard mode (or -a)." 
	echo -e "\tIt should contain symlinks to files in scripts.available or "
	echo -e "\tscripts.constant.  If you would like to customize the symlinks" 
	echo -e "\tyourself make sure to only run this utility's 'apply' mode."

	echo -e "\nscripts.constant:"
	echo -e "\tThis is where you may store custom files that will not be"
	echo -e "\ttouched by the script (all files here will be included" 
	echo -e "\tduring 'apply' mode)."
	echo
}

while getopts "ahv" opt; do
	OPT=1
	case $opt in
		a)
			update
			puller
			wizard
			applier
			;;
		h)
			show_help
			;;
		v)
			echo "ahungry_scripter version $VER"
			;;
		*)
			echo "Unknown option, skipping"
			;;
	esac
done

if [ -z "$OPT" ]; then
	case $1 in
		pull)
			puller
			;;
		push)
			pusher $2
			;;
		apply)
			applier
			;;
		wizard)
			wizard
			applier
			;;
		update)
			update
			;;
		about)
			show_help
			show_verbose_help
			;;
		*)
			show_help
			;;
	esac
fi
