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

###
# System Shock 2
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260707.4

PLAYIT_COMPATIBILITY_LEVEL=2.35

GAME_ID='system-shock-2'
GAME_NAME='System Shock 2'

ARCHIVE_BASE_6_NAME='setup_system_shocktm_2_2.48_(31076).exe'
ARCHIVE_BASE_6_MD5=15d8702a3a6de94e5c04a0006299b9e0
ARCHIVE_BASE_6_TYPE=innosetup
ARCHIVE_BASE_6_SIZE=650274
ARCHIVE_BASE_6_VERSION=2.48-2

ARCHIVE_BASE_5_NAME='setup_system_shocktm_2_2.48_(31077).exe'
ARCHIVE_BASE_5_MD5=b8bbec00aef894db6641e46a0446cbb2
ARCHIVE_BASE_5_TYPE=innosetup
ARCHIVE_BASE_5_SIZE=660000
ARCHIVE_BASE_5_VERSION=2.48-1

ARCHIVE_BASE_4_NAME='setup_system_shock_2_2.47_nd_(22087).exe'
ARCHIVE_BASE_4_MD5=cc2ff390b566364447dc5bd05757fe57
ARCHIVE_BASE_4_TYPE=innosetup
ARCHIVE_BASE_4_SIZE=670000
ARCHIVE_BASE_4_VERSION=2.47-1

ARCHIVE_BASE_3_NAME='setup_system_shock_2_2.46_update_3_(19935).exe'
ARCHIVE_BASE_3_MD5=cdafcdea01556eccab899f94503843df
ARCHIVE_BASE_3_TYPE=innosetup
ARCHIVE_BASE_3_SIZE=670000
ARCHIVE_BASE_3_VERSION=2.46-4

ARCHIVE_BASE_2_NAME='setup_system_shock_2_2.46_update_2_(18733).exe'
ARCHIVE_BASE_2_MD5=39fab64451ace95966988bb90c7bb17e
ARCHIVE_BASE_2_TYPE=innosetup
ARCHIVE_BASE_2_SIZE=680000
ARCHIVE_BASE_2_VERSION=2.46-3

ARCHIVE_BASE_1_NAME='setup_system_shock_2_2.46_update_(18248).exe'
ARCHIVE_BASE_1_MD5=b76803e4a632b58527eada8993999143
ARCHIVE_BASE_1_TYPE=innosetup
ARCHIVE_BASE_1_SIZE=690000
ARCHIVE_BASE_1_VERSION=2.46-2

ARCHIVE_BASE_0_NAME='setup_system_shock_2_2.46_nd_(11004).exe'
ARCHIVE_BASE_0_MD5=98c3d01d53bb2b0dc25d7ed7093a67d3
ARCHIVE_BASE_0_TYPE=innosetup
ARCHIVE_BASE_0_SIZE=680000
ARCHIVE_BASE_0_VERSION=2.46-1

CONTENT_PATH_DEFAULT='.'
CONTENT_PATH_DEFAULT_2='app'
CONTENT_PATH_DEFAULT_1='app'
CONTENT_PATH_DEFAULT_0='app'
CONTENT_GAME_MAIN_FILES='
Binds
misdml
OSM
patch
sq_scripts
LGVID.AX
user.bnd
SHOCK.BND
CAM.CFG
cam_ext.cfg
install.cfg
SHOCK.CFG
patch_ext.crf
SS2.exe
cam_mod.ini
lg.ini'
CONTENT_GAME0_MAIN_FILES='
Data/res
Data/allobjs.osm
Data/motiondb.bin
Data/*.DIF
Data/*.GAM
Data/*.MIS
Data/*.RES'
# TODO: Drop unused libraries.
# TODO: Install libraries in a standard path.
CONTENT_GAME1_MAIN_FILES='
Microsoft.VC90.CRT/Microsoft.VC90.CRT.manifest
Microsoft.VC90.CRT/msvcm90.dll
Microsoft.VC90.CRT/msvcp90.dll
Microsoft.VC90.CRT/msvcr90.dll
7z.dll
D3DX9_43.dll
ffmpeg.dll
fmsel.dll
ir41_32.dll
ir50_32.dll
MSVCRT40.DLL'
CONTENT_GAME2_MAIN_RELATIVE_PATH='__support/app'
CONTENT_GAME2_MAIN_FILES='
*.cfg
*.ini'
# Older builds used a different binary name.
CONTENT_GAME3_MAIN_FILES_OLD='
shock2.exe'
CONTENT_GAME3_MAIN_FILES_3=$CONTENT_GAME3_MAIN_FILES_OLD
CONTENT_GAME3_MAIN_FILES_2=$CONTENT_GAME3_MAIN_FILES_OLD
CONTENT_GAME3_MAIN_FILES_1=$CONTENT_GAME3_MAIN_FILES_OLD
CONTENT_GAME3_MAIN_FILES_0=$CONTENT_GAME3_MAIN_FILES_OLD
# Cutscenes are installed through a dedicated package, to make them easier to disable.
# This should help with a crash on launch seemingly related to a failure to play a video.
CONTENT_GAME_VIDEO_FILES='
Data/cutscenes'
# TODO: Install libraries in a standard path.
CONTENT_GAME0_VIDEO_FILES='
lgvid.dll'
CONTENT_DOC_MAIN_FILES='
doc
release_notes.txt
SS2 community fixes.txt
troubleshooting.txt
Manual.pdf
Readme.wri
readmep.wri'

USER_PERSISTENT_DIRECTORIES='
current
save_0
save_1
save_2
save_3
save_4
save_5
save_6
save_7
save_8
save_9
save_10
save_11
save_12
save_13
save_14'
USER_PERSISTENT_FILES='
*.bnd
*.BND
*.cfg
*.CFG
*.ini'
# TODO: Do we need to keep log files?
USER_PERSISTENT_FILES="$USER_PERSISTENT_FILES
*.log"

APP_MAIN_EXE='SS2.exe'
APP_MAIN_EXE_3='shock2.exe'
APP_MAIN_EXE_2='shock2.exe'
APP_MAIN_EXE_1='shock2.exe'
APP_MAIN_EXE_0='shock2.exe'

PACKAGES_LIST='
PKG_MAIN
PKG_VIDEO'

PKG_MAIN_ARCH=32
# Easier upgrades from pre-20260707.1 packages
PKG_MAIN_PROVIDES='
system-shock-2-data'

PKG_VIDEO_ID=${GAME_ID}-cutscenes
PKG_VIDEO_DESCRIPTION='Cutscenes'
PKG_VIDEO_ARCH=32
PKG_VIDEO_DEPENDENCIES_SIBLINGS='
PKG_MAIN'
PKG_VIDEO_DEPENDENCIES_GSTREAMER_PLUGINS='
video/x-msvideo'

# Load common functions.

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
if [ -z "$PLAYIT_LIB2" ]; then
	printf '\n\033[1;31mError:\033[0m\n'
	printf 'libplayit.sh.2 not found.\n'
	exit 1
fi
. "$PLAYIT_LIB2"

# Run default initialisation actions.

initialization_default "$@"

# Extract game data.

archive_extraction_default

# Include game data.

content_inclusion_icons
content_inclusion_default

# Write launchers.

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
