#!/bin/sh

if [ `uname` != "FreeBSD" ]; then
	echo `basename $0`: this script have to be started under FreeBSD.
	return 1
fi

GEOMS=/root/geoms
SMART=/tmp/smartctl-current

if ! [ -f ${DEVS} ]; then
	echo "Names of target devices (e.g. da3, ada1 etc.) have to be stored in \"${GEOMS}\" file, one device per line"
	return 1
fi

for GEOM in `cat ${GEOMS}`; do
	DEV=/dev/${GEOM}
	if [ `echo ${DEV} | cut -c 1` == '#' ]; then
		echo Skipping device $DEV
		continue
	fi
	if [ -b "${DEV}p1" ]; then
		echo Device $DEV had already prepared.
		continue
	fi
	(smartctl -a ${DEV} > ${SMART}) || return 1
	cat ${SMART} | grep 'Model Family'
	SLABEL=`cat ${SMART} | grep 'Serial Number' | rev | cut -c 1-4 | rev`
	echo $DEV: $SLABEL
	gpart create -s gpt ${GEOM} || return 1
	gpart add -a 2M -s 1G -t linux-data ${GEOM}
	gpart add -a 2M -t freebsd-zfs -l 2tb${SLABEL} ${GEOM}
done
