#!/bin/sh
function createmenu
{
export menudesc=`stty -a|grep rows|cut -f 2-6 -d \;|cut -f 3,5,8 -d \ |tr \; \ |sed s/"  "/" "/|sed s/"  "/" "/`
cat $1|tail -n 1 >/tmp/itemlist
export numitems=`cat /tmp/itemlist|wc -l`
export linecounter=1
while true;do
cat /tmp/itemlist|head -n $linecounter|tail -n 1 >>/tmp/items
export linecounter=$(($linecounter+1))
if [ $linecounter -gt $numitems ];then
break
else
continue
fi
done
echo whiptail --menu --nocancel `cat $1|head -n 1` `echo -n $menudesc` `cat $1|tail -n 1`>/tmp/menu
chmod 755 /tmp/menu
/tmp/menu 2>/tmp/choice
export choice=`cat /tmp/choice|cut -f 2 -d \;`
cat /tmp/itemlist|sed s/"\"\ /\\n/g"|sed "s/\"//g" > /tmp/items
export testnum=`cat /tmp/items|grep -nw $choice |cut -f 1 -d \:`
export itemname=`cat /tmp/items|head -n $(($testnum+1))|tail -n 1`
rm /tmp/choice /tmp/menu /tmp/itemlist /tmp/items /tmp/dynmenu /tmp/menutitle
clear
}
function createdynamicmenu
{
$@>/tmp/itemlist
export numitems=`cat /tmp/itemlist|wc -l`
export counter=1
if [ -f /tmp/menutitle ];then
export title=`cat /tmp/menutitle|head -n 1`
else
export title="$numitems items available"
fi
echo \"$title\" > /tmp/dynmenu
while true;do
export ltr=`cat /tmp/itemlist|head -n $counter|tail -n 1|cut -c 1`
export item=`cat /tmp/itemlist|head -n $counter|tail -n 1`
echo -n \" >> /tmp/dynmenu
if [ $ltr = \" ];then
export ltr="|"
fi
echo -n $ltr\;$counter >> /tmp/dynmenu
echo -n \"\  >>/tmp/dynmenu
echo -n \" >> /tmp/dynmenu
if echo $item|grep -q \";then
export item=`echo $item|sed "s/\"/\|/g"`
fi
echo -n $item >> /tmp/dynmenu
echo -n \"\  >>/tmp/dynmenu
export counter=$(($counter+1))
if [ $counter -gt $numitems ];then
break
else
continue
fi
done
createmenu /tmp/dynmenu
}
pkgs="@world sys-kernel/gentoo-sources sys-apps/pciutils sys-kernel/genkernel sys-kernel/linux-firmware net-misc/netifrc  sys-apps/pcmciautils app-admin/sysklogd sys-process/cronie sys-apps/mlocate net-misc/dhcpcd net-dialup/ppp net-wireless/iw net-wireless/wpa_supplicant sys-boot/grub:2 sys-boot/os-prober sys-boot/lilo sys-boot/efibootmgr media-sound/alsa-utils app-accessibility/espeakup net-misc/networkmanager app-admin/sudo media-libs/alsa-lib sys-fs/multipath-tools app-editors/vim www-client/lynx"
svcs=`echo -n "NetworkManager alsasound espeakup modules sysklogd"`
cores=$(($(lscpu -p|tail -n 1|cut -f 1 -d ,)+2))
export disk=$1
export arch=$2
case "$arch" in
i686)
darch=x86
farch=i686
karch=x86
;;
amd64)
darch=amd64
farch=amd64
karch=x86_64
;;
arm)
darch=arm
farch=arm
;;
*)
darch=$arch
farch=$arch
;;
esac
baseurl=distfiles.gentoo.org/releases/$darch/autobuilds
links -dump $baseurl/latest-stage3.txt|grep stage3|grep .tar|cut -f 4 -d \  |grep .tar > /tmp/stage3menu
echo "select a stage3 preset" > /tmp/menutitle
createdynamicmenu cat /tmp/stage3menu
dlfile=`cat /tmp/stage3menu|head -n $choice|tail -n 1`
file=`echo $dlfile|sed "s|\/|\n|g"|tail -n 1`
url=$baseurl/$dlfile
custpkgs=`echo -n espeak grub networkmanager sudo wpa_supplicant`
export disklayout="  -o -n 1:2048:4096:EF02 -t 1:EF02 -c 1:BIOS  -n 2:6144:1030143:EF00 -t 2:EF00 -c 2:EFI  -N 3 -t 3:8300 -c 3:linux  "
echo sgdisk $disklayout $disk >> /tmp/part
chmod 755 /tmp/part
/tmp/part
rm /tmp/part
partprobe $disk
sleep 1.5
if [ -z $boot ];then
if [ -e /dev/disk/by-partlabel/EFI ];then
export boot="/dev/disk/by-partlabel/EFI"
else
clear
echo "Your EFI System Partition which will hold the kernel and bootloader cannot automatically be detected. Please select it from the following list."
read var
export entries=`lsblk -nro name,size $disk|wc -l`
lsblk -nro name,size $disk|head -n $entries|tail -n $(($entries-1)) > /tmp/vols
createdynamicmenu cat /tmp/vols
export bootdev=`echo $itemname|cut -f 2 -d \"|cut -f 1 -d \  `
export boot=/dev/$bootdev
sgdisk $disk -c=$choice:EFI
fi
fi
if [ -z $root ];then
if [ -e /dev/disk/by-partlabel/linux ];then
export root="/dev/disk/by-partlabel/linux"
else
clear
echo "Your root device cannot automatically be detected. Please select it from the following list."
read var
export entries=`lsblk -nro name,size $disk|wc -l`
lsblk -nro name,size $disk|head -n $entries|tail -n $(($entries-1)) > /tmp/vols
createdynamicmenu cat /tmp/vols
export rootdev=`echo $itemname|cut -f 2 -d \"|cut -f 1 -d \  `
export root=/dev/$rootdev
sgdisk $disk -c=$choice:linux
fi
fi
clear
mkfs.vfat -n EFI $boot
echo y|mkfs.ext4 -q -L root $root
mount $root /mnt/gentoo
mkdir -p /mnt/gentoo/boot/EFI
mount $boot /mnt/gentoo/boot
cd /mnt/gentoo
while true;do
if curl -LO $url;then
tar -xJpf $file --xattrs-include='*.*' --numeric-owner
rm $file
break
else
continue
fi
done
echo MAKEOPTS=\"-j$(($cores+1))\">>etc/portage/make.conf
while true;do
if mirrorselect -s 5 --deep -t 1 -o >> etc/portage/make.conf;then
break
else
continue
fi
done
echo "USE=\"accessibility alsa hardened networkmanager\"" >> etc/portage/make.conf
echo "GRUB_PLATFORMS=\"coreboot efi-32 efi-64 emu ieee1275 multiboot qemu pc xen xen-32 xen-pvh\"" >> etc/portage/make.conf
mkdir -p etc/portage/package.use
for p in `echo $custpkgs`;do
case "$p" in
espeak)
echo -n "app-accessibility/espeak portaudio" > etc/portage/package.use/espeak
;;
grub)
echo -n "sys-boot/grub device-mapper doc efiemu fonts mount nls sdl test themes truetype" > etc/portage/package.use/grub
;;
networkmanager)
echo -n "net-misc/networkmanager -iwd,wifi" > etc/portage/package.use/networkmanager
;;
sudo)
echo -n "app-admin/sudo offensive" > etc/portage/package.use/sudo
;;
wpa_supplicant)
echo -n "net-wireless/wpa_supplicant dbus" > etc/portage/package.use/wpa_supplicant
;;
esac
done
mkdir -p etc/portage/repos.conf
cp usr/share/portage/config/repos.conf  etc/portage/repos.conf/
cp -L /etc/resolv.conf etc
chroot . /bin/mount -t devtmpfs /dev /dev
chroot . /bin/mount -o gid=5 -t devpts /dev/pts /dev/pts
chroot . /bin/mount -o mode=1777 -t tmpfs /dev/shm /dev/shm
chroot . /bin/mount -t proc /proc /proc
chroot . /bin/mount -t sysfs /sys /sys
while true;do
if chroot . /bin/bash -c "source /etc/profile;env-update;emerge-webrsync -q";then
break
else
continue
fi
done
chroot . /bin/bash -c "source /etc/profile;env-update;while true;do if emerge --deep --newuse --update --verbose --autounmask-write $pkgs;then break;else etc-update --automode -5;continue;fi;done"
for s in `echo $svcs`;do
if [ -e etc/init.d/$s ];then
chroot . /bin/bash -c "source /etc/profile;env-update;rc-update add $s default"
fi
done
chroot . /bin/bash -c "source /etc/profile;env-update;grub-install $disk;grub-install --target x86_64-efi --efi-directory /boot --no-nvram --removable;grub-install --target i386-efi --efi-directory /boot --no-nvram --removable"
chroot . /bin/bash -c "source /etc/profile;env-update;cp /usr/share/genkernel/arch/$karch/generated-config /usr/share/genkernel/arch/$karch/generated-config.orig;sed -i \"s|# CONFIG_SPEAKUP is not set|CONFIG_SPEAKUP=m\nCONFIG_SPEAKUP_SYNTH_ACNTSA=m\nCONFIG_SPEAKUP_SYNTH_APOLLO=m\nCONFIG_SPEAKUP_SYNTH_AUDPTR=m\nCONFIG_SPEAKUP_SYNTH_BNS=m\nCONFIG_SPEAKUP_SYNTH_DECTLK=m\nCONFIG_SPEAKUP_SYNTH_DECEXT=m\nCONFIG_SPEAKUP_SYNTH_LTLK=m\nCONFIG_SPEAKUP_SYNTH_SOFT=m\nCONFIG_SPEAKUP_SYNTH_SPKOUT=m\nCONFIG_SPEAKUP_SYNTH_TXPRT=m\nCONFIG_SPEAKUP_SYNTH_DUMMY=m|g\" /usr/share/genkernel/arch/$karch/generated-config;genkernel --save-config --bcache --hyperv --virtio --oldconfig --install --ramdisk-modules --all-ramdisk-modules --lvm --mdadm --nfs --dmraid --e2fsprogs --xfsprogs --btrfs  --iscsi --luks --gpg --b2sum --busybox --mrproper all"
chroot . /bin/bash -c "source /etc/profile;env-update;grub-mkconfig -o /boot/grub/grub.cfg"
chroot . /bin/bash -c "source /etc/profile;env-update;sed -i \"s|# %wheel ALL=(ALL) ALL|%wheel ALL=(ALL) ALL|g\" /etc/sudoers"
chroot . /bin/bash -c "source /etc/profile;env-update;mkdir -p /etc/modules-load.d;echo speakup_soft > /etc/modules-load.d/speakup_soft.conf"
cat >> etc/fstab<<EOF
/dev/disk/by-label/root / ext4 rw 0 0
/dev/disk/by-label/EFI /boot vfat rw,fmask=077,dmask=077 0 0
EOF
