#!/bin/sh

gnop load
gunion load

test() {
	uniondev=$(gunion create -v $d1 $d2 2>&1 | awk '{ print $3 }')
	if ! gunion list $uniondev >/dev/null
	then
	  echo Error creating gunion device
	  exit 1
	fi

	mdir=$(mktemp -d)
	newfs /dev/$uniondev
	mount /dev/$uniondev $mdir
	tar -cf - /boot /bin /sbin | tar -C $mdir -xf -
	umount $mdir
	fsck /dev/$uniondev
}

if ! diskinfo "$1"
then
  echo Not a disk
  exit 1
fi

if ! diskinfo "$2"
then
  echo Not a disk
  exit 1
fi

if [ $1 = $2 ]
then
  echo $1 and are the same device
  exit 1
fi

s1=$(diskinfo $1 | awk '{print $3}')
s2=$(diskinfo $2 | awk '{print $3}')

if [ $s1 != $s2 ]
then
  echo Devices should have equal size
  exit 1
fi

d1=$1
d2=$2

test
