#!/bin/bash
#  Ibnu Yahya <ibnu.yahya@toroo.org>

DESTDIR="$HOME/IGNSDK-APP/"
DEST=false
HELP=false
DISTCHK=`cat /etc/ignsdk-dist`
APPNAME=""
GIT=false
PACKAGING=true

if [ ! -d $DESTDIR ];then
	mkdir $DESTDIR
fi

TEMP=`getopt -o hp:gr:x: --long help,pkgname:,git,remote:,no-packaging,distribution: -n 'ignsdk-app-creator' -- "$@"`

eval set -- "$TEMP"

help ()
{

cat <<EOF
Usage: ignsdk-app-creator [OPTION]

Option :

	-p, --pkgname		Package name
	-g, --git		Initialize Git repository
	-r, --remote		Set Git remote URL
	--no-packaging		Skip packaging process
	-x, --distribution	Override target distribution (Debian only)
	-h, --help		Show this help

Example:

	Create a package
	$ $0 -p control-panel

	Create a package and initialize git repo
	$ $0 -p control-panel -g

EOF

exit 0
}

create_project ()
{
	/usr/share/ign-sdk/bin/ignsdk-$DIST-creator $1 $2 $3 $4 $5
}

init_git ()
{
	pushd $BUILDDEST > /dev/null 2>&1
	git init > /dev/null 2>&1
	if [ "$(git config --list | grep user.name)" == "" ]; then
		read -p "Set user.name for git  : " git_username
		read -p "Set user.email for git : " git_useremail
		git config user.name "$git_username"
		git config user.email "$git_useremail"
	fi
	git remote add origin $GIT_REMOTE
	popd > /dev/null 2>&1
}

commit_git ()
{
	echo ""
	pushd $BUILDDEST > /dev/null 2>&1
	git add *
	echo "Committing changes..."
	git commit -m "Initial project files" > /dev/null 2>&1
	popd > /dev/null 2>&1
}

if [ $? != 0 ] ; then
	help
fi

if [ $DISTCHK == "debian" -o $DISTCHK == "ign" -o $DISTCHK == "arch" -o $DISTCHK == "slack" ];then
	DIST=$DISTCHK
else
	echo "ERROR: Distribution is not supported!"
	exit 0
fi

while true; do
	case "$1" in
		-p | --pkgname )
			if [ ! $2 ]; then
				DEST=false
			else
				if [ ! -d "$DESTDIR$2.ign" ]; then
					DEST=true
					BUILDDEST="$DESTDIR$2.ign"
					PKGNAME="$2.ign"
					PROJECT="$2"
				else
					echo "Project exist!"
					help
				fi
	 		fi
			shift 2
			;;
		-g | --git )
			if [ "$(which git)" == ""  ]; then
				GIT=false
				echo "Git is not installed."
				exit 1
			else
				GIT=true
			fi
			shift
			;;
		-r | --remote )
			GIT_REMOTE=$2
			shift
			;;
		--no-packaging )
			PACKAGING=false
			shift
			;;
		-x | --distribution )
			if [ ! "$2" == "ign" -a $DISTCHK == "debian" ]; then
				echo "Invalid/unsupported distribution: $2"
				exit 1
			else
				DIST=$2
			fi
			shift
			;;
		-h | --help )
			HELP=true
			shift
			;;
		-- )
			shift
			break
			;;
		* )
		break
		;;
	esac
done

if [ $HELP == true ]
then
	help
fi

if [ $DEST == true ]
then
	read -p "Application name: " APPNAME
	cat <<EOF
Application category :
1. AudioVideo (multimedia)
2. Audio (audio only)
3. Video (video only)
4. Development
5. Education
6. Game
7. Graphics
8. Network
9. Office
10. Science
11. Settings
12. System
13. Utility [default]
EOF
	read -p "Choose one: " CTGR

	echo ""

	case "$CTGR" in
	    "1" ) MENU="AudioVideo"
		shift 2 ;;
	    "2" ) MENU="Audio;AudioVideo"
		shift 2 ;;
	    "3" ) MENU="Video;AudioVideo"
		shift 2 ;;
	    "4" ) MENU="Development"
		shift 2 ;;
	    "5" ) MENU="Education"
		shift 2 ;;
	    "6" ) MENU="Game"
		shift 2 ;;
	    "7" ) MENU="Graphics"
		shift 2 ;;
	    "8" ) MENU="Network"
		shift 2 ;;
	    "9" ) MENU="Office"
		shift 2 ;;
	    "10" ) MENU="Science"
		shift 2 ;;
	    "11" ) MENU="Settings"
		shift 2 ;;
	    "12" ) MENU="System"
		shift 2 ;;
	    "13" ) MENU="Utility"
		shift 2 ;;
	    -- ) MENU="Utility"
		shift; break ;;
	    * ) MENU="Utility"
		shift;;
	esac

	if [ "$APPNAME" == "" ]
	then
		echo "ERROR: Application name can't be empty!"
		help
	else
		mkdir $BUILDDEST
		if [ "$GIT" == "true"  ]; then
			echo "Initializing git repository..."
			init_git
		fi
		tar xf /usr/share/ign-sdk/template/main.tpl -C $BUILDDEST
		mv $BUILDDEST/menu/ignsdk.desktop $BUILDDEST/menu/ignsdk-$PROJECT.desktop
		sed -i -e "s/\[package\]/$PKGNAME/" $BUILDDEST/menu/ignsdk-$PROJECT.desktop
		sed -i -e "s/\[appname\]/$APPNAME/g" $BUILDDEST/menu/ignsdk-$PROJECT.desktop
		sed -i -e "s/\[category\]/$MENU/g" $BUILDDEST/menu/ignsdk-$PROJECT.desktop
		sed -i -e "s/\[appname\]/$APPNAME/g" $BUILDDEST/ignsdk.json
		if [ "$PACKAGING" == "true" ]; then
			create_project $PKGNAME $BUILDDEST "$APPNAME" "$MENU" $PROJECT
		fi
		if [ "$GIT" == "true" ]; then
			commit_git
		fi
	fi

else
	echo "ERROR: Please name your project."
	help
fi
