#!/usr/bin/env python3
#
# Copyright (C) 2023 Denis 'GNUtoo' Carikli <GNUtoo@no-log.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

import os
import sys

latest_guix_version = "v1.4.0"

def usage(progname):
    print("Usage:")
    print("\t{} build <PACKAGE> [GUIX-COMMIT] # {}".format(
        progname,
        "generate PACKAGE compilation test"))
    print("")
    print("This program generates phoronix-test-suite compatible tests that use guix.")
    print("")
    print("The GUIX-COMMIT argument is a valid git revision. ", end='')
    print("If not set it defaults to {}".format(latest_guix_version))

if len(sys.argv) not in [3, 4] or sys.argv[1] != "build":
    usage(sys.argv[0])
    sys.exit(64) # see sysexits.h

package = sys.argv[2]

guix_commit = latest_guix_version
if len(sys.argv) == 4:
    guix_commit = sys.argv[3]

test_dir = ("{}/.phoronix-test-suite/test-profiles/local/build-guix-{}".format(
    os.environ['HOME'],
    package))

if not os.path.isdir(test_dir):
    os.makedirs(test_dir)

def generate_file(path, content):
    if os.path.isfile(path):
        os.remove(path)
    install_sh = open(path, 'x')
    install_sh.write(content)
    install_sh.close()

generate_file("{}/install.sh".format(test_dir),
              """#!/bin/sh
echo "#!/bin/sh
guix time-machine --commit={commit} -- \\
     build \\
     --no-substitutes \\
     --no-grafts \\
     --no-offload \\
     --check \\
     {p}" > time-compile-guix-{p}
chmod +x time-compile-guix-{p}""".format(p=package, commit=guix_commit))

generate_file("{}/pre.sh".format(test_dir),
              """#!/bin/sh
guix time-machine --commit={commit} -- build {p}""".format(p=package,
                                                           commit=guix_commit))

generate_file("{}/results-definition.xml".format(test_dir),
              """<?xml version="1.0"?>
<!--Phoronix Test Suite v7.4.0m3-->
<PhoronixTestSuite>
  <SystemMonitor>
    <Sensor>sys.time</Sensor>
  </SystemMonitor>
</PhoronixTestSuite>""")

generate_file("{}/test-definition.xml".format(test_dir),
              """<?xml version="1.0"?>
<!--Phoronix Test Suite v7.4.0m3-->
<PhoronixTestSuite>
  <TestInformation>
    <Title>Timed Guix package compilation</Title>
    <AppVersion>{commit}</AppVersion>
    <Description>This test times how long it takes to build the {p} package with Guix {commit}.</Description>
    <ResultScale>Seconds</ResultScale>
    <Proportion>LIB</Proportion>
    <SubTitle>Time To Compile</SubTitle>
    <Executable>time-compile-guix-{p}</Executable>
  </TestInformation>
  <TestProfile>
    <Version>{commit}</Version>
    <SupportedPlatforms>Linux</SupportedPlatforms>
    <SoftwareType>Utility</SoftwareType>
    <TestType>Processor</TestType>
    <License>Free</License>
    <Status>Verified</Status>
    <ExternalDependencies>build-utilities</ExternalDependencies>
    <EnvironmentSize>114</EnvironmentSize>
    <ProjectURL>https://guix.gnu.org</ProjectURL>
    <InternalTags>SMP</InternalTags>
    <Maintainer>Denis 'GNUtoo' Carikli</Maintainer>
  </TestProfile>
</PhoronixTestSuite>""".format(p=package, commit=guix_commit))

print("""To run the test you need to run the following commands:
\tphoronix-test-suite install build-guix-{p}
\tphoronix-test-suite test build-guix-{p}""".format(p=package))
