[Unit]
Description=Workaround Incus not having the correct label
ConditionPathExists=/usr/bin/incus
ConditionPathExists=/usr/bin/incus-agent
ConditionPathExists=/usr/lib/incus
After=local-fs.target

[Service]
Type=oneshot
# Copy if it doesn't exist
ExecStartPre=/usr/bin/bash -c "[ -x /usr/local/bin/.incus ] || /usr/bin/cp $(readlink /usr/bin/incus) /usr/local/bin/.incus"
ExecStartPre=/usr/bin/bash -c "[ -x /usr/local/bin/.incus-agent ] || /usr/bin/cp $(readlink /usr/bin/incus-agent) /usr/local/bin/.incus-agent"
ExecStartPre=/usr/bin/bash -c "[ -d /usr/local/lib/.incus ] || /usr/bin/cp -r /usr/lib/incus /usr/local/lib/.incus"
# This is faster than using .mount unit. Also allows for the previous line/cleanup
ExecStartPre=/usr/bin/mount --bind /usr/local/bin/.incus /usr/bin/incus
ExecStartPre=/usr/bin/mount --bind /usr/local/bin/.incus-agent /usr/bin/incus-agent
ExecStartPre=/usr/bin/mount --bind /usr/local/lib/.incus /usr/lib/incus
# Fix SELinux label
ExecStart=/usr/sbin/restorecon -R /usr/bin/incus
ExecStart=/usr/sbin/restorecon -R /usr/bin/incus-agent
ExecStart=/usr/sbin/restorecon -R /usr/lib/incus
# Clean-up after ourselves
ExecStop=/usr/bin/umount /usr/bin/incus
ExecStop=/usr/bin/umount /usr/bin/incus-agent
ExecStop=/usr/bin/umount /usr/lib/incus
ExecStop=/usr/bin/rm -r /usr/local/bin/.incus
ExecStop=/usr/bin/rm -r /usr/local/bin/.incus-agent
ExecStop=/usr/bin/rm -r /usr/local/lib/.incus
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
