#!/bin/bash
[% c("var/set_default_env") -%]
[% pc('go', 'var/setup', { go_tarfile => c('input_files_by_name/go') }) %]
distdir=/var/tmp/dist/[% project %]
mkdir -p $distdir

tar -C /var/tmp/dist -xf [% c('input_files_by_name/go-licenses') %]
export PATH=$PATH:/var/tmp/dist/go-licenses

[% IF c("var/android") -%]
  [% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
  # We need to explicitly set CGO_ENABLED with Go 1.13.x as the Android build
  # breaks otherwise.
  export CGO_ENABLED=1
[% END -%]

mkdir -p /var/tmp/build
tar -C /var/tmp/build -xf [% project %]-[% c('version') %].tar.[% c('compress_tar') %]
cd /var/tmp/build/[% project %]-[% c('version') %]

tar -xf $rootdir/[% c('input_files_by_name/go_vendor') %]

go build -mod=vendor -ldflags '-X main.lyrebirdVersion=[% c("version") %] -s[% IF c("var/android") %] -checklinkname=0[% END %]' ./cmd/lyrebird
cp -a lyrebird[% IF c("var/windows") %].exe[% END %] $distdir

go-licenses save ./cmd/lyrebird --save_path=$distdir/licenses

cd $distdir
[% c('tar', {
        tar_src => [ '.' ],
        tar_args => '-caf ' _ dest_dir _ '/' _ c('filename'),
    }) %]
