#! /bin/bash
# Copyright (C) 2018-2019 Merspieler, merspieler _at_ airmail.cc
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

first=1
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
	-h|--help)
		echo "usage: create-project <project> [OPTIONS]"
		echo "Creates a new project with all necesary files."
		echo ""
		echo "OPTIONS"
		echo "  -h, --help      Shows this help and exit"
		exit 0
	;;

	*)
		if [ $first == "1" ]; then
			project="$1"
			shift # past project
			first=0
		else
			echo "Unknown option $key"
			exit 1
		fi
	;;
esac
done

if [ -z "$project" ]; then
	echo "Option -p <project> is mandatory"
	exit 1
fi

if [ -f "projects/$project/params.ini" ]; then
	echo "The project was already created"
	exit 1
fi

source general-settings > /dev/null
if [ $? == 1 ]; then
        echo "Couldn't find the general settings. Please run ./install first."
        exit 2
fi

if [ ! -f ".databases" ]; then
	echo "There are no databases created by easy-osm2city. Please create one by running ./create-db"
	exit 2
fi

echo "Which database do you want to use?"
read db_name
cat .databases | grep -e "$db_name" > /dev/null
if [ $? == 1 ]; then
	echo "Database doesn't exsist. Please choose a different database or create one by running"
	echo "./create-db -d $db_name"
	exit 2
fi

mkdir -p "projects/$project/scenery"
echo "PREFIX = '$project'" > "projects/$project/params.ini"
echo "PATH_TO_SCENERY = '$fg_scenery'" >> "projects/$project/params.ini"
echo "PATH_TO_OUTPUT = '$(pwd)/projects/$project/scenery'" >> "projects/$project/params.ini"
echo "PATH_TO_OSM2CITY_DATA = '$(pwd)/osm2city-data'" >> "projects/$project/params.ini"
echo "" >> "projects/$project/params.ini"
echo "NO_ELEV = False" >> "projects/$project/params.ini"
echo "FG_ELEV = '$fg_elev'" >> "projects/$project/params.ini"
echo "" >> "projects/$project/params.ini"
echo "DB_HOST = '$db_host'" >> "projects/$project/params.ini"
echo "DB_PORT = '$db_port'" >> "projects/$project/params.ini"
echo "DB_NAME = '$db_name'" >> "projects/$project/params.ini"
echo "DB_USER = '$db_user'" >> "projects/$project/params.ini"
echo "DB_USER_PASSWORD = '$db_passwd'" >> "projects/$project/params.ini"
echo "" >> "projects/$project/params.ini"
echo "POINTS_ON_LINE_DISTANCE_MAX = 20" >> "projects/$project/params.ini"
echo "HIGHWAY_TYPE_MIN = 3" >> "projects/$project/params.ini"

pass=0
while [ $pass == 0 ]
do
	echo "What is the WESTside boundary of the area?"
	read west
	case $west in
		''|*[!-0-9]*)
			echo "Value is no number."
		;;
		*)
			pass=1
		;;
	esac
done

pass=0
while [ $pass == 0 ]
do
	echo "What is the SOUTHside boundary of the area?"
	read south
	case $south in
		''|*[!-0-9]*)
			echo "Value is no number."
		;;
		*)
			pass=1
		;;
	esac
done

pass=0
while [ $pass == 0 ]
do
	echo "What is the EASTside boundary of the area?"
	read east
	case $east in
		''|*[!-0-9]*)
			echo "Value is no number."
		;;
		*)
			pass=1
		;;
	esac
done

pass=0
while [ $pass == 0 ]
do
	echo "What is the NORTHside boundary of the area?"
	read north
	case $north in
		''|*[!-0-9]*)
			echo "Value is no number."
		;;
		*)
			pass=1
		;;
	esac
done

if [ "$west" -lt "0" ]; then
	echo "bounds=*${west}_${south}_${east}_${north}" > projects/$project/settings
else
	echo "bounds=${west}_${south}_${east}_${north}" > projects/$project/settings
fi