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

variables:
  GIT_DEPTH: "1"

mat_common:
  stage: prepare
  tags: 
    - linux
  script:
    # lua
    - wget -O - https://mat-cacher.moenext.com/https://www.lua.org/ftp/lua-5.4.8.tar.gz | tar zfx -
    - mv lua-5.4.8 lua
    # sqlite3
    - wget -O - https://mat-cacher.moenext.com/https://www.sqlite.org/2025/sqlite-autoconf-3500100.tar.gz | tar zfx -
    - mv sqlite-autoconf-3500100 sqlite3
    # freetype
    # - wget -O - https://mat-cacher.moenext.com/https://downloads.sourceforge.net/freetype/freetype-2.13.3.tar.gz | tar zfx -
    # - mv freetype-2.13.3 freetype
    # event
    - wget -O - https://mat-cacher.moenext.com/https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz | tar zfx -
    - mv libevent-2.1.12-stable event
    # irrlicht
    - ./.ci/prepare-irrlicht.sh
    # miniaudio
    #- ./.ci/prepare-miniaudio.sh
    # premake
    - cp -rf premake/* .;
  artifacts:
    paths:
      - lua
      #- freetype
      - sqlite3
      - event
      - irrlicht
      #- 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
#    - ./.ci/prepare-irrlicht.sh
#  artifacts:
#    paths:
#      - irrlicht

mat_macos:
  stage: prepare
  tags:
    - linux
  script:
    - apt update; apt -y install wget tar
    - wget -O - https://cdn02.moecube.com:444/premake5-built/premake-5.0.0-beta7-macosx.tar.gz | tar zfx -
    - chmod +x premake5
  artifacts:
    paths:
      - premake5

mat_windows:
  stage: prepare
  tags:
    - linux
  script:
    - apt update; apt -y install wget tar patch p7zip-full
    # premake5.exe
    - wget https://cdn02.moecube.com:444/premake5-built/premake-5.0.0-beta7-windows.zip
    - 7z x -y premake-5.0.0-beta7-windows.zip
  artifacts:
    paths:
      - premake5.exe

._exec_build:
  stage: build
  variables:
    # NO_LUA_SAFE: '1' # on client no lua safe
    GIT_STRATEGY: clone
  #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
  before_script:
    - bash -c 'cp -rf premake/* . ; cp -rf resource/* .'
  artifacts:
    paths:
      - dist

exec_windows:
  extends: .exec_windows
  script:
    - '.\premake5.exe vs2019 --server-zip-support'
    - cmd /c '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\msbuild.exe" build\YGOPro.sln /m /p:Configuration=Release /p:Platform=x64'
    - mkdir dist
    - mkdir dist\windows
    - copy bin\release\x64\ygopro.exe dist\windows\ygopro.exe

exec_windows_pro2:
  extends: .exec_windows
  script:
    - '.\premake5.exe vs2019 --server-pro2-support'
    - cmd /c '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\msbuild.exe" build\YGOPro.sln /m /p:Configuration=Release /p:Platform=x64'
    - mkdir dist
    - mkdir dist\windows
    - copy bin\release\x64\AI.Server.exe dist\windows\AI.Server.exe

exec_windows_pro3:
  extends: .exec_windows
  script:
    - '.\premake5.exe vs2019 --server-pro3-support --log-in-chat'
    - cmd /c '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\msbuild.exe" build\YGOPro.sln /m /p:Configuration=Release /p:Platform=x64'
    - mkdir dist
    - mkdir dist\windows
    - copy bin\release\x64\ygoserver.dll dist\windows\ygoserver.dll

exec_windows_mdpro2:
  extends: .exec_windows
  script:
    - '.\premake5.exe vs2019 --server-pro3-support --log-in-chat --server-pro2-support'
    - cmd /c '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\msbuild.exe" build\YGOPro.sln /m /p:Configuration=Release /p:Platform=x64'
    - mkdir dist
    - mkdir dist\windows
    - copy bin\release\x64\ygoserver.dll dist\windows\ygoserver-mdpro2.dll

.exec_unix_common:
  extends: ._exec_build
  variables:
    RELEASE_DIR: ''
    TARGET_FILE: ygopro
    PREMAKE5_BIN: premake5
    BUILD_EVENT: '1'
    BUILD_IRRLICHT: '1'
    BUILD_SQLITE: '1'
  script:
    - ./.ci/configure-libevent.sh
    - $PREMAKE5_BIN gmake
    - cd build
    - make config=release -j$(nproc)
    - cd ..
    - mkdir -p dist/$RELEASE_DIR
    - mv bin/release/$TARGET_FILE dist/$RELEASE_DIR/$TARGET_FILE
    - |
      if [ "$TARGET_FILE" = "ygopro" ]; then
        strip dist/$RELEASE_DIR/$TARGET_FILE
      fi
  artifacts:
    paths:
      - dist

.exec_linux_common:
  extends: .exec_unix_common
  tags:
    - linux
  image: git-registry.moenext.com/mycard/docker-ygopro-builder
  dependencies:
    - mat_common
    #- mat_linux
    - mat_submodules

.exec_linux:
  extends: .exec_linux_common
  variables:
    BUILD_SQLITE: '1'
    SERVER_ZIP_SUPPORT: '1'
    RELEASE_DIR: linux-x64

.exec_debian:
  extends: .exec_linux_common
  variables:
    LUA_DEB: '1'
    RELEASE_DIR: debian-x64
  before_script:
    - apt update; apt -y install git build-essential liblua5.4-dev libsqlite3-dev libevent-dev

.use_arm:
  tags:
    - arm

.use_pro3:
  variables:
    SERVER_PRO3_SUPPORT: '1'
    LOG_IN_CHAT: '1'

.use_pro3_linux:
  extends: .use_pro3
  variables:
    TARGET_FILE: libygoserver.so

.use_pro3_macos:
  extends: .use_pro3
  variables:
    TARGET_FILE: libygoserver.dylib

exec_linux:
  extends: .exec_linux
  tags:
    - noavx2

exec_linux_pro3:
  extends:
    - .exec_linux
    - .use_pro3_linux
  tags:
    - avx2

exec_debian:
  extends: .exec_debian

exec_linuxarm:
  extends:
    - .exec_linux
    - .use_arm
  variables:
    RELEASE_DIR: linux-arm64

exec_linuxarm_pro3:
  extends:
    - .exec_linux
    - .use_arm
    - .use_pro3_linux
  variables:
    RELEASE_DIR: linux-arm64

exec_debianarm:
  extends:
    - .exec_debian
    - .use_arm
  variables:
    RELEASE_DIR: debian-arm


.exec_macos_platform:
  extends: .exec_unix_common
  dependencies:
    - mat_common
    - mat_macos
    - mat_submodules
  variables:
    PREMAKE5_BIN: ./premake5
    BUILD_SQLITE: '1'
    BUILD_EVENT: '1'
    SERVER_ZIP_SUPPORT: '1'
  before_script:
    - rm -rf sqlite3/VERSION sqlite3/version

exec_macos_x64:
  extends: .exec_macos_platform
  tags:
    - macos
  variables:
    RELEASE_DIR: macos-x64

exec_macos_arm64:
  extends: .exec_macos_platform
  tags:
    - macos-m1
  variables:
    RELEASE_DIR: macos-arm64

exec_macos_x64_pro3:
  extends:
    - .exec_macos_platform
    - .use_pro3_macos
  tags:
    - macos
  variables:
    RELEASE_DIR: macos-x64

exec_macos_arm64_pro3:
  extends:
    - .exec_macos_platform
    - .use_pro3_macos
  tags:
    - macos-m1
  variables:
    RELEASE_DIR: macos-arm64

exec_macos:
  stage: combine
  tags:
    - macos
  dependencies:
    - exec_macos_x64
    - exec_macos_arm64
    - exec_macos_x64_pro3
    - exec_macos_arm64_pro3
  script:
    - mkdir -p dist/macos
    - lipo -create -output dist/macos/ygopro dist/macos-x64/ygopro dist/macos-arm64/ygopro
    - lipo -create -output dist/macos/libygoserver.dylib dist/macos-x64/libygoserver.dylib dist/macos-arm64/libygoserver.dylib
    - rm -rf dist/macos-x64 dist/macos-arm64
  artifacts:
    paths:
      - dist

#win_server_pack:
#  stage: pack
#  dependencies:
#    - exec_windows
#  tags:
#    - linux
#  script:
#    - apt update; apt -y install p7zip-full wget
#    - wget https://nodejs.org/dist/v14.17.0/node-v14.17.0-win-x64.7z

upload_to_minio:
  stage: deploy
  dependencies:
    - exec_windows
    - exec_linux
    - exec_linuxarm
    - exec_windows_pro3
    - exec_windows_pro2
    - exec_windows_mdpro2
    - exec_linux_pro3
    - exec_linuxarm_pro3
    - exec_debian
    - exec_debianarm
    - exec_macos
  tags: 
    - linux
  script:
    - aws s3 --endpoint=https://minio.mycard.moe:9000 sync --delete dist/ s3://mycard/koishipro/server
  only:
    - server
    - tags
