stages:
  - prepare
  - build
  - combine
  - pack
  - deploy

variables:
  GIT_DEPTH: "1"
  CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
  CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest

mat_common:
  stage: prepare
  tags: 
    - linux
  script:
    # lua
    - wget -O - https://cdn01.moecube.com/ygopro-build-materials/lua-5.4.4.tar.gz | tar zfx -
    - mv lua-5.4.4 lua
    # sqlite3
    - wget -O - https://cdn01.moecube.com/ygopro-build-materials/sqlite-autoconf-3390300.tar.gz | tar zfx -
    - mv sqlite-autoconf-3390300 sqlite3
    # freetype
    - wget -O - https://cdn01.moecube.com/ygopro-build-materials/freetype-2.11.1.tar.gz | tar zfx -
    - mv freetype-2.11.1 freetype
    # miniaudio
    - ./.ci/prepare-miniaudio.sh
    # premake
    - cp -rf premake/* .;
  artifacts:
    paths:
      - lua
      - freetype
      - sqlite3
      - miniaudio

mat_submodules:
  stage: prepare
  tags: 
    - linux
  script:
    - rm -rf ocgcore script
    - mkdir ocgcore script
    - git config --global --add safe.directory $CI_PROJECT_DIR
    - git submodule update --init
  artifacts:
    paths:
      - ocgcore
      - script

mat_linux:
  stage: prepare
  tags:
    - linux
  image: git-registry.mycard.moe/mycard/docker-runner-base:debian11
  script:
    - apt update; apt -y install git wget tar
    - git clone --depth=1 https://code.mycard.moe/mycard/irrlicht-new irrlicht
  artifacts:
    paths:
      - irrlicht

mat_macos:
  stage: prepare
  tags:
    - linux
  script:
    - apt update; apt -y install wget tar
    - wget -O - https://cdn01.moecube.com/ygopro-build-materials/premake-5.0.0-beta5-macosx.tar.gz | tar zfx -
    - chmod +x premake5
    - mkdir irrlicht
    - cd irrlicht
    - wget -O - https://cdn01.moecube.com/ygopro-build-materials/irrlicht-mycard-mac.tar.gz | tar zfx -
    - cd ..
  artifacts:
    paths:
      - premake5
      - irrlicht

mat_windows:
  stage: prepare
  tags:
    - linux
  script:
    - apt update; apt -y install wget tar patch p7zip-full
    # premake5.exe
    - wget https://cdn01.moecube.com/ygopro-build-materials/premake-5.0.0-beta5-windows.zip
    - 7z x -y premake-5.0.0-beta5-windows.zip
    # event
    - wget -O - https://cdn01.moecube.com/ygopro-build-materials/libevent-2.0.22-stable.tar.gz | tar zfx -
    - mv libevent-2.0.22-stable event
    # irrlicht
    - git clone --depth=1 -b develop https://code.mycard.moe/mycard/irrlicht-new irrlicht
  artifacts:
    paths:
      - premake5.exe
      - event
      - irrlicht

._exec_build:
  stage: build
  variables:
    NO_LUA_SAFE: '1' # on client no lua safe
  cache:
    key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
    paths:
      - bin/
      - obj/

exec_windows:
  extends: ._exec_build
  tags:
    - vs
  dependencies:
    - mat_common
    - mat_windows
    - mat_submodules
  script:
    - bash -c 'cp -rf premake/* . ; cp -rf resource/* .'
    - '.\premake5.exe vs2019'
    - cmd /c '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\msbuild.exe" build\YGOPro.sln /m /p:Configuration=Release'
    - copy bin\release\ygopro.exe .
  artifacts:
    paths:
      - ygopro.exe

.exec_linux:
  extends: ._exec_build
  tags:
    - linux
  image: git-registry.moenext.com/mycard/docker-ygopro-builder
  dependencies:
    - mat_common
    - mat_linux
    - mat_submodules
  variables:
    EVENT_INCLUDE_DIR: /usr/share/libevent-stable/include
    EVENT_LIB_DIR: /usr/share/libevent-stable/lib
    OPUS_INCLUDE_DIR: $CI_PROJECT_DIR/miniaudio/external-built/include/opus
    OPUS_LIB_DIR: $CI_PROJECT_DIR/miniaudio/external-built/lib
    VORBIS_INCLUDE_DIR: $CI_PROJECT_DIR/miniaudio/external-built/include
    VORBIS_LIB_DIR: $CI_PROJECT_DIR/miniaudio/external-built/lib
    OGG_INCLUDE_DIR: $CI_PROJECT_DIR/miniaudio/external-built/include
    OGG_LIB_DIR: $CI_PROJECT_DIR/miniaudio/external-built/lib
  script:
    - ./.ci/build-opus.sh
    - premake5 gmake --build-freetype --build-sqlite --build-irrlicht
    - cd build
    - make config=release -j$(nproc)
    - cd ..
    - mv bin/release/YGOPro ./ygopro
    - strip ygopro
  artifacts:
    paths:
      - ygopro

exec_linux:
  extends: .exec_linux

exec_linuxarm:
  extends: .exec_linux
  tags:
    - arm

._exec_macos_platform:
  extends: ._exec_build
  dependencies:
    - mat_macos
    - mat_common
    - mat_submodules
  script:
    - ./.ci/exec-macos-platform.sh
  artifacts:
    paths:
      - ygopro-platforms

exec_macos_platform_x86:
  extends: ._exec_macos_platform
  tags:
    - macos

      
exec_macos_platform_m1:
  extends: ._exec_macos_platform
  tags:
    - macos-m1
  variables:
    MAC_ARM: 1

exec_macos:
  stage: combine
  tags:
    - macos
  dependencies:
    - exec_macos_platform_x86
    - exec_macos_platform_m1
  script:
    - mkdir -p ygopro.app/Contents/MacOS ygopro.app/Contents/Frameworks;
    - lipo -create -output ygopro.app/Contents/MacOS/ygopro $(ls -1 ygopro-platforms/ygopro-platform-*);
    - mkdir ygopro.app/Contents/Resources;
    - mv resource/gframe/ygopro.icns ygopro.app/Contents/Resources/Icon.icns;
    - defaults write "$PWD/ygopro.app/Contents/Info.plist" "CFBundleIconFile" "Icon.icns";
    - defaults write "$PWD/ygopro.app/Contents/Info.plist" "CFBundleIdentifier" "moe.mycard.ygopro";

  artifacts:
    paths:
      - ygopro.app

#exec_macos_sign:
#  stage: sign
#  tags:
#    - macos
#  dependencies:
#    - exec_macos
#  script: ./.ci/sign-macos.sh
#  artifacts:
#    paths:
#      - ygopro.app
#  only:
#    - tags
#    - another

assets:
  stage: prepare
  tags:
    - linux
  script:
    - apt update; apt -y install tar wget git
    # starter pack
    - wget -O - https://code.mycard.moe/mycard/ygopro-starter-pack/-/archive/master/ygopro-starter-pack-master.tar.gz | tar zfx -
    - mv ygopro-starter-pack-master/* .
    # sound
    - wget -O - https://code.mycard.moe/mycard/ygopro-sounds/-/archive/master/ygopro-sounds-master.tar.gz | tar zfx -
    - mv ygopro-sounds-master/sound/* sound
    # fonts
    - mkdir fonts
    - cd fonts
    - wget -O - https://cdn01.moecube.com/ygopro-fonts.tar.gz | tar zfx -
    - cd ..
    # pack
    - git clone --depth=1 https://code.mycard.moe/mycard/ygopro-card-list
    - mv ygopro-card-list/pack .
    # locales
    - git clone --depth=1 https://code.mycard.moe/nanahira/ygopro-database
    - mv ygopro-database/locales .
  artifacts:
    paths:
      - deck
      - single
      - sound
      - fonts
      - pack
      - locales
  only:
    - tags
    - another

assets_nonwindows:
  stage: prepare
  tags:
    - linux
  script:
    - apt update; apt -y install tar wget
    - wget -O - https://cdn01.moecube.com/windbot/windbot.tar.gz | tar zfx -
  artifacts:
    paths:
      - bot.conf
      - windbot
      - bot
  only:
    - tags
    - another

assets_windows:
  stage: prepare
  tags:
    - linux
  script:
    - wget https://cdn01.moecube.com/windbot/WindBot.7z
    - 7z x -y WindBot.7z
    - aws s3 --endpoint=https://minio.mycard.moe:9000 sync s3://nanahira/koishipro-another/contents/update-koishipro/ ./update-koishipro
    - rm -rf update-koishipro/.git || true
  artifacts:
    paths:
      - bot.conf
      - WindBot
      - Bot.exe
      - update-koishipro
  only:
    - tags
    - another

._locale_zh-CN:
  variables:
    TARGET_LOCALE: zh-CN

._locale_en-US:
  variables:
    TARGET_LOCALE: en-US

._assets_locale:
  stage: build
  tags:
    - linux
  script: ./.ci/assets-locale.sh
  artifacts:
    paths:
      - pics
      - cards.cdb
      - strings.conf
      - bot.conf
  only:
    - tags
    - another

assets_zh-CN:
  extends: 
    - ._assets_locale
    - ._locale_zh-CN

assets_en-US:
  extends: 
    - ._assets_locale
    - ._locale_en-US

._pack:
  stage: pack
  tags:
    - linux
  artifacts:
    paths:
      - dist
  only:
    - tags
    - another


._pack_format_gz:
  extends: ._pack
  variables:
    ARCHIVE_SUFFIX: gz

._pack_format_zst:
  extends: ._pack
  variables:
    ARCHIVE_SUFFIX: zst

._pack_dlc:
  extends: ._pack_format_zst
  only:
    - tags

._pack_script_windows_non7z:
  extends: ._pack
  script: ./.ci/pack-windows.sh

._pack_script_windows_7z:
  extends: ._pack
  script: ./.ci/pack-windows-7z.sh

._pack_script_linux:
  extends: ._pack
  script: ./.ci/pack-linux.sh

._pack_script_macos:
  extends: ._pack
  script: ./.ci/pack-macos.sh

._pack_script_windows_dlc:
  extends: ._pack_dlc
  script: ./.ci/pack-windows-dlc.sh

._pack_script_linux_dlc:
  extends: ._pack_dlc
  script: ./.ci/pack-linux-dlc.sh

._pack_script_macos_dlc:
  extends: ._pack_dlc
  script: ./.ci/pack-macos-dlc.sh

._pack_assets_windows_zh-CN:
  extends: ._pack
  dependencies:
    - assets
    - assets_windows
    - exec_windows
    - assets_zh-CN
    - mat_submodules

._pack_assets_linux_zh-CN:
  extends: ._pack
  dependencies:
    - assets
    - assets_nonwindows
    - exec_linux
    - assets_zh-CN
    - mat_submodules

._pack_assets_linuxarm_zh-CN:
  extends: ._pack
  dependencies:
    - assets
    - assets_nonwindows
    - exec_linuxarm
    - assets_zh-CN
    - mat_submodules

._pack_assets_macos_zh-CN:
  extends: ._pack
  dependencies:
    - assets
    - assets_nonwindows
    - exec_macos
    - assets_zh-CN
    - mat_submodules

._pack_assets_windows_en-US:
  extends: ._pack
  dependencies:
    - assets
    - assets_windows
    - exec_windows
    - assets_en-US
    - mat_submodules

._pack_assets_linux_en-US:
  extends: ._pack
  dependencies:
    - assets
    - assets_nonwindows
    - exec_linux
    - assets_en-US
    - mat_submodules

._pack_assets_linuxarm_en-US:
  extends: ._pack
  dependencies:
    - assets
    - assets_nonwindows
    - exec_linuxarm
    - assets_en-US
    - mat_submodules

._pack_assets_macos_en-US:
  extends: ._pack
  dependencies:
    - assets
    - assets_nonwindows
    - exec_macos
    - assets_en-US
    - mat_submodules

._pack_assets_windows_dlc:
  extends: ._pack
  dependencies:
    - assets
    - exec_windows
    - mat_submodules

._pack_assets_linux_dlc:
  extends: ._pack
  dependencies:
    - assets
    - exec_linux
    - mat_submodules

._pack_assets_macos_dlc:
  extends: ._pack
  dependencies:
    - assets
    - exec_macos
    - mat_submodules

pack_windows_dlc_zh-CN:
  extends:
    - ._pack_assets_windows_dlc
    - ._pack_script_windows_dlc
    - ._locale_zh-CN

pack_linux_dlc_zh-CN:
  extends:
    - ._pack_assets_linux_dlc
    - ._pack_script_linux_dlc
    - ._locale_zh-CN

pack_macos_dlc_zh-CN:
  extends:
    - ._pack_assets_macos_dlc
    - ._pack_script_macos_dlc
    - ._locale_zh-CN

pack_windows_dlc_en-US:
  extends:
    - ._pack_assets_windows_dlc
    - ._pack_script_windows_dlc
    - ._locale_en-US

pack_linux_dlc_en-US:
  extends:
    - ._pack_assets_linux_dlc
    - ._pack_script_linux_dlc
    - ._locale_en-US

pack_macos_dlc_en-US:
  extends:
    - ._pack_assets_macos_dlc
    - ._pack_script_macos_dlc
    - ._locale_en-US

pack_windows_zh-CN_7z:
  extends:
    - ._pack_assets_windows_zh-CN
    - ._pack_script_windows_7z
    - ._locale_zh-CN

#pack_windows_zh-CN_gz:
#  extends:
#    - ._pack_assets_windows_zh-CN
#    - ._pack_script_windows_non7z
#    - ._pack_format_gz
#    - ._locale_zh-CN

#pack_windows_zh-CN_zst:
#  extends:
#    - ._pack_assets_windows_zh-CN
#    - ._pack_script_windows_non7z
#    - ._pack_format_zst
#    - ._locale_zh-CN

pack_linux_zh-CN_gz:
  extends:
    - ._pack_assets_linux_zh-CN
    - ._pack_script_linux
    - ._pack_format_gz
    - ._locale_zh-CN

pack_linuxarm_zh-CN_gz:
  extends:
    - ._pack_assets_linuxarm_zh-CN
    - ._pack_script_linux
    - ._pack_format_gz
    - ._locale_zh-CN
  variables:
    TARGET_PLATFORM: linuxarm

#pack_linux_zh-CN_zst:
#  extends:
#    - ._pack_assets_linux_zh-CN
#    - ._pack_script_linux
#    - ._pack_format_zst
#    - ._locale_zh-CN


pack_macos_zh-CN_gz:
  extends:
    - ._pack_assets_macos_zh-CN
    - ._pack_script_macos
    - ._pack_format_gz
    - ._locale_zh-CN

#pack_macos_zh-CN_zst:
#  extends:
#    - ._pack_assets_macos_zh-CN
#    - ._pack_script_macos
#    - ._pack_format_zst
#    - ._locale_zh-CN

pack_windows_en-US_7z:
  extends:
    - ._pack_assets_windows_en-US
    - ._pack_script_windows_7z
    - ._locale_en-US

#pack_windows_en-US_gz:
#  extends:
#    - ._pack_assets_windows_en-US
#    - ._pack_script_windows_non7z
#    - ._pack_format_gz
#    - ._locale_en-US

#pack_windows_en-US_zst:
#  extends:
#    - ._pack_assets_windows_en-US
#    - ._pack_script_windows_non7z
#    - ._pack_format_zst
#    - ._locale_en-US

pack_linux_en-US_gz:
  extends:
    - ._pack_assets_linux_en-US
    - ._pack_script_linux
    - ._pack_format_gz
    - ._locale_en-US

pack_linuxarm_en-US_gz:
  extends:
    - ._pack_assets_linuxarm_en-US
    - ._pack_script_linux
    - ._pack_format_gz
    - ._locale_en-US
  variables:
    TARGET_PLATFORM: linuxarm

#pack_linux_en-US_zst:
#  extends:
#    - ._pack_assets_linux_en-US
#    - ._pack_script_linux
#    - ._pack_format_zst
#    - ._locale_en-US

pack_macos_en-US_gz:
  extends:
    - ._pack_assets_macos_en-US
    - ._pack_script_macos
    - ._pack_format_gz
    - ._locale_en-US

#pack_macos_en-US_zst:
#  extends:
#    - ._pack_assets_macos_en-US
#    - ._pack_script_macos
#    - ._pack_format_zst
#    - ._locale_en-US

upload_contents:
  stage: deploy
  dependencies:
    - pack_windows_zh-CN_7z
  tags: 
    - linux
  script:
    - 7z x -y -ocontents dist/KoishiPro-$CI_COMMIT_REF_NAME-win32-zh-CN.7z
    - cd contents/update-koishipro
    - chmod +x *.sh
    - mono update.exe -ci
    - sed -i "/git/d" filelist.txt
    - cd ../..
    - aws s3 --endpoint=https://minio.mycard.moe:9000 sync --delete contents/ s3://nanahira/koishipro-another/contents/
  only:
    - another

upload_to_minio:
  stage: deploy
  dependencies:
    - pack_windows_zh-CN_7z
    #- pack_windows_zh-CN_gz
    #- pack_windows_zh-CN_zst
    - pack_linux_zh-CN_gz
    - pack_linuxarm_zh-CN_gz
    #- pack_linux_zh-CN_zst
    - pack_macos_zh-CN_gz
    #- pack_macos_zh-CN_zst
    - pack_windows_en-US_7z
    #- pack_windows_en-US_gz
    #- pack_windows_en-US_zst
    - pack_linux_en-US_gz
    - pack_linuxarm_en-US_gz
    #- pack_linux_en-US_zst
    - pack_macos_en-US_gz
    #- pack_macos_en-US_zst
    - pack_windows_dlc_zh-CN
    - pack_linux_dlc_zh-CN
    - pack_macos_dlc_zh-CN
    - pack_windows_dlc_en-US
    - pack_linux_dlc_en-US
    - pack_macos_dlc_en-US
  tags: 
    - linux
  script:
    - aws s3 --endpoint=https://minio.mycard.moe:9000 sync dist/ s3://nanahira-priv/koishipro/archive
  only:
    - tags
    - another

upload_mycard:
  stage: deploy
  dependencies:
    - pack_windows_dlc_zh-CN
    - pack_linux_dlc_zh-CN
    - pack_macos_dlc_zh-CN
    - pack_windows_dlc_en-US
    - pack_linux_dlc_en-US
    - pack_macos_dlc_en-US
  tags: 
    - linux
  variables:
    appVersion: $CI_COMMIT_TAG
  script: ./.ci/upload-packager.sh
  only:
    - tags

#docker:
#  stage: build
#  dependencies: []
#  tags: 
#    - docker
#  script:
#    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
#    - apk add --no-cache git
#    - git submodule update --init
#    - docker build --pull -t $CONTAINER_TEST_IMAGE .
#    - docker push $CONTAINER_TEST_IMAGE
