#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import time

if sys.platform != "win32":
    print("This stub is only used on Windows.")
    sys.exit(1)

import os
import subprocess as sp

appname = "DisplayCAL"

if getattr(sys, "frozen", False):
    args = [
        os.path.join(os.path.dirname(sys.executable), f"{appname}-apply-profiles.exe")
    ]
else:
    args = [sys.executable]
    dirname = os.path.dirname(__file__)
    pyw = os.path.join(dirname, "..", f"{appname}-apply-profiles.pyw")
    if os.path.isfile(pyw):
        args.append(os.path.normpath(pyw))
    else:
        args.append(os.path.join(dirname, f"{appname.lower()}-apply-profiles"))

args.append("--task")
if "--oneshot" in sys.argv[1:]:
    args.append("--oneshot")

sp.Popen(args, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.STDOUT)

# Sleep a few seconds so task scheduler can see an instance already running and
# won't start another one due to multiple triggers firing at the same time
time.sleep(9)
