#!/bin/sh -e

rm -rf tests/
mkdir tests

cat >tests/testcase.1 <<EOF
# RUN: -a
auto eth0
iface eth0 inet static
  address 1.2.3.4
  netmask 255.255.255.0
  up echo hi
  post-up echo hello
EOF
cat >tests/up.1 <<EOF
====stdout====
====stderr====
Configuring interface eth0=eth0 (inet)
run-parts --verbose /etc/network/if-pre-up.d

ifconfig eth0 1.2.3.4 netmask 255.255.255.0  	 up

echo hi
echo hello
run-parts --verbose /etc/network/if-up.d
EOF

cat >tests/testcase.2 <<EOF
# RUN: -a
auto eth0 eth1 eth2
auto eth3 eth4 eth5
allow-hotplug eth6
iface eth0 inet static
  address 1.2.3.4
  netmask 255.255.255.0
iface eth1 inet static
  address 1.3.4.5
  netmask 255.255.255.0
iface eth2 inet static
  address 1.4.5.6
  netmask 255.255.255.0
iface eth3 inet static
  address 1.5.6.7
  netmask 255.255.255.0
iface eth4 inet static
  address 1.7.8.9
  netmask 255.255.255.0
iface eth5 inet static
  address 1.8.9.10
  netmask 255.255.255.0
iface eth6 inet static
  address 1.11.12.13
  netmask 255.255.255.0
iface eth7 inet static
  address 1.14.15.16
  netmask 255.255.255.0
EOF
cat >tests/up.2 <<EOF
====stdout====
====stderr====
Configuring interface eth0=eth0 (inet)
run-parts --verbose /etc/network/if-pre-up.d

ifconfig eth0 1.2.3.4 netmask 255.255.255.0  	 up

run-parts --verbose /etc/network/if-up.d
Configuring interface eth1=eth1 (inet)
run-parts --verbose /etc/network/if-pre-up.d

ifconfig eth1 1.3.4.5 netmask 255.255.255.0  	 up

run-parts --verbose /etc/network/if-up.d
Configuring interface eth2=eth2 (inet)
run-parts --verbose /etc/network/if-pre-up.d

ifconfig eth2 1.4.5.6 netmask 255.255.255.0  	 up

run-parts --verbose /etc/network/if-up.d
Configuring interface eth3=eth3 (inet)
run-parts --verbose /etc/network/if-pre-up.d

ifconfig eth3 1.5.6.7 netmask 255.255.255.0  	 up

run-parts --verbose /etc/network/if-up.d
Configuring interface eth4=eth4 (inet)
run-parts --verbose /etc/network/if-pre-up.d

ifconfig eth4 1.7.8.9 netmask 255.255.255.0  	 up

run-parts --verbose /etc/network/if-up.d
Configuring interface eth5=eth5 (inet)
run-parts --verbose /etc/network/if-pre-up.d

ifconfig eth5 1.8.9.10 netmask 255.255.255.0  	 up

run-parts --verbose /etc/network/if-up.d
EOF
cat >tests/testcase.3 <<EOF
# RUN: -a
auto eth0
iface eth0 inet static
  address 1.2.3.4
  netmask 255.255.255.0
iface eth0 inet6 static
  address 3ffe:ffff:100:f101::1
  netmask 64
EOF
cat >tests/up.3 <<EOF
====stdout====
====stderr====
Configuring interface eth0=eth0 (inet)
run-parts --verbose /etc/network/if-pre-up.d

ifconfig eth0 1.2.3.4 netmask 255.255.255.0             up

run-parts --verbose /etc/network/if-up.d
Configuring interface eth0=eth0 (inet6)
run-parts --verbose /etc/network/if-pre-up.d
modprobe -Q ipv6
ifconfig eth0    up
ifconfig eth0 add 3ffe:ffff:100:f101::1/64

run-parts --verbose /etc/network/if-up.d
EOF

cat >tests/testcase.4 <<EOF
# RUN: eth0=work
mapping eth0
  script tests/map.eth0.work
iface work inet static
  address 1.2.3.4
  netmask 255.255.255.0
  up echo hi
  post-up echo hello
EOF
cat >tests/up.4 <<EOF
====stdout====
====stderr====
Configuring interface eth0=work (inet)
run-parts --verbose /etc/network/if-pre-up.d

ifconfig eth0 1.2.3.4 netmask 255.255.255.0  	 up

echo hi
echo hello
run-parts --verbose /etc/network/if-up.d
EOF


result=true
for test in 1 2 3 4; do
	args="$(cat tests/testcase.$test | sed -n 's/^# RUN: //p')"
	./ifup -nv --force -i tests/testcase.$test $args \
		>tests/up-res-out.$test 2>tests/up-res-err.$test || 
		true
	(echo "====stdout===="; cat tests/up-res-out.$test
	 echo "====stderr===="; cat tests/up-res-err.$test) > tests/up-res.$test

	echo "Testcase $test: $args"
	
	if diff -ub tests/up.$test tests/up-res.$test; then
		echo "(okay)"
	else
		echo "(failed)"
		result=false
	fi
	echo "=========="
done

if $result; then
	echo "(okay overall)"
	exit 0
else
	echo "(failed overall)"
	exit 1
fi
