#!/bin/bash
# Running from ramdisk with snapshot and similar to Debian Live 10.6
export LIVE_BUILD=/home/roland/git/live-build
export SOURCE_DATE_EPOCH=1601116968
cd /dev/shm
mkdir live
cd live
mount /dev/shm -odev,exec,remount

lb config --apt-http-proxy http://localhost:3142 --parent-mirror-bootstrap http://localhost:3142/snapshot.debian.org/archive/debian/20200926T104248Z --parent-mirror-binary http://deb.debian.org/debian/ --security false --updates false --apt-options "--yes -o Acquire::Check-Valid-Until=false" --distribution buster --debian-installer live --loadlin false --cache-packages false

echo "live-task-standard acpid" > config/package-lists/desktop.list.chroot
cat > config/hooks/normal/1000-more-similar-to-live-wrapper.hook.binary << EOF
#!/bin/sh

set -e

# live-wrapper: d-i
# lb build: install
mv install d-i
EOF
cat > config/hooks/normal/1001-reproducible.hook.chroot << EOF
#!/bin/sh

set -e
set -x

# pwconv from package shadow adds a day number in the field 'last_changed'
mv etc/shadow shadow_orig
awk \
	-v LOCAL_DATE=$(date +%s) \
	-v SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH} \
	'{ c = split(\$0, a, ":"); if (a[3]==sprintf("%d",LOCAL_DATE/24/60/60)) a[3]=sprintf("%d",SOURCE_DATE_EPOCH/24/60/60); for (i=1;i<c;i++) printf("%s:", a[i]); printf "%s\n", a[c] }' shadow_orig > etc/shadow
chown root:shadow etc/shadow
chmod u=rw,g=r,o= etc/shadow
rm shadow_orig

# Remove ldconfig cache, which is not reproducible
rm -f var/cache/ldconfig/aux-cache

# The filenames in the fontconfig cache folder are uuids
rm -fr var/cache/fontconfig/*

# The content of the .uuid files changes every time
find usr/share/fonts -name .uuid -delete || true
find usr/local/share/fonts -name .uuid -delete || true
EOF
