#!/bin/bash
# Creates new ramdisk image from ./initrd/* and puts it in disk/initrd.gz
sync
filename=vpn-`cat VERSION`.img
if [ -d initrd ] && [ -d disk ] && [ -d mnt ]; then
	echo -n creating new blank disk image..
	dd if=/dev/zero of=$filename bs=1k count=1440 > /dev/null 2>/dev/null
	echo done

	echo -n formatting blank disk image..
	mkfs.msdos $filename > /dev/null
	echo done

	echo -n mounting disk image..
	mount -o loop $filename mnt
	mount | grep loop | sed s/^.*type//

	echo -n copying root filesystem over..  
	cp -a disk/* mnt
	# Intermittently crashes while doing copy.
	sync
	echo done
	
	echo -n checking disk usage..
	size=`du -s mnt | cut -f1`
	echo $size kilobytes

	echo -n unmounting disk image..
	umount mnt; mount | grep loop && echo existing loops detected.
	echo done

	echo -n syslinux -sf $filename..
	syslinux -sf $filename
	echo done

else
	echo Directories missing.  mnt, initrd, disk.  Not in correct directory?
	exit 0
fi
