#!/bin/bash
[% c("var/set_default_env") -%]
distdir=/var/tmp/dist/[% project %]

OUTDIR='[% dest_dir _ "/" _ c("filename") %]'
mkdir -p $OUTDIR
mkdir -p "$distdir"

umask 0022

[% IF c("var/build_deb_pkg") -%]
  [% IF c("var/browser-linux-i686") -%]
    # When building an i386 deb package, dh_strip requires i686-linux-gnu-*
    # commands not provided by the binutils x86_64 package on debian stretch.
    # We can use the binutils-i686-gnu package when we switch to buster.
    mkdir -p /var/tmp/dist/binutils
    for bin in strip objcopy objdump; do
      ln -s /usr/bin/$bin /var/tmp/dist/binutils/i686-linux-gnu-$bin
    done
    export PATH="/var/tmp/dist/binutils:$PATH"
  [% END -%]
  cd $distdir
  project_dir=[% c("var/system_pkg/pkg_name") %]-[% c("var/system_pkg/pkg_version") %]
  mkdir "$project_dir"
  cp -a $rootdir/Makefile $rootdir/[% c("var/system_pkg/pkg_name") %].desktop "$project_dir"
  [% c('tar', {
        tar_src => [ '$project_dir' ],
        tar_args => '-cJf ' _ c("var/system_pkg/pkg_name") _ '_' _ c("var/system_pkg/pkg_version") _ '.orig.tar.xz',
       }) %]

  cd "$project_dir"
  mv $rootdir/debian .
  chmod +x debian/docs debian/rules
  mkdir debian/source
  echo -n '3.0 (quilt)' > debian/source/format
  # Fix permissions to avoid mismatching debian.tar.xz file depending
  # on umask outside the container (tor-browser-build#41173)
  find debian -executable -exec chmod 700 {} \;
  find debian ! -executable -exec chmod 600 {} \;


  function extract_src_tar {
    src_tar="$1"
    deb_arch="$2"
    mkdir $deb_arch
    tar -C $deb_arch --strip-components=1 -xf "$src_tar"
    cp "$src_tar" \
      ../[% c("var/system_pkg/pkg_name") %]_[% c("var/system_pkg/pkg_version") %].orig-$deb_arch.tar.xz
  }
  [% IF c("var/browser-linux-x86_64") -%]
    extract_src_tar \
      $rootdir/[% c('input_files_by_name/linux-x86_64') %]/[% c("var/project-name") %]-linux-x86_64-[% c("version") %].tar.xz \
      amd64
  [% END -%]
  [% IF c("var/browser-linux-i686") -%]
    extract_src_tar \
      $rootdir/[% c('input_files_by_name/linux-i686') %]/[% c("var/project-name") %]-linux-i686-[% c("version") %].tar.xz \
      i386
  [% END -%]
  [% IF c("var/browser-linux-aarch64") -%]
    extract_src_tar \
      $rootdir/[% c('input_files_by_name/linux-aarch64') %]/[% c("var/project-name") %]-linux-aarch64-[% c("version") %].tar.xz \
      arm64
  [% END -%]

  dpkg-buildpackage --unsigned-source --unsigned-changes --build=source
  [% FOREACH deb_arch = c("var_p/system_pkg/deb_archs") -%]
    dpkg-buildpackage --unsigned-source --unsigned-changes --build=binary --host-arch=[% deb_arch %]
    deb_file_name='[% c("var/system_pkg/pkg_name") %]_[% c("var/system_pkg/pkg_version") %]-[% c("var/system_pkg/pkg_revision") %]_[% deb_arch %].deb'
    dpkg-deb --info "../$deb_file_name"
    mv -f ../"$deb_file_name" "$OUTDIR/"
  [% END -%]

  cd ..
  mv -f *.dsc *.orig.tar.xz *.debian.tar.xz "$OUTDIR/"
[% END %]

[% IF c("var/build_rpm_pkg") -%]

  cat > ~/.rpmmacros << EOF
  %clamp_mtime_to_source_date_epoch 1
  %use_source_date_epoch_as_buildtime 1
  %build_mtime_policy clamp_to_source_date_epoch
  %_buildhost localhost
EOF

  mkdir rpm
  cd rpm
  mkdir SPECS SOURCES
  cp -a $rootdir/[% c("var/system_pkg/pkg_name") %].spec SPECS
  cp -a $rootdir/Makefile $rootdir/[% c("var/system_pkg/pkg_name") %].desktop SOURCES/
  [% IF c("var/browser-linux-x86_64") -%]
    cp -a $rootdir/[% c('input_files_by_name/linux-x86_64') %]/[% c("var/project-name") %]-linux-x86_64-[% c("version") %].tar.xz \
      SOURCES/[% c("var/system_pkg/pkg_name") %]-linux-x86_64-[% c("var/system_pkg/pkg_version") %].tar.xz
  [% END -%]
  [% IF c("var/browser-linux-i686") -%]
    cp -a $rootdir/[% c('input_files_by_name/linux-i686') %]/[% c("var/project-name") %]-linux-i686-[% c("version") %].tar.xz \
      SOURCES/[% c("var/system_pkg/pkg_name") %]-linux-i386-[% c("var/system_pkg/pkg_version") %].tar.xz
  [% END -%]
  [% IF c("var/browser-linux-aarch64") -%]
    cp -a $rootdir/[% c('input_files_by_name/linux-aarch64') %]/[% c("var/project-name") %]-linux-aarch64-[% c("version") %].tar.xz \
      SOURCES/[% c("var/system_pkg/pkg_name") %]-linux-aarch64-[% c("var/system_pkg/pkg_version") %].tar.xz
  [% END -%]
  rpmdir=$(pwd)
  [% IF c("var/browser-linux-x86_64") -%]
    rpmbuild --define "_topdir $rpmdir" -v -bb SPECS/[% c("var/system_pkg/pkg_name") %].spec
  [% END -%]
  [% IF c("var/browser-linux-i686") -%]
    rpmbuild --define "_topdir $rpmdir" --target i686-redhat-linux -v -bb SPECS/[% c("var/system_pkg/pkg_name") %].spec
  [% END -%]
  [% IF c("var/browser-linux-aarch64") -%]
    rpmbuild --define "_topdir $rpmdir" --target aarch64-redhat-linux -v -bb SPECS/[% c("var/system_pkg/pkg_name") %].spec
  [% END -%]
  mv RPMS/*/*.rpm "$OUTDIR/"

[% END %]
