#!/bin/bash

# a bare-minimal installer for ppc gentoo.

# instructions:
# - download and burn the latest iso from
#   https://distfiles.gentoo.org/releases/ppc/autobuilds/current-install-powerpc-minimal/
# - boot from the iso, then run this script:
#   wget -O - http://leopard.sh/linux/gentoo-ppc.sh | bash

set -e -o pipefail
set -x

GREEN='\033[1;32m'
WHITE='\033[1;37m'
NC='\033[0m'

{ set +x; } 2>/dev/null
echo -e "\n ${GREEN}* ${WHITE}Setting the time.${NC}\n"
set -x

if which chronyd >/dev/null 2>&1 ; then
    chronyd -q
elif which ntpd >/dev/null 2>&1 ; then
    ntpd -q -g
fi

{ set +x; } 2>/dev/null
echo -e "\n ${GREEN}* ${WHITE}Partitioning the disk.${NC}\n"
set -x

mac-fdisk /dev/sda << EOF
i
y

b
2p
c
3p
3p
root
w
y
EOF
sleep 1

{ set +x; } 2>/dev/null
echo -e "\n ${GREEN}* ${WHITE}Formatting the root partition.${NC}\n"
set -x

umount /dev/sda3 || true
mkfs.ext4 /dev/sda3
mkdir -p /mnt/gentoo
mount /dev/sda3 /mnt/gentoo

{ set +x; } 2>/dev/null
echo -e "\n ${GREEN}* ${WHITE}Installing a stage 3 tarball.${NC}\n"
set -x

# determine the latest tarball filename
stage3_tarball=$(wget -O - https://distfiles.gentoo.org/releases/ppc/autobuilds/current-stage3-ppc-openrc/latest-stage3-ppc-openrc.txt | grep '\.tar\.' | awk '{print $1}')

cd /mnt/gentoo
url=https://distfiles.gentoo.org/releases/ppc/autobuilds/current-stage3-ppc-openrc/$stage3_tarball
wget -O - $url | unxz | tar xp --xattrs-include='*.*' --numeric-owner

{ set +x; } 2>/dev/null
echo -e "\n ${GREEN}* ${WHITE}Setting up the boot partition.${NC}\n"
set -x

hformat -l bootstrap /dev/sda2
mkdir -p /mnt/gentoo/boot/NWBB
mount --types hfs /dev/sda2 /mnt/gentoo/boot/NWBB/
grub-install --boot-directory=/mnt/gentoo/boot --macppc-directory=/mnt/gentoo/boot/NWBB /dev/sda2
umount /mnt/gentoo/boot/NWBB/
hmount /dev/sda2
hattrib -t tbxi -c UNIX :System:Library:CoreServices:BootX
hattrib -b :System:Library:CoreServices
humount

{ set +x; } 2>/dev/null
echo -e "\n ${GREEN}* ${WHITE}Copying kernel + modules from livecd.${NC}\n"
set -x

cd /mnt/cdrom/boot
cp -a ppc32 ppc32.igz System-ppc32.map grub /mnt/gentoo/boot/
mkdir -p /mnt/gentoo/lib/modules
cp -a /lib/modules/6.12.21-gentoo-ppc32-ppc /mnt/gentoo/lib/modules

{ set +x; } 2>/dev/null
echo -e "\n ${GREEN}* ${WHITE}Creating a grub config.${NC}\n"
set -x

cat > /mnt/gentoo/boot/grub/grub.cfg << EOF
set default=0
set gfxpayload=keep
set timeout=3
insmod all_video

menuentry 'ppc32' --class gnu-linux --class os {
	linux /boot/ppc32 root=/dev/sda4 ro
	initrd /boot/ppc32.igz
}
EOF

{ set +x; } 2>/dev/null
echo -e "\n ${GREEN}* ${WHITE}Chrooting.${NC}\n"
set -x

mount --types proc /proc /mnt/gentoo/proc
mount --rbind /sys /mnt/gentoo/sys
mount --make-rslave /mnt/gentoo/sys
mount --rbind /dev /mnt/gentoo/dev
mount --make-rslave /mnt/gentoo/dev
mount --bind /run /mnt/gentoo/run
mount --make-slave /mnt/gentoo/run
chroot /mnt/gentoo /bin/bash << 'ENDCHROOT'
set -e -o pipefail
source /etc/profile
set -x

GREEN='\033[1;32m'
WHITE='\033[1;37m'
NC='\033[0m'

{ set +x; } 2>/dev/null
echo -e "\n ${GREEN}* ${WHITE}Setting the root password to 'root'.${NC}\n"
sleep 3
set -x

passwd << EOF
root
root
EOF

{ set +x; } 2>/dev/null
echo -e "\n ${GREEN}* ${WHITE}Configuring sshd.${NC}\n"
set -x

rc-update add sshd default

cat >> /etc/ssh/sshd_config << EOF
PermitRootLogin yes
EOF

ENDCHROOT

{ set +x; } 2>/dev/null
echo -e "\n ${GREEN}* ${WHITE}Configuring portage.${NC}\n"
set -x

cat >> /mnt/gentoo/etc/portage/make.conf << EOF

FEATURES="buildpkg getbinpkg"
EOF

{ set +x; } 2>/dev/null
echo -e "\n ${GREEN}* ${WHITE}Rebooting.${NC}\n"
set -x

cd /
reboot
