#!/bin/sh

# Copyright (C) 2005, 2006, 2008, 2009  Martin Michlmayr <tbm@cyrius.com>
# Copyright (C) 2008  Matthew Palmer <mpalmer@debian.org>

# This code is covered by the GNU General Public License (version 2
# or higher)

set -e

. /usr/lib/oldsys-preseed/functions

log() {
	logger -t oldsys-preseed "$@"
}

exit_unknown() {
	log "Unknown system - not writing preseed file"
	exit 0
}

# If this is set to "yes", the user *absolutely* cannot input anything
# before network-console comes up.  Therefore, preseed some info which
# is not optimal but which will ensure that network-console is reached
# without prompting for user input.
NONINTERACTIVE="yes"
FILE=/preseed.cfg

case "`archdetect`" in
	arm*/orion5x | arm*/kirkwood)
		machine=$(grep "^Hardware" /proc/cpuinfo | sed 's/Hardware\s*:\s*//')
		# /proc/device-tree may not exist on all architectures
		if [ -e /proc/device-tree/model ] ; then
		    dt_model=$(cat /proc/device-tree/model 2>/dev/null)
		else
		    dt_model="UNKNOWN"
		fi
		if echo "$machine" | grep -q "^Buffalo/Revogear Kurobox Pro" ||
		    echo "$dt_model" | grep -q "^Buffalo/Revogear Kurobox Pro"; then
			check_file /proc/mtd
			rootfs=$(get_mtdblock "rootfs")
			if [ -z "$rootfs" ]; then
				log "Can't find rootfs MTD partition"
				exit 0
			fi
			path=/tmp/oldsys-preseed
			mkdir -p $path/rootfs
			mount -t jffs2 -o ro /dev/$rootfs $path/rootfs || true
			INTERFACE=eth0
			parse_unix_tree $path/rootfs
			netinfo=$path/rootfs/etc/netinfo
			if [ -e "$netinfo" ]; then
				usage=$(sed -n '/^my_ipaddress/ {s/.*=//; p}' "$netinfo")
				if [ "$usage" = "dhcp" ]; then
					NET_CONFIG=dhcp
				else
					NET_CONFIG=static
					IP_ADDRESS=$usage
					NETMASK=$(sed -n '/^my_subnetmask/ {s/.*=//; p}' "$netinfo")
					GATEWAY=$(sed -n '/^my_dgw/ {s/.*=//; p}' "$netinfo")
				fi
			fi
			hostinfo=$path/rootfs/etc/host.info
			if [ -e "$hostinfo" ]; then
				HOSTNAME=$(sed -n '/^hostname/ {s/.*=//; p}' "$hostinfo")
			fi
			unset_matching_var "HOSTNAME" "KUROBOX-PRO"
			sanity_check_static_config
			if [ "$NET_CONFIG" != "static" ]; then
				IPADDRESS=192.168.11.150
				NETMASK=255.255.255.0
				GATEWAY=192.168.11.1
				[ -z "$NAMESERVERS" ] && NAMESERVERS=192.168.11.1
				dhcp_fallback $FILE
			fi
			umount $path/rootfs || true
			rmdir $path/rootfs $path || true
		elif echo "$machine" | grep -q "^Buffalo Linkstation Pro/Live" ||
		   echo "$machine" | grep -q "^Buffalo Linkstation LiveV3 (LS-CHL)" ||
		   echo "$machine" | grep -q "^Buffalo Linkstation Mini" ||
		   echo "$dt_model" | grep -q "^Buffalo Linkstation"; then
			# the default filesystem for the system partition is XFS, which isn't included
			# in our startup environment.  However, customized boxes might have ext3
			# instead, so try to mount anyway.
			path=/tmp/oldsys-preseed
			mkdir -p $path/rootfs
			mount -o ro /dev/sda2 $path/rootfs ||
				mount -o ro /dev/md1 $path/rootfs || true
			INTERFACE=eth0
			parse_unix_tree $path/rootfs
			info=$path/rootfs/etc/melco/info
			NET_CONFIG=dhcp
			if [ -e "$info" ]; then
				HOSTNAME=$(sed -n '/^hostname/ {s/.*=//; p}' "$info")
				usage=$(sed -n '/^my_ipaddress/ {s/.*=//; p}' "$info")
				if [ "$usage" = "dhcp" ]; then
					NET_CONFIG=dhcp
				else
					NET_CONFIG=static
					IPADDRESS=$usage
					NETMASK=$(sed -n '/^my_subnetmask/ {s/.*=//; p}' "$info")
					GATEWAY=$(sed -n '/^my_dgw/ {s/.*=//; p}' "$info")
					NAMESERVERS="$(sed -n '/^my_dns1/ {s/.*=//; p}' "$info") $(sed -n '/^my_dns2/ {s/.*=//; p}' "$info")"
				fi
			fi
			MAC=$(ip addr | grep '^[[:space:]]*link/ether' | head -n 1 | sed 's/.*ether \([^ ]*\).*/\1/')
			unset_matching_var "HOSTNAME" LS-GL$(echo "$MAC" | sed 's/^..:..:..:..:.//' | sed 's/://g')
			sanity_check_static_config
			if [ "$NET_CONFIG" != "static" ]; then
				IPADDRESS=192.168.11.150
				NETMASK=255.255.255.0
				GATEWAY=192.168.11.1
				[ -z "$NAMESERVERS" ] && NAMESERVERS=192.168.11.1
				dhcp_fallback $FILE
			fi
			umount $path/rootfs || true
			rmdir $path/rootfs $path || true
		elif echo "$machine" | grep -q "^D-Link DNS-323"; then
			check_file /proc/mtd
			rootfs=$(get_mtdblock "MTD1")
			if [ -z "$rootfs" ]; then
				log "Can't find rootfs MTD partition"
				exit 0
			fi
			INTERFACE=eth0
			path=/tmp/oldsys-preseed
			mkdir -p $path/rootfs
			mount -t minix -o ro /dev/$rootfs $path/rootfs
			parse_sib_conf $path/rootfs/sib.conf
			unset_matching_var "HOSTNAME" "DNS-323"
			unset_matching_var "HOSTNAME" "CH3SNAS"
			umount $path/rootfs
			rmdir $path/rootfs $path || true
			sanity_check_static_config
			if [ "$NET_CONFIG" != "static" ]; then
				IPADDRESS=192.168.0.32
				NETMASK=255.255.255.0
				GATEWAY=192.168.0.1
				[ -z "$NAMESERVERS" ] && NAMESERVERS=192.168.0.1
				dhcp_fallback $FILE
			fi
		elif echo "$machine" | grep -q "^HP Media Vault mv2120"; then
			path=/tmp/oldsys-preseed
			mkdir -p $path/sda5
			mount -t ext3 -o ro /dev/sda5 $path/sda5 || true
			INTERFACE=eth0
			if [ -e $path/sda5/etc/nas_conf_db.xml ]; then
				dhcp=$(remove_xml $(grep "<dhcpenabled>" $path/sda5/etc/nas_conf_db.xml))
				if [ "$dhcp" = "true" ]; then
					NET_CONFIG=dhcp
				else
					NET_CONFIG=static
					IPADDRESS=$(remove_xml $(grep "<ipaddr>" $path/sda5/etc/nas_conf_db.xml))
					NETMASK=$(remove_xml $(grep "<netmask>" $path/sda5/etc/nas_conf_db.xml))
					GATEWAY=$(remove_xml $(grep "<gateway>" $path/sda5/etc/nas_conf_db.xml))
					for i in $(grep "<dnsserver>" $path/sda5/etc/nas_conf_db.xml); do
							var_add NAMESERVERS $(remove_xml $i)
					done
				fi
				HOSTNAME=$(remove_xml $(grep "<hostname>" $path/sda5/etc/nas_conf_db.xml | head -n 1))
				unset_matching_var "HOSTNAME" "HPMediaVault"
				DOMAIN=$(remove_xml $(grep "<domain>" $path/sda5/etc/nas_conf_db.xml))
				unset_matching_var "DOMAIN" "local"
			else
				NET_CONFIG=dhcp
			fi
			sanity_check_static_config
			if [ "$NET_CONFIG" != "static" ]; then
				IPADDRESS=192.168.1.100
				NETMASK=255.255.255.0
				GATEWAY=192.168.1.1
				[ -z "$NAMESERVERS" ] && NAMESERVERS=192.168.1.1
				dhcp_fallback $FILE
			fi
			umount $path/sda5 || true
			rmdir $path/sda5 $path || true
		elif echo "$machine" | grep -q "^QNAP TS-109/TS-209" ||
		   echo "$machine" | grep -q "^QNAP TS-119/TS-219" ||
		   echo "$machine" | grep -q "^QNAP TS-409" ||
		   echo "$machine" | grep -q "^QNAP TS-41x" ||
		   echo "$dt_model" | grep -q "^QNAP TS219 family" ||
		   echo "$dt_model" | grep -q "^QNAP TS419 family"; then
			path=/tmp/oldsys-preseed
			mkdir -p $path/sda1
			mount -t ext2 -o ro /dev/sda1 $path/sda1 || true
			INTERFACE=eth0
			if [ -e $path/sda1/.config/uLinux.conf ]; then
				capture=0
				eth=$(cat $path/sda1/.config/uLinux.conf | while read line; do
					if [ $capture -gt 0 ]; then echo $line ; fi
					if [ "$line" = "[eth0]" ]; then capture=1 ; fi
					if [ "$line" = "" ]; then capture=0 ; fi
				done)
				usage=$(echo "$eth" | grep "^Usage" | sed 's/^.* //')
				if [ "$usage" = "DHCP" ]; then
					NET_CONFIG=dhcp
				else
					NET_CONFIG=static
					IPADDRESS=$(echo "$eth" | grep "^IP Address" | sed 's/^.* //')
					NETMASK=$(echo "$eth" | grep "^Subnet Mask" | sed 's/^.* //')
					GATEWAY=$(echo "$eth" | grep "^Gateway" | sed 's/^.* //')
				fi
				for i in $(grep "Domain Name Server" $path/sda1/.config/uLinux.conf | sed 's/^.* //' | grep -v 0.0.0.0); do
						var_add NAMESERVERS "$i"
				done
				HOSTNAME=$(grep "Server Name" $path/sda1/.config/uLinux.conf | sed 's/^.* //')
				MAC=$(ip addr | grep '^[[:space:]]*link/ether' | head -n 1 | sed 's/.*ether \([^ ]*\).*/\1/')
				unset_matching_var "HOSTNAME" NAS$(echo "$MAC" | sed 's/^..:..:..://' | sed 's/://g')
			else
				NET_CONFIG=dhcp
			fi
			sanity_check_static_config
			if [ "$NET_CONFIG" != "static" ]; then
				IPADDRESS=192.168.1.100
				NETMASK=255.255.255.0
				GATEWAY=192.168.1.1
				[ -z "$NAMESERVERS" ] && NAMESERVERS=192.168.1.1
				dhcp_fallback $FILE
			fi
			umount $path/sda1 || true
			rmdir $path/sda1 $path || true
		elif echo "$machine" | grep -q "^LaCie" ||
		   echo "$dt_model" | grep -q "^LaCie"; then
			NET_CONFIG=dhcp
			IPADDRESS=192.168.1.100
			NETMASK=255.255.255.0
			GATEWAY=192.168.1.1
			NAMESERVERS=192.168.1.1
			dhcp_fallback $FILE
		else
			exit_unknown
		fi
	;;
	*)
		exit_unknown
	;;
esac

if [ "$NONINTERACTIVE" = "yes" ]; then
	# Just continue if d-i enters lowmem mode
	add "$FILE" "lowmem/low" "note"
	# Any hostname and domain names assigned from DHCP take precedence
	# over values set here.  However, setting the values still prevents
	# the questions from being shown, even if values come from dhcp.
	add "$FILE" "netcfg/get_hostname" "string" "debian"
	add "$FILE" "netcfg/get_domain" "string" "example.org"
	# I'm not terribly happy to preseed a generic password but I guess
	# there's no other way on some machines.
	add "$FILE" "network-console/password" "password" "install"
	add "$FILE" "network-console/password-again" "password" "install"
	# Continue if there is missing firmware at ethdetect, and hope that
	# it is not actually needed to get on the network.
	add "$FILE" "ethdetect/prompt_missing_firmware" "boolean" "false"
	# Allow network configuration to finish even if the DHCP server
	# doesn't send a gateway.  This way, users can at least connect
	# to the installer and fix their config.
	add "$FILE" "netcfg/no_default_route" "boolean" "true"
fi

# Workaround for broken partconf
add "$FILE" "partconf/already-mounted" "boolean" "false"

generate_preseed_file $FILE

