#! /bin/sh
set -e

. /usr/share/debconf/confmodule

# File paths for various configuration files
FILE_PATH_NM_CONFIG=etc/NetworkManager/system-connections
FILE_INTERFACES=/etc/network/interfaces
FILE_NETCFG_CONNECTION_TYPE=/tmp/connection_type

# The connection type file is written by the nm-conf code.
# The base-installer snippet will already take care of copying
# the generated interfaces file, so if we are not dealing
# with nm-conf anyway: just exit.
if [ ! -e $FILE_NETCFG_CONNECTION_TYPE ]; then
	logger -t netcfg "DEBUG: copy-config: $FILE_NETCFG_CONNECTION_TYPE not found: netcfg did not complete or was compiled without network-manager support; exiting."
	exit 0
fi

# Flag to determine whether Network Manager is installed.
if in-target sh -c "dpkg-query -s network-manager 2>/dev/null | grep -q '^Status: install ok installed'"; then
	NM_IS_INSTALLED=true
else
	NM_IS_INSTALLED=false
fi

# The type of the connection used during installation
NETCFG_CONNECTION_TYPE=$(cat $FILE_NETCFG_CONNECTION_TYPE | \
    grep "connection type" | cut -d ':' -f2 | sed 's/ //g')
NETCFG_CONNECTION_SECURITY=$(cat $FILE_NETCFG_CONNECTION_TYPE | \
    grep "security" | cut -d ':' -f2 | sed 's/ //g')

# netcfg/target_network_config question values
CONFIG_NM="nm_config"
CONFIG_INTERFACES="ifupdown"
CONFIG_LOOPBACK="loopback"

db_get netcfg/target_network_config

# Check for preseeding. If the value of the question is empty then set
# default options. Document automatic selection changes in the template.
if [ -z "$RET" ]; then
	if $NM_IS_INSTALLED; then
		db_set netcfg/target_network_config $CONFIG_NM
	else
		if [ "$NETCFG_CONNECTION_TYPE" = "wireless" ]; then
			db_set netcfg/target_network_config $CONFIG_LOOPBACK
		else # vlan/wired
			db_set netcfg/target_network_config $CONFIG_INTERFACES
		fi
	fi
fi

db_get netcfg/target_network_config

case $RET in
    $CONFIG_NM)
	# Copy NM config file. First make sure the directory exists
	mkdir -p /target/$FILE_PATH_NM_CONFIG
	cp /$FILE_PATH_NM_CONFIG/* /target/$FILE_PATH_NM_CONFIG/

	# Write netplan config file to hand over management to NM
	cat <<EOF > /target/etc/netplan/01-network-manager-all.yaml
# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
EOF

	# Rewrite /etc/network/interfaces to contain only loopback
	netcfg write_loopback
	;;

    $CONFIG_LOOPBACK)
	# Rewrite /etc/network/interfaces to contain only loopback
	netcfg write_loopback
	;;
esac

case $RET in
    $CONFIG_NM|$CONFIG_LOOPBACK)
	# Copy /etc/network/interfaces to target.
	mkdir -p /target$(dirname $FILE_INTERFACES)
	cp $FILE_INTERFACES /target$FILE_INTERFACES
	;;
esac

exit 0
