#!/bin/ash
Arch=armv7
Name=$Arch
chrootDir=/chroot/$Name 

#Setup core
apk -X http://192.168.1.3/alpine/v3.18/main -U --allow-untrusted -p ${chrootDir} --arch ${Arch} --initdb add alpine-base git doas doas-sudo-shim alpine-sdk

#Create required device nodes
mknod -m 666 ${chrootDir}/dev/full c 1 7
mknod -m 666 ${chrootDir}/dev/ptmx c 5 2
mknod -m 644 ${chrootDir}/dev/random c 1 8
mknod -m 644 ${chrootDir}/dev/urandom c 1 9
mknod -m 666 ${chrootDir}/dev/zero c 1 5
mknod -m 666 ${chrootDir}/dev/tty c 5 0

#Setup DNS
cp -L /etc/resolv.conf ${chrootDir}/etc/

#Setup Repo
mkdir -p ${chrootDir}/etc/apk 
cp -L /etc/apk/repositories ${chrootDir}/etc/apk/repositories
cp -L /home/jenkins/.abuild/*.pub ${chrootDir}/etc/apk/keys/

#Install config files 
echo  "PS1='(Chroot-$Name)Test:\w\$ '" >>  ${chrootDir}/etc/profile
cp -L -p /etc/doas.d/jenkinsAPKbuilder.conf ${chrootDir}/etc/doas.d/jenkinsAPKbuilder.conf

#Setup user
echo "jenkins:x:1000:1000::/home/jenkins:/bin/sh" >> ${chrootDir}/etc/passwd
echo "jenkins:x:1000:" >> ${chrootDir}/etc/group
sed -i 's/abuild.*/abuild:x:300:jenkins/' ${chrootDir}/etc/group
mkdir -p ${chrootDir}/home/jenkins 
cp -L -r /home/jenkins/.abuild ${chrootDir}/home/jenkins/
chown -R jenkins:jenkins ${chrootDir}/home/jenkins 


chgrp abuild ${chrootDir}/var/cache/distfiles
chmod g+w ${chrootDir}/var/cache/distfiles 
