#!/usr/bin/env bash

LFS=/home/lomig/dev/lfs
sudo umount -Rl $LFS/* 
sudo rm -rf $LFS/*
mkdir -pv $LFS/{dev/{pts,shm},proc,sys,run}

podman build -t build-lfs:0.1 .

sudo mount -v --bind /dev $LFS/dev
sudo mount -vt devpts devpts -o gid=5,mode=0620 $LFS/dev/pts
sudo mount -vt proc proc $LFS/proc
sudo mount -vt sysfs sysfs $LFS/sys
sudo mount -vt tmpfs tmpfs $LFS/run
if [ -h $LFS/dev/shm ]; then
  install -v -d -m 1777 $LFS$(realpath /dev/shm)
else
  sudo mount -vt tmpfs -o nosuid,nodev tmpfs $LFS/dev/shm
fi
podman run --rm -it --privileged -v /home/lomig/dev/lfs:/mnt/lfs build-lfs:0.1 /bin/bash /lfs-12.2.sh

sudo cp /etc/resolv.conf $LFS/etc/resolv.conf
sudo mkdir -p $LFS/etc/ssl/certs
sudo wget https://curl.se/ca/cacert.pem -O $LFS/etc/ssl/certs/ca-certificates.crt
sudo cp ../Govel/govel $LFS/usr/bin/govel
sudo chroot "$LFS" /usr/bin/env -i   \
    HOME=/root                  \
    TERM="$TERM"                \
    PS1='(lfs chroot) \u:\w\$ ' \
    PATH=/usr/bin:/usr/sbin     \
    MAKEFLAGS="-j$(nproc)"      \
    TESTSUITEFLAGS="-j$(nproc)" \
    /bin/bash /root/post-chroot
umount -Rl $LFS/* >> /dev/null 2>&1
