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

variables:
  GIT_DEPTH: "1"

mat_common:
  stage: prepare
  tags: 
    - linux
  script:
    # lua
    - wget -O - https://cdn01.moecube.com/ygopro-build-materials/lua-5.3.5.tar.gz | tar zfx -
    - mv lua-5.3.5/src lua
    # sqlite3
    - wget -O - https://cdn01.moecube.com/ygopro-build-materials/sqlite-autoconf-3360000.tar.gz | tar zfx -
    - mv sqlite-autoconf-3360000 sqlite3
    # freetype
    - wget -O - https://cdn01.moecube.com/ygopro-build-materials/freetype-2.10.4.tar.gz | tar zfx -
    - mv freetype-2.10.4 freetype
    # premake
    - cp -rf premake/* .;
  artifacts:
    paths:
      - lua
      - freetype
      - sqlite3

mat_irrklang:
  stage: prepare
  tags:
    - linux
  script:
    - apt update; apt -y install git
    - mkdir -p ~/.ssh; chmod 700 ~/.ssh; echo "$NANAHIRA_SSH_KEY" | base64 --decode > ~/.ssh/id_rsa; chmod 600 ~/.ssh/id_rsa
    - ssh-keyscan git.mycard.moe >> ~/.ssh/known_hosts
    - git clone --depth=1 git@git.mycard.moe:nanahira/irrklang
    #- mv -f irrklang/plugins/ikpmp3 .
  artifacts:
    paths:
      - irrklang
      - ikpmp3

mat_linux:
  stage: prepare
  tags:
    - linux
  script:
    - apt update; apt -y install git wget tar
    - git clone --depth=1 https://code.mycard.moe/mycard/irrlicht irrlicht_linux
    - wget -O - https://cdn01.moecube.com/ygopro-build-materials/premake-5.0.0-alpha14-linux.tar.gz | tar zfx -
    - env PROCESSOR_COUNT=$(nproc) ./.ci/libevent-prebuild.sh
  artifacts:
    paths:
      - premake5
      - irrlicht_linux
      - libevent-stable

mat_macos_platform_x86:
  stage: prepare
  tags:
    - macos
  variables:
    PROCESSOR_COUNT: '4'
  script: ./.ci/libevent-prebuild.sh
  artifacts:
    paths:
      - libevent-stable

mat_macos_platform_m1:
  stage: prepare
  tags:
    - linux
  script:
    - wget -O - https://cdn01.moecube.com/ygopro-build-materials/libevent-m1mac.tar.gz | tar zfx -
  artifacts:
    paths:
      - libevent-stable


mat_macos_irrklang_patched:
  stage: build
  tags:
    - linux
  dependencies: []
  script:
    - wget -O - https://cdn01.moecube.com/ygopro-build-materials/libirrklang-patched-dylib.tar.gz | tar zfx -
  artifacts:
    paths:
      - libirrklang-patched.dylib

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-alpha14-macosx.tar.gz | tar zfx -
    - wget -O - https://cdn01.moecube.com/ygopro-build-materials/libirrklang-patched-dylib.tar.gz | tar zfx -
    - mkdir irrlicht
    - cd irrlicht
    - wget -O - https://cdn01.moecube.com/ygopro-build-materials/irrlicht-mycard-mac.tar.gz | tar zfx -
    - cd ..
  artifacts:
    paths:
      - libirrklang-patched.dylib
      - 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-alpha14-windows.zip
    - 7z x -y premake-5.0.0-alpha14-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 ; cp -rf event/WIN32-Code/* event/include
    # irrlicht
    - wget https://cdn01.moecube.com/ygopro-build-materials/irrlicht-1.8.4.zip
    - 7z x -y irrlicht-1.8.4.zip
    - mv premake/irrlicht .
    - mv irrlicht-1.8.4/source/Irrlicht irrlicht/src
    - mv irrlicht-1.8.4/include irrlicht/include
  artifacts:
    paths:
      - premake5.exe
      - event
      - irrlicht

exec_windows:
  stage: build
  tags:
    - vs
  dependencies:
    - mat_common
    - mat_irrklang
    - mat_windows
  variables:
    irrklang_pro: '1'
    USE_IRRKLANG: "1"
  cache:
    paths:
      - bin/
      - obj/
  script:
    - git submodule update --init
    - bash -c 'cp -rf premake/* .'
    - cmd /C "patch -p0 < irrlicht\irrlicht.patch"
    - '.\premake5.exe vs2019'
    - cmd /c '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\msbuild.exe" build\ygo.sln /m /p:Configuration=Release'
    - copy bin\release\ygopro.exe .
  artifacts:
    paths:
      - ygopro.exe

exec_linux:
  stage: build
  tags:
    - linux
  variables:
    YGOPRO_BUILD_ALL: '1'
    USE_IRRKLANG: "1"
  dependencies:
    - mat_common
    - mat_irrklang
    - mat_linux
  cache:
    paths:
      - bin/
      - obj/
  script:
    - apt update; apt -y install git build-essential libgl1-mesa-dev libglu-dev libxxf86vm-dev
    - git submodule update --init
    - mkdir lib
    - cp -rf irrklang/bin/linux-gcc-64/libIrrKlang.so ./lib/
    - env YGOPRO_LIBEVENT_STATIC_PATH=$PWD/libevent-stable ./premake5 gmake
    - cd build
    - make config=release -j$(nproc)
    - cd ..
    - mv bin/release/ygopro .
    - strip ygopro
  artifacts:
    paths:
      - ygopro
      - lib

exec_macos_platform_x86:
  stage: build
  tags:
    - macos
  dependencies:
    - mat_irrklang
    - mat_macos
    - mat_common
    - mat_macos_platform_x86
  variables:
    USE_IRRKLANG: "1"
    TARGET_PATFORM: x86
  cache:
    paths:
      - bin/
      - obj/
  script:
    - ./.ci/exec-macos-platform.sh
    - install_name_tool -change /usr/local/lib/libirrklang.dylib @executable_path/../Frameworks/libirrklang.dylib ./ygopro-platforms/ygopro-platform-$TARGET_PATFORM
  artifacts:
    paths:
      - ygopro-platforms
      
exec_macos_platform_m1:
  stage: build
  tags:
    - macos
  dependencies:
    - mat_macos
    - mat_common
    - mat_macos_platform_m1
  variables:
    TARGET_PATFORM: m1
    YGOPRO_TARGET_ARM: '1'
  cache:
    paths:
      - bin/
      - obj/
  script: ./.ci/exec-macos-platform.sh
  artifacts:
    paths:
      - ygopro-platforms

exec_macos:
  stage: combine
  tags:
    - macos
  dependencies:
    - exec_macos_platform_x86
    - exec_macos_platform_m1
    - mat_macos_irrklang_patched
  script:
    - mkdir -p ygopro.app/Contents/MacOS ygopro.app/Contents/Frameworks;
    - lipo -create -output ygopro.app/Contents/MacOS/ygopro ygopro-platforms/ygopro-platform-x86 ygopro-platforms/ygopro-platform-m1;
    # do some special things for irrklang
    - mv ./libirrklang-patched.dylib ygopro.app/Contents/Frameworks/libirrklang.dylib 
    #- install_name_tool -change /usr/local/lib/libirrklang.dylib @executable_path/../Frameworks/libirrklang.dylib ygopro.app/Contents/MacOS/ygopro
    #- dylibbundler -x ygopro.app/Contents/MacOS/ygopro -b -d ygopro.app/Contents/Frameworks/ -p @executable_path/../Frameworks/ -cd -i @executable_path/../Frameworks;
    #- strip ygopro.app/Contents/MacOS/ygopro;
    - mkdir ygopro.app/Contents/Resources;
    - mv premake/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
#    - master

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 ..
    # locales
    - git clone --depth=1 https://code.mycard.moe/mycard/ygopro-database
    - mv ygopro-database/locales .
  artifacts:
    paths:
      - deck
      - single
      - sound
      - fonts
      - locales
  only:
    - tags
    - master

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
    - master

assets_windows:
  stage: prepare
  tags:
    - linux
  image: python
  script:
    - apt update; apt -y install wget p7zip-full
    - pip install -U -i https://mirrors.aliyun.com/pypi/simple/ awscli
    - wget https://cdn01.moecube.com/windbot/WindBot.7z
    - 7z x -y WindBot.7z
    - aws s3 --endpoint=https://minio.mycard.moe:9000 sync s3://mycard/koishipro/contents/update-koishipro/ ./update-koishipro
    - rm -rf update-koishipro/.git || true
  artifacts:
    paths:
      - bot.conf
      - WindBot
      - Bot.exe
      - update-koishipro
  only:
    - tags
    - master

assets_zh-CN:
  stage: build
  tags:
    - linux
  script: ./.ci/assets-locale.sh
  variables:
    TARGET_LOCALE: zh-CN
  artifacts:
    paths:
      - pics
      - cards.cdb
      - strings.conf
      - bot.conf
  only:
    - tags
    - master

assets_en-US:
  stage: build
  tags:
    - linux
  script: ./.ci/assets-locale.sh
  variables:
    TARGET_LOCALE: en-US
  artifacts:
    paths:
      - pics
      - cards.cdb
      - strings.conf
      - bot.conf
  only:
    - tags
    - master

pack_windows_zh-CN:
  stage: pack
  tags:
    - linux
  dependencies:
    - assets
    - assets_windows
    - exec_windows
    - assets_zh-CN
  script: ./.ci/pack-windows.sh
  variables:
    TARGET_LOCALE: zh-CN
  artifacts:
    paths:
      - dist
  only:
    - tags
    - master

pack_windows_zh-CN:
  stage: pack
  tags:
    - linux
  dependencies:
    - assets
    - assets_windows
    - exec_windows
    - assets_zh-CN
  script: ./.ci/pack-windows.sh
  variables:
    TARGET_LOCALE: zh-CN
  artifacts:
    paths:
      - dist
  only:
    - tags
    - master

pack_windows_en-US:
  stage: pack
  tags:
    - linux
  dependencies:
    - assets
    - assets_windows
    - exec_windows
    - assets_en-US
  script: ./.ci/pack-windows.sh
  variables:
    TARGET_LOCALE: en-US
  artifacts:
    paths:
      - dist
  only:
    - tags
    - master

pack_linux_zh-CN:
  stage: pack
  tags:
    - linux
  dependencies:
    - assets
    - assets_nonwindows
    - exec_linux
    - assets_zh-CN
  script: ./.ci/pack-linux.sh
  variables:
    TARGET_LOCALE: zh-CN
  artifacts:
    paths:
      - dist
  only:
    - tags
    - master

pack_linux_en-US:
  stage: pack
  tags:
    - linux
  dependencies:
    - assets
    - assets_nonwindows
    - exec_linux
    - assets_en-US
  script: ./.ci/pack-linux.sh
  variables:
    TARGET_LOCALE: en-US
  artifacts:
    paths:
      - dist
  only:
    - tags
    - master

pack_macos_zh-CN:
  stage: pack
  tags:
    - linux
  dependencies:
    - assets
    - assets_nonwindows
    - exec_macos
    - assets_zh-CN
  script: ./.ci/pack-macos.sh
  variables:
    TARGET_LOCALE: zh-CN
  artifacts:
    paths:
      - dist
  only:
    - tags
    - master

pack_macos_en-US:
  stage: pack
  tags:
    - linux
  dependencies:
    - assets
    - assets_nonwindows
    - exec_macos
    - assets_en-US
  script: ./.ci/pack-macos.sh
  variables:
    TARGET_LOCALE: en-US
  artifacts:
    paths:
      - dist
  only:
    - tags
    - master

upload_contents:
  stage: deploy
  dependencies:
    - pack_windows_zh-CN
  tags: 
    - linux
  image: python
  script:
    - apt update; apt -y install mono-complete p7zip-full
    - pip install -U -i https://mirrors.aliyun.com/pypi/simple/ awscli
    - 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://mycard/koishipro/contents/
  only:
    - master

upload_to_minio:
  stage: deploy
  dependencies:
    - pack_windows_zh-CN
    - pack_linux_zh-CN
    - pack_macos_zh-CN
    - pack_windows_en-US
    - pack_linux_en-US
    - pack_macos_en-US
  tags: 
    - linux
  script:
    - aws s3 --endpoint=https://minio.mycard.moe:9000 sync dist/ s3://mycard/koishipro/archive
  only:
    - tags
    - master
