# this starlark script should be used to generate the .drone.yml
# configuration file.

def main(ctx):
  # TODO consider running unit tests before building and
  # publishing docker images.
  before = {}

  stages = [
    linux('arm'),
    linux('arm64'),
    linux('amd64'),
    windows('1903'),
    windows('1809'),
  ]

  after = manifest()

  # the after stage should only execute after all previous
  # stages complete. this builds the dependency graph.
  for stage in stages:
    after['depends_on'].append(stage['name'])

  return stages + [ after ]

# create a pipeline stage responsible for building and
# publishing the Docker image on linux.
def linux(arch):
  return {
    'kind': 'pipeline',
    'type': 'docker',
    'name': 'linux-%s' % arch,
    'platform': {
      'os': 'linux',
      'arch': arch,
    },
    'steps': [
      {
        'name': 'build',
        'image': 'golang:1.10',
        'commands': [
          'cd posix',
          'tar -xf fixtures.tar -C /',
          'go test -v',
        ],
      },
      {
        'name': 'publish',
        'image': 'plugins/docker',
        'settings': {
          'auto_tag': 'true',
          'auto_tag_suffix': 'linux-%s' % arch,
          'dockerfile': 'docker/Dockerfile.linux.%s' % arch,
          'password': { 
            'from_secret': 'docker_password',
          },
          'repo': 'drone/git',
          'username': 'drone',
        },
        'when': {
          'event': ['push', 'tag']
        }
      }
    ]
  }

# create a pipeline stage responsible for building and
# publishing the Docker image on windows. The windows stage
# uses an ssh runner, as opposed to a docker runner.
def windows(version):
  return {
    'kind': 'pipeline',
    'type': 'ssh',
    'name': 'windows-%s-amd64' % version,
    'platform': {
      'os': 'windows'
    },
    'server': {
      'host':     { 'from_secret': 'windows_server_%s' % version },
      'user':     { 'from_secret': 'windows_username' },
      'password': { 'from_secret': 'windows_password' },
    },
    'steps': [
      {
        'name': 'build',
        'environment': {
          'USERNAME': { 'from_secret': 'docker_username' },
          'PASSWORD': { 'from_secret': 'docker_password' },
        },
        # TODO these commands build and publish the latest
        # docker tag regardless of git tag.
        'commands': [
          'docker login -u $env:USERNAME -p $env:PASSWORD',
          'docker build -f docker/Dockerfile.windows.%s -t drone/git:windows-%s-amd64 .' % (version, version),
          'docker push drone/git:windows-%s-amd64' % version,
        ],
      },
    ],
    'trigger': {
      'event': ['push']
    }
  }

# create a pipeline stage responsible for creating and
# publishing a docker manifest to the registry.
def manifest():
  return {
    'kind': 'pipeline',
    'type': 'docker',
    'name': 'manifest',
    'steps': [
      {
        'name': 'manifest',
        'image': 'plugins/manifest',
        'settings': {
          'auto_tag': 'true',
          'username': 'drone',
          'password': {
            'from_secret': 'docker_password'
          },
          'spec': 'docker/manifest.tmpl',
          'ignore_missing': 'true',
        },
      },
    ],
    'depends_on': [],
    'trigger': {
      'event': ['push', 'tag']
    }
  }