#! /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.

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

case $key in
	--skip-install)
		skip_install=1
		shift # past argument
	;;

	--skip-git)
		skip_git=1
		shift # past argument
	;;

	--skip-sql)
		skip_sql=1
		shift # past argument
	;;

	-h|--help)
		echo "usage: install [OPTIONS]..."
		echo "Installs all needed packages and clones the osm2city and osm2city-data repository"
		echo ""
		echo "OPTIONS:"
		echo "  -h, --help		Shows this help and exits"
		echo "      --skip-install	Skips install of packages and continues with setup"
		echo "      --skip-git		Skips git clones of the osm2city and osm2city-data repository"
		echo "      --skip-sql		Skips setup of the sql user"
		exit
	;;

	*)
		echo "Unknow option $key"
		exit 1
	;;
esac
done

if [ "$skip_install" != 1 ]; then
	which apt-get > /dev/null
	if [ $? == 1 ]; then
		echo "Currently apt is the only supported package manager but it seems that apt isn't installed."
		echo "Please install the following packages manually"
		echo "python3-venv"
		echo "python-pip"
		echo "postgresql"
		echo "postgresql-postgis"
		echo "postgresql-client"
		echo "postgresql-contrib"
		echo "pgadmin3"
		echo "postgis"
		echo "python3-psycopg2"
		echo "osmosis"
		echo "git"
		echo "Then rerun this script with skip-install argument"
		echo "./install --skip-install"
		exit 3
	fi
	sudo apt-get install -y git osmosis postgis pgadmin3 postgresql-contrib postgresql-client postgresql-postgis postgresql python-pip python3-venv python3-dev
	if [ $? == 100 ]; then
		echo "apt-get had problems installing some packages"
		echo "Please make sure that all of the following packages are installed"
		echo "python3-dev"
		echo "python3-venv"
		echo "python-pip"
		echo "postgresql"
		echo "postgresql-postgis"
		echo "postgresql-client"
		echo "postgresql-contrib"
		echo "pgadmin3"
		echo "postgis"
		echo "python3-psycopg2"
		echo "osmosis"
		echo "git"
		echo "Then rerun this script with skip-install argument"
		echo "./install --skip-install"
		exit 3
	fi
fi

if [ "$skip_git" != 1 ]; then
	which git > /dev/null
	if [ $? == 1 ]; then
		echo "Unable to find git. Please manually download the following two repositories:"
		echo "https://gitlab.com/osm2city/osm2city"
		echo "https://gitlab.com/osm2city/osm2city-data"
		echo "Unzip them in this directory and make sure, the are named osm2city and osm2city-data"
		exit 3
	fi
	git clone https://gitlab.com/osm2city/osm2city.git
	git clone https://gitlab.com/osm2city/osm2city-data.git
fi

if [ "$skip_sql" != 1 ]; then
	echo "Do you want to create a database user[y/n]? "
	read ret
	if [ "$ret" == "y" ]; then
		echo "Please enter the username: "
		read db_user
		echo "Please enter a password: "
		read db_passwd
		sudo -u postgres psql -c "CREATE USER $db_user WITH PASSWORD '$db_passwd';"
		echo "db_user=$db_user" >> general-settings
		echo "db_passwd=$db_passwd" >> general-settings
	fi
fi

if [ ! -f general-settings ]; then
	echo "# This file must be used with "source general-settings" *from bash*" > general-settings
	echo "# you cannot run it directly" >> general-settings
	echo "" >> general-settings
fi


# check if the threads value is already in the general-settings
cat general-settings|grep ^threads > /dev/null
if [ $? == 1 ]; then
	echo "What is the default number of threads you want to use for scenery generation?"
	read threads
	echo "threads=$threads" >> general-settings
fi


# check if the FG_ROOT value is already in the general-settings
cat general-settings|grep ^FG_ROOT > /dev/null
if [ $? == 1 ]; then
	echo "Please give the full path to the fgdata directory"
	read fg_root
	echo "FG_ROOT=$fg_root" >> general-settings
	echo "export FG_ROOT" >> general-settings
fi

# check if the fg_elev value is already in the general-settings
cat general-settings|grep ^fg_elev > /dev/null
if [ $? == 1 ]; then
	echo "Please give the full path to the fgelev executable"
	read fg_elev
	echo "fg_elev=$fg_elev" >> general-settings
fi

# check if the fg_scenery value is already in the general-settings
cat general-settings|grep ^fg_scenery > /dev/null
if [ $? == 1 ]; then
	echo "Please give the full path to the flightgear scenery"
	read fg_scenery
	echo "fg_scenery=$fg_scenery" >> general-settings
fi

# check if the PYTHONPATH value is already in the general-settings
cat general-settings|grep ^PYTHONPATH > /dev/null
if [ $? == 1 ]; then
	echo "PYTHONPATH=$(pwd)/osm2city" >> general-settings
	echo "export PYTHONPATH" >> general-settings
fi

# check if the db_host value is already in the general-settings
cat general-settings|grep ^db_host > /dev/null
if [ $? == 1 ]; then
	echo "db_host=localhost" >> general-settings
fi

# check if the db_port value is already in the general-settings
cat general-settings|grep ^db_port > /dev/null
if [ $? == 1 ]; then
	echo "db_port=5432" >> general-settings
fi

# Create directory for .pbf files
mkdir -p pbf
mkdir -p projects

./create-venv

echo "User scripts are no longer shipped with easy-osm2city"
echo "You can clone them with:"
echo "git clone https://gitlab.com/merspieler/osm2city-scripts.git scripts"