#!/usr/bin/env bash

set -e

if [ "$#" -ne 1 ]; then
    echo "Usage: psemek-create-project <project-name>"
    exit 0
fi

git init .
git submodule add git@git.lisyarus.dev:lisyarus/psemek.git
git submodule update --init --recursive

mkdir -p source include/${1}

cat > .gitignore << EOF
.gitignore
.qtcreator
build
package
EOF

cat > CMakeLists.txt << EOF
cmake_minimum_required(VERSION 3.16)
project(${1})

set(TARGET_NAME ${1})

set(CMAKE_CXX_STANDARD 20)
set(PSEMEK_EXAMPLES OFF)

set(PSEMEK_PACKAGE_OUTPUT_PATH package)
set(PSEMEK_GRAPHICS_API OPENGL)

add_subdirectory(psemek)

file(GLOB_RECURSE ${1^^}_SOURCES LIST_DIRECTORIES FALSE "\${CMAKE_CURRENT_SOURCE_DIR}/source/*")
file(GLOB_RECURSE ${1^^}_HEADERS LIST_DIRECTORIES FALSE "\${CMAKE_CURRENT_SOURCE_DIR}/include/*")

psemek_add_application(\${TARGET_NAME} \${${1^^}_SOURCES} \${${1^^}_HEADERS})
if(TARGET \${TARGET_NAME})
	target_include_directories(\${TARGET_NAME} PUBLIC "\${CMAKE_CURRENT_SOURCE_DIR}/include")

	if(PSEMEK_PLATFORM_WEB)
		# Required by itch.io
		set_target_properties(\${TARGET_NAME} PROPERTIES
			OUTPUT_NAME "index"
		)
	endif()
endif()
EOF

cat > source/application.cpp << EOF
#include <psemek/app/default_application_factory.hpp>

namespace ${1}
{

	using namespace psemek;

	struct application
		: app::application
	{
		application(options const &, context const &)
		{

		}

		bool running() const override
		{
			return running_;
		}

		void stop() override
		{
			running_ = false;
		}

		void update() override
		{}

		void present() override
		{}

	private:
		bool running_ = true;
	};

}

namespace psemek::app
{

	std::unique_ptr<application::factory> make_application_factory()
	{
		application::options options
		{
			.name = "${1^}",
		};

		return default_application_factory<${1}::application>(options);
	}

}
EOF
