#!/bin/sh
# SPDX-License-Identifier: ACSL
# SPDX-FileCopyrightText: © 2025 Antoine Le Gonidec <vv221@dotslashplay.it>
set -o errexit

###
# Old Skies
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260104.3

PLAYIT_COMPATIBILITY_LEVEL='2.34'

GAME_ID='old-skies'
GAME_NAME='Old Skies'

ARCHIVE_BASE_0_NAME='old_skies_v2_0a_82221.sh'
ARCHIVE_BASE_0_MD5='ccc4e578bf71a86a70dca8b4c12bab73'
ARCHIVE_BASE_0_SIZE='4406691'
ARCHIVE_BASE_0_VERSION='2.0a-gog82221'
ARCHIVE_BASE_0_URL='https://www.gog.com/game/old_skies'

## Optional icons pack
ARCHIVE_OPTIONAL_ICONS_NAME='old-skies_icons.tar.xz'
ARCHIVE_OPTIONAL_ICONS_MD5='6994d258c4a03bc48a91b89e515dca9c'
ARCHIVE_OPTIONAL_ICONS_URL='https://downloads.dotslashplay.it/games/old-skies/'
CONTENT_ICONS_PATH='.'
CONTENT_ICONS_FILES='
16x16
32x32
48x48
64x64
128x128
256x256'

CONTENT_PATH_DEFAULT='data/noarch/game'
CONTENT_LIBS_BIN_AGS_RELATIVE_PATH='lib64'
CONTENT_LIBS_BIN_AGS_FILES='
alleg-alsadigi.so
alleg-alsamidi.so
alleg-sdl2digi.so
libagscontrollerpulp.so
libagsshell.so
libaldmb.so.1
liballeg.so.4.4
libdumb.so.1'
CONTENT_GAME_MAIN_FILES='
OldSkies.ags
acsetup.cfg
vernum.osk
winsetup
*.vox'
CONTENT_GAME_BIN_AGS_FILES='
ags64'
CONTENT_DOC_MAIN_FILES='
licenses'

APP_MAIN_ICON='../support/icon.png'

## Dedicated Old Skies support is not available from ScummVM.
## (ScummVM 2.9.1)
APP_SCUMMVM_SCUMMID='ags:ags'

## With ScummVM animation is slowed down,
## support for the shipped engine is provided as a workaround.
## (ScummVM 2.9.1)
APP_AGS_EXE='ags64'

PACKAGES_LIST='
PKG_MAIN
PKG_BIN_SCUMMVM
PKG_BIN_AGS'

PKG_MAIN_DEPENDENCIES_SIBLINGS='
PKG_BIN'

PKG_BIN_ID="${GAME_ID}-bin"
PKG_BIN_PROVIDES="
$PKG_BIN_ID"

PKG_BIN_SCUMMVM_ID="${PKG_BIN_ID}-scummvm"
PKG_BIN_SCUMMVM_PROVIDES="$PKG_BIN_PROVIDES"
PKG_BIN_SCUMMVM_DESCRIPTION='Run with ScummVM'

PKG_BIN_AGS_ID="${PKG_BIN_ID}-ags"
PKG_BIN_AGS_PROVIDES="$PKG_BIN_PROVIDES"
PKG_BIN_AGS_DESCRIPTION='Run with shipped AGS binaries'
PKG_BIN_AGS_ARCH='64'
PKG_BIN_AGS_DEPENDENCIES_LIBRARIES='
libasound.so.2
libc.so.6
libdl.so.2
libgcc_s.so.1
libICE.so.6
libm.so.6
libogg.so.0
libpthread.so.0
librt.so.1
libSDL2-2.0.so.0
libSM.so.6
libstdc++.so.6
libtheora.so.0
libvorbis.so.0
libX11.so.6
libXcursor.so.1
libXext.so.6
libXxf86vm.so.1'

# 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_MAIN' 'APP_MAIN'
content_inclusion_default

# Write launchers.

launchers_generation 'PKG_BIN_SCUMMVM' 'APP_SCUMMVM'
launchers_generation 'PKG_BIN_AGS' 'APP_AGS'

# Build packages.

packages_generation
printf '\n'
printf 'ScummVM:'
print_instructions 'PKG_MAIN' 'PKG_BIN_SCUMMVM'
printf 'Adventure Game Studio:'
print_instructions 'PKG_MAIN' 'PKG_BIN_AGS'

# Clean up.

working_directory_cleanup

exit 0
