version: 2
jobs:
  openscad-mxe-32bit:
    working_directory: ~/workspace
    docker:
      - image: openscad/mxe-i686-deps:latest
    steps:
      - checkout
      - run:
          name: Build OpenSCAD Windows Application (32bit)
          no_output_timeout: 18000
          command: |
              export NUMCPU=2
              export MXEDIR=/mxe
              export LIB3MF_INCLUDEPATH=/mxe/usr/i686-w64-mingw32.static.posix/include/lib3mf
              export LIB3MF_LIBPATH=/mxe/usr/i686-w64-mingw32.static.posix/include/lib3mf
              if [ x"${CIRCLE_BRANCH}" = xmaster ]; then export SUFFIX=""; else export SUFFIX="_$(echo ${CIRCLE_BRANCH} | sed -e 's,pull/,PR,')"; fi
              export OPENSCAD_VERSION="$(date +%Y.%m.%d).ci${CIRCLE_BUILD_NUM}"
              ./scripts/release-common.sh -snapshot -mingw32 -v "$OPENSCAD_VERSION"
              mkdir -p /tmp/out
              for f in mingw*/*.zip mingw*/*.exe; do N=$(basename "$f" | sed -e "s/\\(-x86-[36][24]\\)/\\1${SUFFIX}/;"); cp -iv "$f" /tmp/out/"$N"; done
              if [ -n "${CODE_SIGNING_KEY}" ]; then
                  cd /tmp/out
                  echo $CODE_SIGNING_DATA | base64 -d | gzip -d | gpg --import --allow-secret-key-import --pinentry-mode loopback --passphrase-file <(echo $CODE_SIGNING_PW)
                  export GPG_ARGS="--batch --yes --passphrase-fd 0 --pinentry-mode loopback --detach-sign --armor"
                  for a in *.exe *.zip; do echo "$CODE_SIGNING_PW" | gpg -u "$CODE_SIGNING_KEY" $GPG_ARGS "$a"; sha1sum "$a" | tee "${a}.sha1"; sha256sum "$a" | tee "${a}.sha256"; done
                  rm -rf ~/.gnupg
              else
                  echo "Skipping code signing."
              fi
      - store_artifacts:
          path: /tmp/out
          destination: 32-bit
  openscad-mxe-64bit:
    working_directory: ~/workspace
    docker:
      - image: openscad/mxe-x86_64-deps:latest
    steps:
      - checkout
      - run:
          name: Build OpenSCAD Windows Application (64bit)
          no_output_timeout: 18000
          command: |
              export NUMCPU=2
              export MXEDIR=/mxe
              export LIB3MF_INCLUDEPATH=/mxe/usr/x86_64-w64-mingw32.static.posix/include/lib3mf
              export LIB3MF_LIBPATH=/mxe/usr/x86_64-w64-mingw32.static.posix/include/lib3mf
              if [ x"${CIRCLE_BRANCH}" = xmaster ]; then export SUFFIX=""; else export SUFFIX="_$(echo ${CIRCLE_BRANCH} | sed -e 's,pull/,PR,')"; fi
              export OPENSCAD_VERSION="$(date +%Y.%m.%d).ci${CIRCLE_BUILD_NUM}"
              ./scripts/release-common.sh -snapshot -mingw64 -v "$OPENSCAD_VERSION"
              mkdir -p /tmp/out
              for f in mingw*/*.zip mingw*/*.exe; do N=$(basename "$f" | sed -e "s/\\(-x86-[36][24]\\)/\\1${SUFFIX}/;"); cp -iv "$f" /tmp/out/"$N"; done
              if [ -n "${CODE_SIGNING_KEY}" ]; then
                  cd /tmp/out
                  echo $CODE_SIGNING_DATA | base64 -d | gzip -d | gpg --import --allow-secret-key-import --pinentry-mode loopback --passphrase-file <(echo $CODE_SIGNING_PW)
                  export GPG_ARGS="--batch --yes --passphrase-fd 0 --pinentry-mode loopback --detach-sign --armor"
                  for a in *.exe *.zip; do echo "$CODE_SIGNING_PW" | gpg -u "$CODE_SIGNING_KEY" $GPG_ARGS "$a"; sha1sum "$a" | tee "${a}.sha1"; sha256sum "$a" | tee "${a}.sha256"; done
                  rm -rf ~/.gnupg
              else
                  echo "Skipping code signing."
              fi
      - store_artifacts:
          path: /tmp/out
          destination: 64-bit
  openscad-appimage-64bit:
    working_directory: ~/workspace
    docker:
      - image: openscad/appimage-x86_64-base:latest
    steps:
      - checkout
      - run:
          name: Build OpenSCAD AppImage (64bit)
          no_output_timeout: 18000
          command: |
              set +e
              . /opt/qt511/bin/qt511-env.sh
              set -e
              export OPENSCAD_COMMIT=$(git log -1 --pretty=format:%h)
              export OPENSCAD_VERSION="$(date +%Y.%m.%d).ai${CIRCLE_BUILD_NUM}"
              if [ x"${CIRCLE_BRANCH}" = xmaster ]; then export SUFFIX=""; else export SUFFIX="_$(echo ${CIRCLE_BRANCH} | sed -e 's,pull/,PR,')"; fi
              qmake -v
              qmake CONFIG+=experimental CONFIG+=snapshot CONFIG-=debug PREFIX=/usr VERSION="$OPENSCAD_VERSION" OPENSCAD_COMMIT="$OPENSCAD_COMMIT"
              make -j2
              make install INSTALL_ROOT=AppDir
              export PATH=/appimage/usr/bin:"$PATH"
              linuxdeploy --plugin qt --output appimage --appdir AppDir
              mkdir -p /tmp/out
              for f in OpenSCAD-*.AppImage; do N=$(basename "$f" | sed -e "s/OpenSCAD-\\(.*\\)/OpenSCAD-${OPENSCAD_VERSION}-${SUFFIX}\\1/;"); cp -iv "$f" /tmp/out/"$N"; done
              if [ -n "${CODE_SIGNING_KEY}" ]; then
                  cd /tmp/out
                  echo $CODE_SIGNING_DATA | base64 -d | gzip -d | gpg --import
                  export GPG_ARGS="--batch --yes --passphrase-fd 0 --detach-sign --armor"
                  for a in *.AppImage; do echo "$CODE_SIGNING_PW" | gpg -u "$CODE_SIGNING_KEY" $GPG_ARGS "$a"; sha1sum "$a" | tee "${a}.sha1"; sha256sum "$a" | tee "${a}.sha256"; done
                  rm -rf ~/.gnupg
              else
                  echo "Skipping code signing."
              fi
      - store_artifacts:
          path: /tmp/out
          destination: 64-bit

workflows:
  version: 2
  build:
    jobs:
      - openscad-mxe-32bit:
          context: secret-context
          filters:
              branches:
                  ignore:
                      - coverity_scan
                      - /^(?i:continuous)$/
      - openscad-mxe-64bit:
          context: secret-context
          filters:
              branches:
                  ignore:
                      - coverity_scan
                      - /^(?i:continuous)$/
      - openscad-appimage-64bit:
          context: secret-context
          filters:
              branches:
                  ignore:
                      - coverity_scan
                      - /^(?i:continuous)$/
