## Copyright (C) 2015 Jason Mehring <nrgaway@gmail.com>
## License: GPL-2+

## If not running inside a Qubes TemplateVM,
## and if running inside Whonix-Gateway (i.e. if file /usr/share/anon-gw-base-files/gateway exists),
## and if running as a NetVM or ProxyVM (i.e. if file /run/qubes/this-is-netvm or /run/qubes/this-is-proxyvm exists),
## and if qubes-updates-cache is not enabled (i.e. if file /run/qubes-service/qubes-updates-cache does not exist),
## create qubes-updates-proxy (i.e. create file /run/qubes-service/qubes-updates-proxy).

[Unit]
Description=conditionally enable qubes-updates-proxy
Documentation=https://github.com/Whonix/qubes-whonix

Before=qubes-updates-proxy.service

After=qubes-sysinit.service

## Do not enable qubes-updates-proxy if qubes-updates-cache is already enabled
## to avoid these conflicting with each other.
ConditionPathExists=!/run/qubes-service/qubes-updates-cache

## Not inside TemplateVM.
ConditionPathExists=!/run/qubes/this-is-templatevm
## Whonix-Gateway
ConditionPathExists=/usr/share/anon-gw-base-files/gateway
## And ( NetVM OR ProxyVM )
ConditionPathExists=|/run/qubes/this-is-netvm
ConditionPathExists=|/run/qubes/this-is-proxyvm

[Service]
Type=oneshot
RemainAfterExit=yes

# Trigger starting the systemd qubes-updates-proxy.service on gateways, unless
# already configured to trigger starting qubes-updates-cache.service instead.
# The caching one is heavier, hence the user is supposed to enable it manually
# with qvm-service or the Qubes VM Manager's Services tab. The same applies if
# the user wants to enable the non-caching proxy on workstations, so as not to
# waste 4 MB memory everywhere.
ExecStart=/usr/bin/touch /run/qubes-service/qubes-updates-proxy

[Install]
WantedBy=multi-user.target
