stages:
  - build
  - publish

variables:
  OPENWRT_VERSION: 23.05.3
  FILES: ${CI_PROJECT_DIR}/files
  DEFAULT_PACKAGES: luci
  EXTRA_PACKAGES: openwisp-config openwisp-monitoring netjson-monitoring
  DISABLED_SERVICES: dnsmasq firewall odhcpd
  EXTRA_IMAGE_NAME: fakedOrg

.build_image:
  stage: build
  image: openwrt/imagebuilder:${TARGET}-${SUBTARGET}-${OPENWRT_VERSION}
  before_script:
    - if [[ -d ./packages ]]; then mv ./packages /builder/; fi
    - if [[ -d ./dl ]]; then mv ./dl /builder/; fi
    - for url in $(cat packages.txt); do curl --create-dirs -s -O --output-dir /builder/packages $url; done && ls -1 /builder/packages
    - cd /builder
  script:
    - echo PROFILE=${PROFILE} FILES=${FILES} PACKAGES="${DEFAULT_PACKAGES} ${EXTRA_PACKAGES}"
    - make image PROFILE=${PROFILE} FILES=${FILES} PACKAGES="${DEFAULT_PACKAGES} ${EXTRA_PACKAGES}"
  after_script:
    - if [[ -d /builder/bin ]]; then mv /builder/bin .; fi
    - if [[ -d /builder/dl ]]; then mv /builder/dl .; fi
  cache:
    key: ${OPENWRT_VERSION}-${TARGET}
    paths:
      - dl/
  artifacts:
    paths:
      - bin/
    expire_in: 1 hour

cudy_wr3000-v1:
  extends: .build_image
  variables:
    TARGET: mediatek
    SUBTARGET: filogic
    PROFILE: cudy_wr3000-v1

cudy_re3000-v1:
  extends: .build_image
  variables:
    TARGET: mediatek
    SUBTARGET: filogic
    PROFILE: cudy_re3000-v1

publish:
  stage: publish
  script:
    - sudo rsync -avrhW --delete bin/targets/mediatek/filogic/*.bin /srv/www/faked.org/openwrt/
  tags:
    - shell
