#!/bin/sh
# SPDX-License-Identifier: BSD-2-Clause
# SPDX-FileCopyrightText: © 2019 BetaRays <array@dotslashplay.it>
# SPDX-FileCopyrightText: © 2020 Antoine Le Gonidec <vv221@dotslashplay.it>
set -o errexit

###
# CrossCode
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260517.2

PLAYIT_COMPATIBILITY_LEVEL='2.34'

GAME_ID='crosscode'
GAME_NAME='CrossCode'

GAME_ID_DEMO="${GAME_ID}-demo"
GAME_NAME_DEMO="$GAME_NAME (demo)"

ARCHIVE_BASE_64BIT_0_NAME='crosscode-new-linux64.zip'
ARCHIVE_BASE_64BIT_0_MD5='2fece5dc78bf6bfdde7e6b3239da73b6'
ARCHIVE_BASE_64BIT_0_SIZE='1019175'
ARCHIVE_BASE_64BIT_0_VERSION='1.4.2-itch.2022.04.19'
ARCHIVE_BASE_64BIT_0_URL='https://radicalfishgames.itch.io/crosscode'

ARCHIVE_BASE_32BIT_0_NAME='crosscode-new-linux32.zip'
ARCHIVE_BASE_32BIT_0_MD5='630cfd22cd5e9e168f833dfa3fce9931'
ARCHIVE_BASE_32BIT_0_SIZE='1021105'
ARCHIVE_BASE_32BIT_0_VERSION='1.4.2-itch.2022.04.19'
ARCHIVE_BASE_32BIT_0_URL='https://radicalfishgames.itch.io/crosscode'

ARCHIVE_BASE_DEMO_64BIT_0_NAME='crosscode-demo-linux64.zip'
ARCHIVE_BASE_DEMO_64BIT_0_MD5='242e4cc9f48bd70a04f2d5d5e3258207'
ARCHIVE_BASE_DEMO_64BIT_0_SIZE='211392'
ARCHIVE_BASE_DEMO_64BIT_0_VERSION='1.0-itch1'
ARCHIVE_BASE_DEMO_64BIT_0_URL='https://radicalfishgames.itch.io/crosscode'

ARCHIVE_BASE_DEMO_32BIT_0_NAME='crosscode-demo-linux32.zip'
ARCHIVE_BASE_DEMO_32BIT_0_MD5='6b6c85bcd7ad315ee42a4c785e3e85a6'
ARCHIVE_BASE_DEMO_32BIT_0_SIZE='221220'
ARCHIVE_BASE_DEMO_32BIT_0_VERSION='1.0-itch1'
ARCHIVE_BASE_DEMO_32BIT_0_URL='https://radicalfishgames.itch.io/crosscode'

CONTENT_PATH_DEFAULT='assets'
CONTENT_PATH_DEFAULT_DEMO='.'
CONTENT_GAME_MAIN_FILES='
data
extension
game
impact
js
media
node-webkit.html
package.json'

APP_MAIN_EXE='node-webkit.html'
APP_MAIN_ICON='../favicon.png'

## Easier upgrades from pre-20251127.1 packages
PKG_MAIN_PROVIDES='
crosscode-data'
PKG_MAIN_PROVIDES_DEMO='
crosscode-demo-data'

# Load common functions.

PLAYIT_LIB_PATHS="
$PWD
${XDG_DATA_HOME:="${HOME}/.local/share"}/play.it
/usr/local/share/games/play.it
/usr/local/share/play.it
/usr/share/games/play.it
/usr/share/play.it"

if [ -z "$PLAYIT_LIB2" ]; then
	for playit_lib_path in $PLAYIT_LIB_PATHS; do
		if [ -e "${playit_lib_path}/libplayit2.sh" ]; then
			PLAYIT_LIB2="${playit_lib_path}/libplayit2.sh"
			break
		fi
	done
fi
if [ -z "$PLAYIT_LIB2" ]; then
	printf '\n\033[1;31mError:\033[0m\n'
	printf 'libplayit2.sh not found.\n'
	exit 1
fi
# shellcheck source=libplayit2.sh
. "$PLAYIT_LIB2"

# Run default initialisation actions.

initialization_default "$@"

# Extract game data.

archive_extraction_default

# Include game data.

case "$(current_archive)" in
	('ARCHIVE_BASE_DEMO_'*)
		# No icon is provided with the demo builds

		ARCHIVE_INNER_PATH="${PLAYIT_WORKDIR}/gamedata/CrossCode"
		# unzip succeeds but returns non-zero status because of offset
		ARCHIVE_INNER_EXTRACTOR='bsdtar'
		archive_extraction 'ARCHIVE_INNER'
		rm "$ARCHIVE_INNER_PATH"
	;;
	(*)
		content_inclusion_icons
	;;
esac

# Fix node-webkit.html to work with regular web browsers.

sed --regexp-extended --in-place \
	--expression="/    \/\/ make sure we don't let node-webkit show it's error page/,+8d" \
	"${PLAYIT_WORKDIR}/gamedata/$(content_path_default)/node-webkit.html"

content_inclusion_default

# Write launchers,

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
