IMGSIZE="2G"
IMGNAME="hda.raw"
GNUBASE="/home/user/hax/my/hurd/devel/gnu/install"

if [[ -f $IMGNAME ]]; then
	rm $IMGNAME
fi

LOOPDEV=$(losetup -f)
echo "[+] Find free loop device: ${LOOPDEV}"
truncate $IMGNAME -s $IMGSIZE
echo "[+] Created ${IMGNAME} with size ${IMGSIZE}"
losetup $LOOPDEV $IMGNAME
echo "[+] Attached $IMGNAME with $LOOPDEV"
printf "o\nn\np\n\n\n+500M\nt\n82\nnn\np\n\n\n\nw\n" | fdisk $LOOPDEV
#printf "o\nn\np\n\n\n\nt\n83\nw\n" | fdisk $LOOPDEV >/dev/null
echo "[+] Created partition"
#exit
losetup -d $LOOPDEV
echo "[+] Detached $LOOPDEV"
LOOPDEV=$(losetup -f)
echo "[+] Find free loop device: ${LOOPDEV}"
losetup -P $LOOPDEV $IMGNAME
echo "[+] Attached $IMGNAME with $LOOPDEV"
mkfs.ext2 -o hurd -I 128 ${LOOPDEV}p2 >/dev/null
echo "[+] Created ext2 filesystem"
#mkfs.ext2 -o hurd -m 1 -v ${LOOPDEV}p1
echo "[+] Changed fs/partition owner to 'hurd'"
mount ${LOOPDEV}p2 /mnt
mkdir -p /mnt/data
mkdir -p /mnt/tmp
chmod 01777 /mnt/tmp
mkdir -p /mnt/etc
echo "Hurd84" > /mnt/etc/hostname
cat <<EOF>/mnt/etc/group
root:x:0:
daemon:x:1:
EOF

mkdir -p /mnt/root
cat <<EOF>/mnt/etc/passwd
root::0:0:Lord of the Files:/root:/bin/bash
EOF

cat <<EOF>/mnt/root/.bashrc
export C_INCLUDE_PATH="/include"
export TERM=mach-gnu-color
EOF

cat <<EOF>/mnt/root/.bash_profile
	. ~/.bashrc
EOF

cat <<EOF>/mnt/etc/shadow
root:*:0:0:99999:7:::
EOF

echo "#device          mount point    filesystem type  options  dump pass" > /mnt/etc/fstab
echo "/dev/hd0s1      none           swap             sw       0    0" >> /mnt/etc/fstab 
echo "/dev/hd0s2      /              ext2fs           rw       1    1" >> /mnt/etc/fstab
#mkdir -p /mnt/{etc,boot,dev,usr,hurd,servers,lib,libexec,proc,sbin,bin,var,root,tools}
cp ${GNUBASE}/../../src/hurd/release/SETUP /mnt
cp -r ${GNUBASE}/boot /mnt
cp -r ${GNUBASE}/hurd /mnt
cp -r /mnt/hurd /mnt/servers
cp -r ${GNUBASE}/lib /mnt
cp -r ${GNUBASE}/include /mnt
cp -r ${GNUBASE}/../libexec /mnt
mkdir -p /mnt/bin/
cp -r ${GNUBASE}/bin /mnt
mkdir -p /mnt/dev/
cp ${GNUBASE}/../dash /mnt/bin/sh
cp ${GNUBASE}/../../gnu_things/bash /mnt/bin/bash
chmod +x /mnt/libexec/runsystem
cp /home/user/hax/my/hurd/devel/gnu/install/bin/ld.so /mnt/lib/
cp -r /home/user/hax/my/hurd/devel/gnu_things/install/bin /mnt/
cp -r /home/user/hax/my/hurd/devel/gnu_things/install/lib /mnt/
cp -r /home/user/hax/my/hurd/devel/gnu_things/install/libexec /mnt/
cp -r /home/user/hax/my/hurd/devel/gnu_things/install/sbin /mnt/
cp -r /home/user/hax/my/hurd/devel/gnu_things/install/share /mnt/
cp /home/user/hax/my/hurd/devel/src/hurd/config/ttys /mnt/etc/ttys_
touch /mnt/sbin/syslogd; chmod +x /mnt/sbin/syslogd
touch /mnt/sbin/inetd; chmod +x /mnt/sbin/inetd
mkdir -p /mnt/servers/socket
mkdir -p /mnt/etc
mkdir -p /mnt/var/run
mkdir -p /mnt/var/logins
mkdir -p /mnt/boot/grub
cp grub.cfg /mnt/boot/grub
grub-install --target=i386-pc --recheck --directory=/gnu/store/8difbvqzpa6552ab37m7q1w5i09b3llv-grub-2.06/lib/grub/i386-pc/ --boot-directory=/mnt/boot $LOOPDEV
umount /mnt
losetup -d $LOOPDEV
