#!/bin/sh

# apk add blkid parted dosfstools e2fsprogs wget tar
set -eu

# before starting this script next steps must be done replacing $TMPDIR with mountpoint of the nvme linux partition
# and replacing $ROOTDEV with nvme linux partition 

#mkfs.ext4 -qL root "$ROOTDEV"
#mount -t ext4 "$ROOTDEV" "$TMPDIR"
#mkdir -p "$TMPDIR"/boot
#mkdir -p "$TMPDIR"/boot/efi "$TMPDIR"/etc/apk/keys
#mount -t vfat "$BOOTDEV" "$TMPDIR"/boot/efi

if ! mountpoint -q "/mnt"; then
	echo "/mnt is not mounted"
  exit
fi

readonly TMPDIR="/mnt"
readonly APK_KEY="alpine-devel@lists.alpinelinux.org-60ac2099.rsa.pub"
readonly MIRROR="http://dl-cdn.alpinelinux.org/alpine"

kernel="linux-asahi-5.17.rc7.20220318-r0.apk"
wget -O update-vendor-firmware  https://dev.alpinelinux.org/~mps/m1/update-vendor-firmware
#wget https://dev.alpinelinux.org/~mps/m1/find-efi.sh
#wget https://dev.alpinelinux.org/~mps/m1/rmmod.sh

echo "Setup apk keys and repositories"
wget -qP "$TMPDIR"/etc/apk/keys https://alpinelinux.org/keys/"$APK_KEY"
printf "$MIRROR/edge/%s\n" main community testing > "$TMPDIR"/etc/apk/repositories

echo "installing base packages"
apk --allow-untrusted --root "$TMPDIR" --arch aarch64 --initdb add \
	alpine-base alpine-baselayout alpine-conf kmod openrc \
  dbus util-linux blkid chrony \
	sysfsutils ssl_client ca-certificates-bundle alpine-keys \
	ethtool e2fsprogs libudev-zero libudev-zero-helper \
	iwd linux-firmware-none efibootmgr ttf-dejavu \
	nvme-cli agetty terminus-font openresolv tar wget
apk --allow-untrusted --root "$TMPDIR" --arch aarch64 --no-script add linux-asahi
apk --allow-untrusted --root "$TMPDIR" --arch aarch64 --no-script add grub-efi grub-mkfont

echo "Setting up services and inittab"
for rc in boot/bootmisc boot/hostname boot/modules boot/sysctl boot/urandom boot/networking \
	sysinit/devfs sysinit/hwdrivers sysinit/mdev sysinit/modules \
	shutdown/mount-ro shutdown/killprocs \
	default/dbus default/chronyd; do
	ln -s /etc/init.d/"${rc##*/}" "$TMPDIR"/etc/runlevels/"$rc"
done

echo "preparing scripts for setup"
wget -O $TMPDIR/root/update-vendor-firmware  https://dev.alpinelinux.org/~mps/m1/update-vendor-firmware
wget -O $TMPDIR/etc/iwd/main.conf https://dev.alpinelinux.org/~mps/m1/iwd-main.conf

cat > "$TMPDIR"/root/grub-setup.sh<<EOF
#!/bin/sh
grub-install --target=arm64-efi --efi-directory=/boot/efi --removable
grub-mkfont -s 24 -o /boot/grub/fonts/dejavu.pf2 /usr/share/fonts/ttf-dejavu/DejaVuSansMono.ttf
grub-mkconfig  > /boot/grub/grub.cfg
exit
EOF

echo 'GRUB_DISABLE_LINUX_PARTUUID=false' >> "$TMPDIR"/etc/default/grub
echo 'GRUB_CMDLINE_LINUX="rootwait console=tty1"' >> "$TMPDIR"/etc/default/grub
echo 'GRUB_TIMEOUT=10' >> "$TMPDIR"/etc/default/grub
echo 'GRUB_FONT="/boot/grub/fonts/dejavu.pf2"' >> "$TMPDIR"/etc/default/grub
chmod 0744 "$TMPDIR"/root/grub-setup.sh

echo "Settting grub boot loader"
mount -t proc none $TMPDIR/proc
mount -o bind /sys $TMPDIR/sys
mount -o bind /dev $TMPDIR/dev
echo "Run this two commands and after that exit"
echo "grub-install --target=arm64-efi --efi-directory=/boot/efi --removable"
echo "grub-mkconfig  > /boot/grub/grub.cfg"
chroot $TMPDIR /root/grub-setup.sh
#chroot $TMPDIR /bin/ash -l
umount $TMPDIR/dev
umount $TMPDIR/sys
umount $TMPDIR/proc

sed -i 's/^tty1.*/tty1::respawn:\/sbin\/agetty -L 115200 tty1 linux --login-pause --autologin root --noclear/' $TMPDIR/etc/inittab
echo "to install apple wifi firmware you can 'sh update-vendor-firmware'" >> $TMPDIR//etc/motd
echo "and then '/etc/init.d/iwd start' to start wifi" >> $TMPDIR/etc/motd

echo "Finished, cleaning up"

