#!/bin/sh
# SPDX-License-Identifier: ACSL
# SPDX-FileCopyrightText: © 2019 BetaRays <array@dotslashplay.it>
# SPDX-FileCopyrightText: © 2020 Antoine Le Gonidec <vv221@dotslashplay.it>
# SPDX-FileCopyrightText: © 2021 HS-157
# SPDX-FileCopyrightText: © 2021 Hoël Bézier <hoelbezier@riseup.net>
set -o errexit

###
# Factorio
# send your bug reports to vv221@dotslashplay.it
###

script_version=20251223.1

PLAYIT_COMPATIBILITY_LEVEL='2.34'

GAME_ID='factorio'
GAME_NAME='Factorio'

GAME_NAME_SPACEAGE="${GAME_NAME}: Space Age"

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

## Factorio (full game)

ARCHIVE_BASE_1_NAME='factorio_linux_2.0.72.tar.xz'
ARCHIVE_BASE_1_MD5='76fb4857cc223569d4397026e3e7996e'
ARCHIVE_BASE_1_SIZE='1979577'
ARCHIVE_BASE_1_VERSION='2.0.72-1'
ARCHIVE_BASE_1_URL='https://www.factorio.com/'

ARCHIVE_BASE_0_NAME='factorio_linux_2.0.7.tar.xz'
ARCHIVE_BASE_0_MD5='016efe181a57ebb649260b68b0696b3a'
ARCHIVE_BASE_0_SIZE='1973608'
ARCHIVE_BASE_0_VERSION='2.0.7-1'

## Factorio including Space Age expansion

ARCHIVE_BASE_SPACEAGE_0_NAME='factorio-space-age_linux_2.0.7.tar.xz'
ARCHIVE_BASE_SPACEAGE_0_MD5='56559ce107be62443b8743dc8696f65e'
ARCHIVE_BASE_SPACEAGE_0_SIZE='4741087'
ARCHIVE_BASE_SPACEAGE_0_VERSION='2.0.7-1'
ARCHIVE_BASE_SPACEAGE_0_URL='https://www.factorio.com/'

## Factorio (demo)

ARCHIVE_BASE_DEMO_0_NAME='factorio-demo_linux_2.0.69.tar.xz'
ARCHIVE_BASE_DEMO_0_MD5='ea86004b1f44d38e14cabcf26144d2e0'
ARCHIVE_BASE_DEMO_0_SIZE='1658679'
ARCHIVE_BASE_DEMO_0_VERSION='2.0.69-1'
ARCHIVE_BASE_DEMO_0_URL='https://www.factorio.com/download'

CONTENT_PATH_DEFAULT='factorio'
CONTENT_GAME_BIN_FILES='
bin/x64/factorio'
CONTENT_GAME_DATA_SPACEAGE_FILES='
data/elevated-rails
data/quality
data/space-age'
CONTENT_GAME_DATA_FILES='
data
config-path.cfg'
CONTENT_DOC_DATA_FILES='
doc-html'

USER_PERSISTENT_FILES='
*.cfg
*.dat
player-data.json'
USER_PERSISTENT_DIRECTORIES='
config
mods
saves'

APP_MAIN_EXE='bin/x64/factorio'
APP_MAIN_ICON='data/core/graphics/factorio-icon.png'
## Work around the engine poor support for symbolic links.
APP_MAIN_PRERUN='
# Work around the engine poor support for symbolic links.
for mod in core base elevated-rails quality space-age; do
	if [ ! -h "data/${mod}" ] && [ -e "${PATH_GAME_DATA}/data/${mod}" ]; then
		rm --force --recursive "data/${mod}"
		ln --symbolic "${PATH_GAME_DATA}/data/${mod}" "data/${mod}"
	fi
done
'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'
PACKAGES_LIST_SPACEAGE='
PKG_BIN
PKG_DATA_SPACEAGE
PKG_DATA'

PKG_BIN_ID_SPACEAGE="${GAME_ID}-space-age"
PKG_BIN_ID_DEMO="$GAME_ID_DEMO"
PKG_BIN_PROVIDES_SPACEAGE="
$GAME_ID"
PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN_DEPENDENCIES_SIBLINGS_SPACEAGE='
PKG_DATA
PKG_DATA_SPACEAGE'
PKG_BIN_DEPENDENCIES_LIBRARIES='
libc.so.6
libdl.so.2
libm.so.6
libpthread.so.0
libresolv.so.2
librt.so.1'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_ID_DEMO="${GAME_ID_DEMO}-data"
PKG_DATA_DESCRIPTION='data'

PKG_DATA_SPACEAGE_ID="${PKG_DATA_ID}-space-age"
PKG_DATA_SPACEAGE_DESCRIPTION="$PKG_DATA_DESCRIPTION - Space Age"

# Load common functions.

## Scan library paths used by ./play.it ≥ 2.35.
if [ -z "$PLAYIT_LIB2" ]; then
	for playit_lib_dir in \
		"$PWD" \
		"${HOME}/.local/lib/play.it" \
		'/usr/local/lib/play.it' \
		'/usr/lib/play.it'
	do
		playit_lib_path="${playit_lib_dir}/libplayit.sh.2"
		if [ -e "$playit_lib_path" ]; then
			PLAYIT_LIB2="$playit_lib_path"
			break
		fi
	done
fi
## Scan library paths used by ./play.it < 2.35.
## This can go away when PLAYIT_COMPATIBILITY_LEVEL is bumped to 2.35 or higher.
if [ -z "$PLAYIT_LIB2" ]; then
	for playit_lib_dir in \
		"$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'
	do
		playit_lib_path="${playit_lib_dir}/libplayit2.sh"
		if [ -e "$playit_lib_path" ]; then
			PLAYIT_LIB2="$playit_lib_path"
			break
		fi
	done
fi
## Error out if the library can not be found.
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.

content_inclusion_icons 'PKG_DATA'
content_inclusion_default

# Write launchers.

## Force the use of system-provided SDL.
APP_MAIN_PRERUN="$(application_prerun 'APP_MAIN')
$(launcher_tweak_sdl_override)"
PKG_BIN_DEPENDENCIES_LIBRARIES="$PKG_BIN_DEPENDENCIES_LIBRARIES
libSDL2-2.0.so.0"

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
