.rdep-build-before-script: &rdep-build-before-script |
   # Reported in https://salsa.debian.org/salsa-ci-team/pipeline/issues/104,
   # GitLab can only expand variables once. So at the beginning CCACHE_WORK_DIR
   # was assigned to `${WORKING_DIR}/.ccache`, and it will be expanded as
   # `$CI_PROJECT_DIR/debian/output/.ccache`, so it creates a folder named
   # "\$CI_PROJECT_DIR", which is then saved as build cache. To allow smooth
   # transition, that wrongly named folder has to be removed:
   rm -rf '$CI_PROJECT_DIR'

   # salsa-ci-team/pipeline#107
   rm -rf ${CI_PROJECT_DIR}/debian/output/.ccache

   mkdir -p ${WORKING_DIR} ${CCACHE_WORK_DIR}

   # https://salsa.debian.org/salsa-ci-team/pipeline/-/merge_requests/230
   rm -rf ${CCACHE_TMP_DIR}

   mv ${CCACHE_WORK_DIR} ${CCACHE_TMP_DIR}
   add_extra_repository.sh -v -e "${SALSA_CI_EXTRA_REPOSITORY}" -k "${SALSA_CI_EXTRA_REPOSITORY_KEY}"


.rdep-build-script: &rdep-build-script |
   export CCACHE_DIR=${CCACHE_TMP_DIR}
   # Add deb-src entries
   sed -n '/^deb\s/s//deb-src /p' /etc/apt/sources.list > /etc/apt/sources.list.d/deb-src.list
   apt-get update && eatmydata apt-get install --no-install-recommends -y \
     aptitude \
     devscripts \
     ccache \
     equivs \
     build-essential
   apt -y build-dep ${REVERSE_DEP}
   # install built packages
   dpkg -i ${WORKING_DIR}/*.deb || apt -y -f install
   # download source
   cd ${WORKING_DIR}
   # Generate ccache links
   dpkg-reconfigure ccache
   PATH="/usr/lib/ccache/:${PATH}"
   # Reset ccache stats
   ccache -z
   # Create salsaci user and fix permissions
   useradd salsaci
   chown -R salsaci. ${WORKING_DIR} ${CCACHE_DIR}
   # Define buildlog filename
   BUILD_LOGFILE="${WORKING_DIR}/${REVERSE_DEP}.build"
   # Build package as user salsaci
   su salsaci -c "eatmydata apt source -b ${REVERSE_DEP}" |& OUTPUT_FILENAME=${BUILD_LOGFILE} filter-output
   # Restore PWD to ${WORKING_DIR}
   cd ${WORKING_DIR}
   rm -rf ${WORKING_DIR}/${REVERSE_DEP}*
   # Print ccache stats on job log
   ccache -s

.rdep-build-definition: &rdep-build-definition
  stage: test
  image: $SALSA_CI_IMAGES_BASE
  cache:
    key: "${REVERSE_DEP}-build"
    paths:
      - .ccache
  except:
    variables:
      - $SALSA_CI_ENABLE_REVERSE_DEPENDENCY_BUILD !~ /^(1|yes|true)$/
      - $CI_COMMIT_TAG != null && $SALSA_CI_ENABLE_PIPELINE_ON_TAGS !~ /^(1|yes|true)$/
      - $CI_MERGE_REQUEST_ID != null
  variables:
    CCACHE_TMP_DIR: ${CI_PROJECT_DIR}/../.ccache
    CCACHE_WORK_DIR: ${CI_PROJECT_DIR}/.ccache
  script:
    - *rdep-build-before-script
    - *rdep-build-script
    - mv ${CCACHE_TMP_DIR} ${CCACHE_WORK_DIR}
  needs:
    - job: build
      artifacts: true






build-rdep-golang-github-evanw-esbuild:
  variables:
    REVERSE_DEP: golang-github-evanw-esbuild
  extends: .rdep-build-definition


build-rdep-node-evp-bytestokey:
  variables:
    REVERSE_DEP: node-evp-bytestokey
  extends: .rdep-build-definition


build-rdep-node-expat:
  variables:
    REVERSE_DEP: node-expat
  extends: .rdep-build-definition


build-rdep-node-fbjs:
  variables:
    REVERSE_DEP: node-fbjs
  extends: .rdep-build-definition


build-rdep-node-grunt-sass:
  variables:
    REVERSE_DEP: node-grunt-sass
  extends: .rdep-build-definition


build-rdep-node-iconv:
  variables:
    REVERSE_DEP: node-iconv
  extends: .rdep-build-definition


build-rdep-node-jasmine:
  variables:
    REVERSE_DEP: node-jasmine
  extends: .rdep-build-definition


build-rdep-node-leveldown:
  variables:
    REVERSE_DEP: node-leveldown
  extends: .rdep-build-definition


build-rdep-node-modern-syslog:
  variables:
    REVERSE_DEP: node-modern-syslog
  extends: .rdep-build-definition


build-rdep-node-nan:
  variables:
    REVERSE_DEP: node-nan
  extends: .rdep-build-definition


build-rdep-node-node-sass:
  variables:
    REVERSE_DEP: node-node-sass
  extends: .rdep-build-definition


build-rdep-node-nodedbi:
  variables:
    REVERSE_DEP: node-nodedbi
  extends: .rdep-build-definition


build-rdep-node-nouislider:
  variables:
    REVERSE_DEP: node-nouislider
  extends: .rdep-build-definition


build-rdep-node-opencv:
  variables:
    REVERSE_DEP: node-opencv
  extends: .rdep-build-definition


build-rdep-node-pause:
  variables:
    REVERSE_DEP: node-pause
  extends: .rdep-build-definition


build-rdep-node-pre-gyp:
  variables:
    REVERSE_DEP: node-pre-gyp
  extends: .rdep-build-definition


build-rdep-node-re2:
  variables:
    REVERSE_DEP: node-re2
  extends: .rdep-build-definition


build-rdep-node-react:
  variables:
    REVERSE_DEP: node-react
  extends: .rdep-build-definition


build-rdep-node-rollup-plugin-sass:
  variables:
    REVERSE_DEP: node-rollup-plugin-sass
  extends: .rdep-build-definition


build-rdep-node-sqlite3:
  variables:
    REVERSE_DEP: node-sqlite3
  extends: .rdep-build-definition


build-rdep-node-stable:
  variables:
    REVERSE_DEP: node-stable
  extends: .rdep-build-definition


build-rdep-node-websocket:
  variables:
    REVERSE_DEP: node-websocket
  extends: .rdep-build-definition


build-rdep-node-ws:
  variables:
    REVERSE_DEP: node-ws
  extends: .rdep-build-definition


build-rdep-node-zipfile:
  variables:
    REVERSE_DEP: node-zipfile
  extends: .rdep-build-definition


build-rdep-npm:
  variables:
    REVERSE_DEP: npm
  extends: .rdep-build-definition


build-rdep-r-cran-v8:
  variables:
    REVERSE_DEP: r-cran-v8
  extends: .rdep-build-definition


build-rdep-ruby-license-finder:
  variables:
    REVERSE_DEP: ruby-license-finder
  extends: .rdep-build-definition


build-rdep-select2.js:
  variables:
    REVERSE_DEP: select2.js
  extends: .rdep-build-definition

