#!/bin/sh

test $# -eq 2 -o $# -eq 3 \
|| { echo "usage: $0 imagefile releasedir [kernel]" >&2; exit 1; }

image=$1
test -n "$image" -a -f "$image" \
|| { echo "'$image' is not a file" >&2; exit 1; }

release=$2
test -n "$release" -a -d "$release" -a -d "$release/amiga/binary" \
|| { echo "'$release' is not a release directory" >&2; exit 1; }

kernel=${3:-$release/amiga/binary/kernel/netbsd-GENERIC.gz}
test -n "$kernel" -a -f "$kernel" \
|| { echo "'$kernel' is not a file" >&2; exit 1; }

tmpdir=$(mktemp -d /tmp/makeimage-$LOGNAME) || exit 1
trap "rmdir $tmpdir; echo ABORT rc=$? >&2" EXIT
set -ex

###################################################

vendor="NetBSD"
product="Hardfile"
label="so heiss ich"

setdir=$release/amiga/binary/sets

hostname=dummy
timezone="Europe/Berlin"

rootdev=wd0
netdev=le0
hostip=10.28.5.99
netmask=255.255.255.0

###################################################

rdbedit=$HOME/rdbedit/rdbedit

dev=$(vnconfig -l | awk -F: '
	NR==1 { n=-1 }
	NF==2 && $2 == " not in use" { print $1; n=-2; exit }
	NF==2 && $1 ~ /^vnd[0-9][0-9]*$/ { x=substr($1,4); n=(x>n)?x:n}
	END { if (n>=-1) print "vnd" (n+1) }
')

echo "Using device $dev"

mkdir -m 700 $tmpdir/mnt || exit 1
mnt=$tmpdir/mnt

$rdbedit -F -i -e -s 2 "$image"  << EOF
r
v$vendor
p$product
q
c0 4000
p3
fbootable
nroot
tNBR\007
o16
p10
x
q
c0 4000
p4
nswap
tNBS\001
o16
p0
x
q
c0 0
p5
nother
x
q
.
q
Y
EOF

$rdbedit -F -t "$image" \
| disklabel -w -F -f /dev/fd/0 "$image" hardfile "$product"

sudo umount $mnt/data || true
sudo umount $mnt || true
sudo vnconfig -u $dev || true

sudo vnconfig $dev "$image"
sudo newfs -B be -O1 NAME="$product"/a
sudo newfs -B be -O2 NAME="$product"/e
sudo mount -o async NAME="$product"/a $mnt
sudo mkdir $mnt/data || true
sudo mount -o async NAME="$product"/e $mnt/data

for s in base comp etc man misc modules tests text; do
	if [ -f "$setdir/$s.tgz" ]; then
		sudo tar -C $mnt -xzpf $setdir/$s.tgz
	fi
done

sudo mkdir $mnt/proc || true

qproduct=$(echo "$product" | sed -e 's#[[:space:]\\]#\\&#g')

case $usewedges in
host)
	# discover label of x86 host system
	parta=NAME=$qproduct/a
	partb=NAME=$qproduct/b
	partd=NAME=$qproduct/e
	;;
native)
	# discover native label
	parta=NAME=$qproduct/a
	partb=NAME=$qproduct/b
	partd=NAME=$qproduct/d
	;;
"")
	# no wedges
	parta=/dev/${rootdev}a
	partb=/dev/${rootdev}b
	partd=/dev/${rootdev}d
	;;
esac

sudo ed <<EOF
a
# NetBSD /etc/fstab
# See /usr/share/examples/fstab/ for more examples.
$parta                  /         ffs     rw               1 1
$partb                  none      swap    sw               0 0
$partd                  /data     ffs     rw,log           1 2
tmpfs                   /tmp      tmpfs   rw,-s=256m
kernfs                  /kern     kernfs  rw
procfs                  /proc     procfs  rw
ptyfs                   /dev/pts  ptyfs   hidden,rw
tmpfs                   /var/shm  tmpfs   hidden,rw,-m1777,-sram%25
.
wq $mnt/etc/fstab
EOF
sudo ls -ld $mnt/etc/fstab || true

sudo ed $mnt/etc/rc.conf <<EOF
/rc_configured=/
s/=.*/=YES/
g/hostname=/d
\$a
hostname=$hostname
auto_ifconfig=YES
sshd=YES
.
wq
EOF

sudo ed $mnt/etc/ssh/sshd_config <<EOF
/^#PermitRootLogin /
s/^#PermitRootLogin .*/PermitRootLogin yes/
wq
EOF

sudo ed <<EOF
a
inet $hostip netmask $netmask
up
.
wq $mnt/etc/ifconfig.$netdev
EOF

sudo sh -c "cd $mnt/dev && ./MAKEDEV all"

sudo ln -fs /usr/share/zoneinfo/$timezone $mnt/etc/localtime

case $kernel in
*.gz)
	sudo cp $kernel $mnt/netbsd.gz
	sudo gzip -d $mnt/netbsd.gz
	;;
*)
	sudo cp $kernel $mnt/netbsd
	;;
esac
sudo cp $mnt/usr/mdec/boot.amiga $mnt/.
sudo disklabel -W NAME="$product"/a
sudo installboot -v -m amiga -o command="netbsd -SCc 4000" NAME="$product"/a $mnt/usr/mdec/bootxx_ffs
sudo disklabel -N NAME="$product"/a

sudo mkdir $mnt/kern || true
sudo umount $mnt/data
sudo umount $mnt

rmdir $mnt
rmdir $tmpdir

sudo vnconfig -u $dev

set +ex
trap EXIT
