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

###
# My Lovely Family series:
# - My Lovely Daughter
# - My Lovely Wife
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260403.3

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID_DAUGHTER='my-lovely-daughter'
GAME_NAME_DAUGHTER='My Lovely Daughter'

GAME_ID_WIFE='my-lovely-wife'
GAME_NAME_WIFE='My Lovely Wife'

ARCHIVE_BASE_DAUGHTER_0_NAME='setup_my_lovely_daughter_1.27f6_gog_linkfix_(70980).exe'
ARCHIVE_BASE_DAUGHTER_0_MD5='66af1010c276944acc0c66ebef72cc0e'
ARCHIVE_BASE_DAUGHTER_0_TYPE='innosetup'
ARCHIVE_BASE_DAUGHTER_0_SIZE='337658'
ARCHIVE_BASE_DAUGHTER_0_VERSION='1.27f6-gog70980'
ARCHIVE_BASE_DAUGHTER_0_URL='https://www.gog.com/game/my_lovely_daughter'

ARCHIVE_BASE_WIFE_1_NAME='setup_my_lovely_wife_1.1.14f2_gog_(74768).exe'
ARCHIVE_BASE_WIFE_1_MD5='f6b68450cf40e9c596327dfc189bd8ff'
ARCHIVE_BASE_WIFE_1_TYPE='innosetup'
ARCHIVE_BASE_WIFE_1_SIZE='651887'
ARCHIVE_BASE_WIFE_1_VERSION='1.1.14f2-gog74768'
ARCHIVE_BASE_WIFE_1_URL='https://www.gog.com/game/my_lovely_wife'

ARCHIVE_BASE_WIFE_0_NAME='setup_my_lovely_wife_1.1.11_gog_(70816).exe'
ARCHIVE_BASE_WIFE_0_MD5='52472f09b7516676b29a43e3a40cd121'
ARCHIVE_BASE_WIFE_0_TYPE='innosetup'
ARCHIVE_BASE_WIFE_0_SIZE='691604'
ARCHIVE_BASE_WIFE_0_VERSION='1.1.11-gog70816'

UNITY3D_NAME_DAUGHTER='My Lovely Daughter'
UNITY3D_NAME_WIFE='My Lovely Wife'
# TODO: Drop unused libraries.
UNITY3D_PLUGINS_DAUGHTER='
msvcp100.dll
msvcr100.dll
session_xmpp_c.dll
sgup_api.dll
XInputInterface.dll'
UNITY3D_PLUGINS_WIFE='
InControlNative.dll
XInputInterface64.dll'
# TODO: Check if Galaxy libraries is required.
UNITY3D_PLUGINS_DAUGHTER="$UNITY3D_PLUGINS_DAUGHTER
Galaxy.dll"
UNITY3D_PLUGINS_WIFE="$UNITY3D_PLUGINS_WIFE
Galaxy64.dll"
# TODO: Check if Steam libraries are required.
UNITY3D_PLUGINS_DAUGHTER="$UNITY3D_PLUGINS_DAUGHTER
steam_api.dll"
UNITY3D_PLUGINS_WIFE="$UNITY3D_PLUGINS_WIFE
steam_api64.dll"

CONTENT_PATH_DEFAULT='.'

WINE_PERSISTENT_DIRECTORIES_DAUGHTER='
users/${USER}/AppData/LocalLow/GameChanger Studio/My Lovely Daughter'
WINE_PERSISTENT_DIRECTORIES_WIFE='
users/${USER}/AppData/LocalLow/GameChanger Studio/My Lovely Wife'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH_DAUGHTER='32'
PKG_BIN_ARCH_WIFE='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'

PKG_DATA_ID_DAUGHTER="${GAME_ID_DAUGHTER}-data"
PKG_DATA_ID_WIFE="${GAME_ID_WIFE}-data"
PKG_DATA_DESCRIPTION='data'

# 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
# shellcheck source=libplayit.sh.2
. "$PLAYIT_LIB2"

# Run default initialisation actions.

initialization_default "$@"

# Extract game data.

archive_extraction_default

# Include game data.

content_inclusion_icons 'PKG_DATA'
# Include Unity3D plugins.
case $(current_archive) in
	('ARCHIVE_BASE_DAUGHTER_'*)
		architecture='x86'
	;;
	('ARCHIVE_BASE_WIFE_'*)
		architecture='x86_64'
	;;
esac
unity3d_plugins_inclusion 'PKG_BIN' "$(unity3d_name)_Data/Plugins/${architecture}" "$(path_libraries)/$(unity3d_name)_Data/Plugins"
content_inclusion_default

# Write launchers.

# Install Unity3D plugins.
wineprefix_init_custom() {
	cat <<- EOF
	# Install shipped libraries.
	PATH_LIBRARIES_SYSTEM='$(path_libraries)'
	EOF
	cat <<- 'EOF'
	find "$PATH_LIBRARIES_SYSTEM" -iname '*.dll' | while read -r dll_source; do
		dll_destination="${PATH_PREFIX}${dll_source#"${PATH_LIBRARIES_SYSTEM}"}"
	    mkdir --parents "$(dirname "$dll_destination")"
	    ln --symbolic --force "$dll_source" "$dll_destination"
	done
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
