#!/bin/sh
# Copyleft by Alexander Titaev tit@irk.ru

HOSTNAME=$1
PASSWD=$2
DISK=$3
TPL=$4

if [ -z ${TPL} ]; then
cat << EOF
 use:
 	'./hetzner.sh hostname password [mir|jour|single] tpl'
 	where 
 	hostname - hostname for this server
 	password - root password
	'[mir|jour|any]'
	mir - build gmirror
	jour - build gmirror+gjournal
	single - use only first hdd
	tpl - DS template like
		FreeBSD-8.2-amd64
		FreeBSD-8.2-amd64-ispmgr
		FreeBSD-8.2-i386
		FreeBSD-8.2-i386-ispmgr
EOF
	exit
fi

/bin/mkdir /mfs
/sbin/mdmfs -s 600M md /mfs

if [ $? = 1 ]; then
	echo cant create memory disk
	exit
fi
cd /mfs

DISKS=`/sbin/sysctl -n kern.disks`

echo $DISKS | while read DISK
do
	gmirror clear -v ${DISK}
#	gpart destroy -F ${DISK}
	dd if=/dev/zero of=/dev/${DISK} count=1 bs=1024
done

HD=`echo $DISKS | xargs -n1 echo | grep ad | sort -td -k2 -n | head -1`
if [ -z ${HD} ]; then
	HD=`echo $DISKS | xargs -n1 echo | grep da | sort -ta -k2 -n | head -1`
	if [ -z ${HD} ]; then
		echo cant find any ad or da disks
		exit
	fi
fi

if [ ${DISK} = "mir" ] || [ ${DISK} = "jour" ]; then
	/sbin/gmirror load
	if [ $? = 1 ]; then
		echo cant load module geom_mirror
		exit
	fi
	/sbin/gmirror label -v -b load gm0 ${DISKS}
	/sbin/fdisk -I /dev/mirror/gm0
else
	/sbin/fdisk -I /dev/${HD}
fi

if [ ${DISK} = "jour" ]; then
	JOUR_SIZE=`/sbin/sysctl -n hw.physmem | awk '{ print int($1*3.5/1024/1024/1024)}'`
	/bin/cat > /mfs/labels << EOF
a:      10G       *       4.2BSD
b:      4G      *       swap
c:      *       *       unused
d:	*	*	4.2BSD
e:	${JOUR_SIZE}G	*	swap
EOF
else
	/bin/cat > /mfs/labels << EOF
a:      10G       *       4.2BSD
b:      4G      *       swap
c:      *       *       unused
d:	*	*	4.2BSD
EOF
fi

echo "/sbin/newfs create"
if [ ${DISK} = "mir" ]; then
	/sbin/bsdlabel -R -B /dev/mirror/gm0s1 /mfs/labels
	/bin/sleep 5
	/sbin/newfs /dev/mirror/gm0s1a
	/sbin/newfs /dev/mirror/gm0s1d
	/sbin/mount /dev/mirror/gm0s1a /mnt
elif [ ${DISK} = "jour" ]; then
        /sbin/bsdlabel -R -B /dev/mirror/gm0s1 /mfs/labels
	/sbin/gjournal clear -v /dev/mirror/gm0s1e /dev/mirror/gm0s1d

	/sbin/kldload geom_journal.ko
	if [ $? = 1 ]; then
		echo cant load module geom_journal
		exit
	fi

	/sbin/gjournal label /dev/mirror/gm0s1d /dev/mirror/gm0s1e
	/bin/sleep 5
        /sbin/newfs  /dev/mirror/gm0s1a
        /sbin/newfs -J /dev/mirror/gm0s1d.journal
        /sbin/mount -o async /dev/mirror/gm0s1a /mnt
else
	/sbin/bsdlabel -R -B /dev/${HD}s1 /mfs/labels
	/bin/sleep 5
	/sbin/newfs /dev/${HD}s1a
	/sbin/newfs /dev/${HD}s1d
	/sbin/mount /dev/${HD}s1a /mnt
fi

if [ $? = 1 ]; then
	echo cant mount !
	exit
fi

echo download template
/usr/bin/ftp http://ru.download.ispsystem.com/DStemplate/${TPL}/disk.tar.bz2
if [ $? != 0 ]; then
	echo cant download template ${TPL}
	exit
fi

echo extract data from template
/usr/bin/tar -jpxf disk.tar.bz2 -C /mnt

GW=`route -n get default | grep gateway | awk '{ print $2 }'`
INT=`route -n get default | grep interface | awk '{ print $2 }'`
IP=`netstat -rn | grep -w UHS | grep -v '::'  | awk '{ print $1 }'`
MASK=`netstat -rn | grep -w U | grep -v '::' | awk '{ print $1 }' | awk -F\/ '{ print $2 }'`

echo $INT | grep -qE 're[0-9]'
if [ $? = 0 ]; then
	MEDIA='media 100baseTX mediaopt full-duplex,flag0'
fi

echo change system settings
/bin/cat >> /mnt/etc/rc.conf << EOF
defaultrouter="${GW}"
hostname="${HOSTNAME}"
ifconfig_${INT}="inet ${IP}/${MASK}"
sshd_enable="YES"
EOF

mkdir /mnt/vs

if [ ${DISK} = "jour" ]; then
	echo 'fsck_y_enable="YES"' >> /mnt/etc/rc.conf
	/bin/cat > /mnt/boot/loader.conf << EOF
geom_mirror_load="YES"
geom_journal_load="YES"
ahci_load="YES"
EOF
	/bin/cat > /mnt/etc/fstab << EOF
# Device                Mountpoint      FStype  Options         Dump    Pass#
/dev/mirror/gm0s1b             none            swap    sw              0       0
/dev/mirror/gm0s1a	/               ufs    rw	1       1
/dev/mirror/gm0s1d.journal      /vs               ufs    async,rw              1       1
EOF
elif [ ${DISK} = "mir" ]; then
	/bin/cat > /mnt/boot/loader.conf << EOF
geom_mirror_load="YES"
ahci_load="YES"
EOF
	/bin/cat > /mnt/etc/fstab << EOF
# Device                Mountpoint      FStype  Options         Dump    Pass#
/dev/mirror/gm0s1b	none            swap    sw              0       0
/dev/mirror/gm0s1a	/               ufs     rw              1       1
/dev/mirror/gm0s1d	/vs               ufs     rw              1       1
EOF
else
	/bin/cat > /mnt/etc/fstab << EOF
# Device                Mountpoint      FStype  Options         Dump    Pass#
/dev/${HD}s1b	none            swap    sw              0       0
/dev/${HD}s1a	/               ufs     rw              1       1
/dev/${HD}s1d	/vs               ufs     rw              1       1
EOF
fi

echo change root password
echo ${PASSWD} | /usr/sbin/pw -V /mnt/etc/ usermod root -h 0

/bin/cat > /mnt/etc/resolv.conf << EOF
nameserver 127.0.0.1
nameserver 188.120.247.2
nameserver 82.146.59.250
EOF

/sbin/umount /mnt
echo Complete, reboot system.
