#!/bin/sh

set -e
sudo apt update
sudo apt install -y gnupg
wget https://ftp.gnu.org/gnu/guix/guix-binary-1.1.0.x86_64-linux.tar.xz
wget https://ftp.gnu.org/gnu/guix/guix-binary-1.1.0.x86_64-linux.tar.xz.sig
wget https://sv.gnu.org/people/viewgpg.php?user_id=15145 -qO - | gpg --import -
gpg --verify guix-binary-1.1.0.x86_64-linux.tar.xz.sig
sudo -s <<'EOF'
cd /tmp
tar -xf ~admin/guix-binary-1.1.0.x86_64-linux.tar.xz
mv var/guix /var && mv gnu /
mkdir -p ~root/.config/guix
ln -sf /var/guix/profiles/per-user/root/current-guix ~root/.config/guix/current
GUIX_PROFILE="`echo ~root`/.config/guix/current"
source $GUIX_PROFILE/etc/profile
groupadd --system guixbuild
for i in `seq -w 1 10`; do
    useradd -g guixbuild -G guixbuild           \
            -d /var/empty -s `which nologin`    \
            -c "Guix build user $i" --system    \
            guixbuilder$i;
done
cp ~root/.config/guix/current/lib/systemd/system/guix-daemon.service /etc/systemd/system
systemctl start guix-daemon && systemctl enable guix-daemon
cd /usr/local/bin
ln -s /var/guix/profiles/per-user/root/current-guix/bin/guix
cd /usr/local/share/info
for i in /var/guix/profiles/per-user/root/current-guix/share/info/*; do
    ln -s $i
done
guix archive --authorize <~root/.config/guix/current/share/guix/ci.guix.gnu.org.pub
EOF
guix install glibc-utf8-locales
cat >>.profile <<'EOF'

export GUIX_LOCPATH=$HOME/.guix-profile/lib/locale
EOF
