#!/bin/bash

# defaults
if [ -d local ] ; then # running with carton
  CMD="perl -Ilocal/lib/perl5 -CSD ../sbin/ngcp-network"
else
  CMD="perl -CSD ../sbin/ngcp-network"
fi

PATH=$PATH:../t/fixtures/bin/

count=0
RC=0

OUTPUT=$(mktemp -t ngcpcfg-testsuite.XXXXXXXXXX) || exit 1

# generated files
rm -rf testfiles
mkdir testfiles

bailout() {
  rm -f "$OUTPUT"
}

trap bailout HUP INT QUIT ABRT ALRM TERM

OK() {
    printf "ok %s           %s\n" "$1" "$2"
}

FAIL() {
    printf "not ok %s        %s\n" "$1" "$2"
    RC=1
}

assertEqualFiles() {
  [ "$#" -eq 2 ] || return 1

  (( count++ ))

  if cmp "$1" "$2" 2>/dev/null ; then
    OK "$count" "$2" >> "$OUTPUT"
  else
    echo "files $1 and $2 do not match" >&2
    diff -u "$1" "$2" >&2
    FAIL "$count" "$2 # $2 differs from $1" >> "$OUTPUT"
    return 1
  fi
}


# make sure ip/netmask/interface can be set on existing host
${CMD} --input-file=network-config/network_pro.yml --output-file=testfiles/eth42.yml \
       --host=sp1 --set-interface=eth42 --ip=203.0.113.42 --netmask=255.255.255.248
assertEqualFiles testfiles/eth42.yml network-config/eth42.yml

# test IPv4/IPv6 with many options
cp network-config/network_pro.yml testfiles/eth46.yml
${CMD} --input-file=testfiles/eth46.yml --output-file=testfiles/eth46.yml \
       --host=sp1 --set-interface=eth46 \
       --hwaddr c0:ff:ee:15:90:0d \
       --ip=127.11.22.33 --netmask=255.0.0.0 --gateway 127.11.22.254 \
       --ipv6 2001:aaaa:1111:bbbb:2222:cccc:3333:dddd --netmask-ipv6 64 --gateway-ipv6 2001:aaaa:1111:bbbb:2222:cccc:3333:0001 \
       --dns 127.0.0.1 --dns 203.0.113.42 --dns ::1 --dns 2606:4700:4700::1111
${CMD} --input-file=testfiles/eth46.yml --output-file=testfiles/eth46.yml \
       --host=sp1 --set-interface=bond0 \
       --bond-mode active-backup --bond-miimon '100' --bond-slaves 'eth11 eth22' \
       --ip 10.10.10.2 --netmask 255.0.0.0 --gateway 10.10.10.254
${CMD} --input-file=testfiles/eth46.yml --output-file=testfiles/eth46.yml \
       --host=sp1 --set-interface=eth11 \
       --hwaddr 11:11:11:11:11:11 --mtu 1111 \
       --dhcp yes
${CMD} --input-file=testfiles/eth46.yml --output-file=testfiles/eth46.yml \
       --host=sp1 --set-interface=eth22 \
       --hwaddr 22:22:22:22:22:22 --mtu 2222 \
       --dhcp yes
${CMD} --input-file=testfiles/eth46.yml --output-file=testfiles/eth46.yml \
       --host=sp1 --set-interface=vlan1111 \
       --vlan-raw-device bond0 \
       --ip 10.11.11.11 --netmask 255.0.0.0 --gateway 10.11.11.254
${CMD} --input-file=testfiles/eth46.yml --output-file=testfiles/eth46.yml \
       --host=sp1 --set-interface=vlan2222 \
       --vlan-raw-device bond0 \
       --ip 10.22.22.22 --netmask 255.0.0.0 --gateway 10.22.22.254
${CMD} --input-file=testfiles/eth46.yml --output-file=testfiles/eth46.yml \
       --host=sp1 --set-interface=vlan3333 \
       --vlan-raw-device bond0 \
       --ip 10.33.33.33 --netmask 255.0.0.0 --gateway 10.33.33.254
assertEqualFiles testfiles/eth46.yml network-config/eth46.yml

# verify deployment steps
ROLE=sp1; PEER=sp2; DEFAULT_INSTALL_DEV=eth0; INTERNAL_DEV=eth1;

in_counter=1; out_counter=1 # do not hardcode input/output files

${CMD} --host=$ROLE --set-interface=lo --ip=127.0.0.1 --netmask=255.0.0.0 \
       --hwaddr=00:00:00:00:00:00 --ipv6='::1' --type=web_int \
       --input-file=network-config/network_pro.yml --output-file=testfiles/deploy_${out_counter}.yml
((out_counter++))

${CMD} --host=$ROLE --set-interface=lo --shared-ip=none --shared-ipv6=none \
       --input-file=testfiles/deploy_${in_counter}.yml --output-file=testfiles/deploy_${out_counter}.yml
((out_counter++)) ; ((in_counter++))

${CMD} --host=$ROLE --set-interface=$DEFAULT_INSTALL_DEV --ip=203.0.113.42 \
       --netmask=255.255.255.0 --hwaddr=de:ad:be:ef:23:42 --dns=203.0.113.43 --dns=203.0.113.44 \
       --gateway=203.0.113.1 \
       --input-file=testfiles/deploy_${in_counter}.yml --output-file=testfiles/deploy_${out_counter}.yml
((out_counter++)) ; ((in_counter++))

${CMD} --host=$ROLE --set-interface=$INTERNAL_DEV --ip=192.168.255.251 \
       --netmask=255.255.255.248 --hwaddr=de:ad:be:ef:42:23 \
       --input-file=testfiles/deploy_${in_counter}.yml --output-file=testfiles/deploy_${out_counter}.yml
((out_counter++)) ; ((in_counter++))

${CMD} --host=$ROLE --peer=$PEER \
       --input-file=testfiles/deploy_${in_counter}.yml --output-file=testfiles/deploy_${out_counter}.yml
((out_counter++)) ; ((in_counter++))

${CMD} --host=$ROLE --move-from=lo --move-to=$INTERNAL_DEV --type=ha_int \
       --input-file=testfiles/deploy_${in_counter}.yml --output-file=testfiles/deploy_${out_counter}.yml
((out_counter++)) ; ((in_counter++))

${CMD} --host=$PEER --peer=$ROLE \
       --input-file=testfiles/deploy_${in_counter}.yml --output-file=testfiles/deploy_${out_counter}.yml
((out_counter++)) ; ((in_counter++))

${CMD} --host=$PEER --set-interface=lo --shared-ip=none --shared-ipv6=none \
       --input-file=testfiles/deploy_${in_counter}.yml --output-file=testfiles/deploy_${out_counter}.yml
((out_counter++)) ; ((in_counter++))

${CMD} --host=$PEER --set-interface=lo --ipv6='::1' --ip=127.0.0.1 --netmask=255.0.0.0 --hwaddr=00:00:00:00:00:00 \
       --input-file=testfiles/deploy_${in_counter}.yml --output-file=testfiles/deploy_${out_counter}.yml
((out_counter++)) ; ((in_counter++))

${CMD} --host=$PEER --set-interface=eth1 --ip=192.168.255.252 --netmask=255.255.255.248 --type=ha_int \
       --input-file=testfiles/deploy_${in_counter}.yml --output-file=testfiles/deploy_${out_counter}.yml
((out_counter++)) ; ((in_counter++))

${CMD} --host=$PEER --role=proxy --role=lb --role=mgmt \
       --input-file=testfiles/deploy_${in_counter}.yml --output-file=testfiles/deploy_${out_counter}.yml
((out_counter++)) ; ((in_counter++))

${CMD} --host=$PEER --set-interface=lo --type=sip_int --type=web_ext --type=sip_ext \
       --type=rtp_ext --type=ssh_ext --type=mon_ext --type=web_int \
       --input-file=testfiles/deploy_${in_counter}.yml --output-file=testfiles/deployment.yml

assertEqualFiles testfiles/deployment.yml network-config/deployment.yml


# make sure host/interfaces doesn't get cleaned on remove one interface
${CMD} --input-file=network-config/eth42.yml --output-file=testfiles/lo.yml \
       --host=sp1 --remove-interface=eth42
assertEqualFiles testfiles/lo.yml network-config/lo.yml

# TAP output
printf "1..%s\n" "${count}"
cat "$OUTPUT"

bailout

exit $RC
