#!/bin/bash
# sb2-upgrade-config:
# - This script is used to automatically update/upgrade configuration files.
# - Called from the "sb2" script whenever needed; see the configuration 
#   version check in load_configuration() of "sb2".
# - Should not be called directly from the command line!
#
# SPDX-FileCopyrightText: Copyright (c) 2009 Nokia Corporation.
#
# SPDX-License-Identifier: GPL-2.0-or-later
#

SBOX_DATADIR=/usr/share
SBOX_DIR=/usr
SBOX_TARGET=$1
SBOX_CONFIG_DIR=~/.scratchbox2/$SBOX_TARGET/sb2.config.d 

exit_error()
{
	echo "$my_path: Error: $*"
	exit 1
}

if [ -z "$SBOX_TARGET" ]; then
	exit_error "this script is intended for sb2's internal use only!"
fi 

log_config_action()
{
	tstamp=$(date '+%Y-%m-%d %H:%M:%S')
	echo "$tstamp	$*" >>"$SBOX_CONFIG_DIR"/CONFIG-LOG
}

# Get the original arguments that were specified to sb2-init from
# the old configuration file; we only need three variables from that file..
get_sb2_init_arguments_from_old_config_file()
{
	OLD_CONFIG_FILE=$HOME/.scratchbox2/$SBOX_TARGET/sb2.config

	if [ ! -f $OLD_CONFIG_FILE ]; then
		exit_error "$OLD_CONFIG_FILE does not exist"
	fi



	# Check version
	SBOX_CONFIG_VERSION=$(sed --regexp-extended \
				  -n '/^SBOX_CONFIG_VERSION/ s/^SBOX_CONFIG_VERSION=.*"/\1/ p' $OLD_CONFIG_FILE)
	if [ "$SBOX_CONFIG_VERSION" -lt 5 ]; then
		exit_error "configuration file version is too old ($OLD_CONFIG_FILE)"
	fi


	# Get original options & target name & compiler(s)
	SBOX_INIT_ORIG_ARGS=$(sed --regexp-extended \
				  -n '/^SBOX_INIT_ORIG_ARGS/ s/^SBOX_INIT_ORIG_ARGS=\"(.*)\"/\1/ p' $OLD_CONFIG_FILE)

	# Get original target_root
	SBOX_TARGET_ROOT=$(sed --regexp-extended \
			       -n '/^SBOX_TARGET_ROOT/ s/^SBOX_TARGET_ROOT=(.*)/\1/ p' $OLD_CONFIG_FILE)
	export SB2INIT_TARGET_ROOT=$SBOX_TARGET_ROOT

	$SBOX_DATADIR/scratchbox2/scripts/sb2-parse-sb2-init-args $SBOX_INIT_ORIG_ARGS \
		> $SBOX_CONFIG_DIR/sb2-init-args
	log_config_action "Config upgrade: arguments of original sb2-init restored from old config file"
}

update_toolchain_configs()
{
	secondary_compiler=""

	if [ -n "$SB2INIT_ARCH" ]; then
		gccconfig_arch_option="-A $SB2INIT_ARCH"
	else
		gccconfig_arch_option=""
	fi
	if [ -n "$SB2INIT_MACHINE_ARCH" ]; then
		gccconfig_arch_option2="-M $SB2INIT_MACHINE_ARCH"
	else
		gccconfig_arch_option2=""
	fi
	for compiler_path in "$@"; do
		# echo "Updating compiler $compiler_path"
		log_config_action "Config upgrade: settings for compiler $compiler_path"
		if ! $SBOX_DATADIR/scratchbox2/scripts/sb2-config-gcc-toolchain \
		     $secondary_compiler \
		     $gccconfig_arch_option \
		     $gccconfig_arch_option2 \
		     -R "$SB2INIT_TARGET_ROOT" \
		     -S "$SBOX_DATADIR" \
		     -t "$SB2INIT_TARGET" \
		     -m "$SB2INIT_MAPPING_MODE" \
		     -C "$SB2INIT_SBOX_EXTRA_CROSS_COMPILER_ARGS" \
		     -L "$SB2INIT_SBOX_EXTRA_CROSS_LD_ARGS" \
		     -- $compiler_path
		then
		    log_config_action "failed to configure $compiler_path"
		    echo "Failed to configure $compiler_path"
		    exit 1
		fi
		secondary_compiler="-V"
	done
}

# "host-ld_library_path.conf" contains settings that are needed for running
# host-compatible binaries in host mode (i.e. started directly, not 
# indirectly by running ld.so).
update_host_ld_library_path()
{
	# Old versions of sb.config will set $LD_LIBRARY_PATH,
	# which is not nice... save the current value and restore it later.
	saved_LD_LIBRARY_PATH=$LD_LIBRARY_PATH

	# Make sure that these stay in the library path:

	# Prefix to be added (trailing colon is needed, if not empty!)
	HOST_LD_LIBRARY_PATH_PREFIX=""

	HOST_LD_LIBRARY_PATH_LIBSB2="$SBOX_DIR/lib/libsb2:$SBOX_DIR/lib64/libsb2:$SBOX_DIR/lib32/libsb2:/emul/lib64/libsb2:/emul/lib32/libsb2"

	# Suffix to be added (leading colon is needed, if not empty!)
	HOST_LD_LIBRARY_PATH_SUFFIX=""

	OLD_CONFIG_FILE=$HOME/.scratchbox2/$SBOX_TARGET/sb2.config

	if [ ! -f $OLD_CONFIG_FILE ]; then
		exit_error "$OLD_CONFIG_FILE does not exist"
	fi
	# Old version of the config file expects to see LD_LIBRARY_PATH set
	# to the following:
	old_cfg_file_expects_LDLBRPATH="$SBOX_DIR/lib/libsb2:$SBOX_DIR/lib64/libsb2:$SBOX_DIR/lib32/libsb2:/emul/lib64/libsb2:/emul/lib32/libsb2"
	LD_LIBRARY_PATH="$old_cfg_file_expects_LDLBRPATH"
	. $OLD_CONFIG_FILE
	if [ "$LD_LIBRARY_PATH" != "$old_cfg_file_expects_LDLBRPATH" ]; then
		# Old version of sb2.config, it set the path.
		HOST_LD_LIBRARY_PATH_LIBSB2="$LD_LIBRARY_PATH"
	else
		# Newer version of sb2.config,
		# LD_LIBRARY_PATH not set there (good), can use separate 
		# components...
		if [ -n "$SBOX_TOOLS_ROOT" ]; then
			# Prefix to be added (notice the trailing colon!)
			HOST_LD_LIBRARY_PATH_PREFIX="/usr/local/lib:/usr/lib/:/usr/lib64:/lib:/lib64:"
		fi
	fi
	LD_LIBRARY_PATH=$saved_LD_LIBRARY_PATH

	echo "HOST_LD_LIBRARY_PATH_PREFIX=\"$HOST_LD_LIBRARY_PATH_PREFIX\"" >$SBOX_CONFIG_DIR/host-ld_library_path.conf
	echo "HOST_LD_LIBRARY_PATH_LIBSB2=\"$HOST_LD_LIBRARY_PATH_LIBSB2\"" >>$SBOX_CONFIG_DIR/host-ld_library_path.conf
	echo "HOST_LD_LIBRARY_PATH_SUFFIX=\"$HOST_LD_LIBRARY_PATH_SUFFIX\"" >>$SBOX_CONFIG_DIR/host-ld_library_path.conf
	log_config_action "Created LD_LIBRARY_PATH configuration file for host-compatible binaries"
}

update_debian_config()
{
	$SBOX_DATADIR/scratchbox2/scripts/sb2-config-debian -t $SBOX_TARGET
}

if [ ! -d $SBOX_CONFIG_DIR ]; then
	mkdir $SBOX_CONFIG_DIR
	log_config_action "Config upgrade: Created configuration directory"
fi

if [ ! -f $SBOX_CONFIG_DIR/sb2-init-args ]; then
	# This is an old target.
	# Need to get sb2-init's parameters from the old config file
	get_sb2_init_arguments_from_old_config_file
fi

# read in the values that were used when sb2-init was executed:
. $SBOX_CONFIG_DIR/sb2-init-args

if [ -z "$SB2INIT_MAPPING_MODE" ]; then
	# Mapping mode was not specified when sb2-init was executed
	# => use the default mode.
	SB2INIT_MAPPING_MODE="simple"
fi

#==============================================
# Finally:

# check if we need to actually update something:
# version "11" added separate config files for gcc toolchains:
# version "12" version-specific specs files for gcc compilers:
# version "13" version- and architecture-specific specs files for gcc compilers:
# version "14" fixes a bug in previous ones; "-A" was not relayed to gcc config
# version "15" just forces a reconfiguration (to check that the "specs" file is
#              used, due to a bug in the top-level Makefile of scratchbox2)
# version "16" added "host-ld_library_path.conf"; technically, we
#		don't have to reconfigure gcc when moving from 15 to 16,
#		but it is easier to just do everything..
# version "17" added a separate tool for configuring DEB_* variables
#		and gcc configuration sets SBOX_GCC_TARGET even if 
#		-A option was given to sb2-init
# version "18" added cross_gcc_progs_path to gcc configuration
# version "19": changed detection of -Wno-poison-system-directories (gcc config)
# version "20": A bug has been fixed in gcc configuration.
if [ ! -f $SBOX_CONFIG_DIR/config-stamp.20 ]; then
	update_toolchain_configs $SB2INIT_COMPILERS

	# DANGER: this will read in the complete old sb2.config file => must
	# be the last step!! (that file will set various variables...)
	update_host_ld_library_path

	# ...well, the previous one wasn't the last thing.
	# "sb2-config-debian" wants to execute the "sb2" command,
	# so this is the new must-be-the-last-thing:
	update_debian_config
fi

log_config_action "Config updated to version #20"
touch $SBOX_CONFIG_DIR/config-stamp.20

