stages:
  - prepare
  - generate
  - deploy
  - deploy2

variables:
  GIT_DEPTH: "1"
  POST_DEPTH: 10

prepare_mats:
  stage: prepare
  tags:
    - linux
  script:
    - mkdir mats
    - cd mats
    - git clone --depth=1 https://code.moenext.com/mycard/ygopro-database
    - wget -O pack.db https://cdn02.moecube.com:444/ygopro-card-list/pack.db
    - wget -O lflist.conf https://code.moenext.com/mycard/ygopro/-/raw/master/lflist.conf
    - cd ..
  artifacts:
    paths:
      - mats

npm_ci:
  stage: prepare
  tags:
    - linux
  script:
    - npm ci
    - npm run build
  artifacts:
    paths:
      - node_modules
      - build

.task:
  stage: generate
  dependencies:
    - prepare_mats
    - npm_ci
  tags: 
    - linux
  variables:
    LOCALE: zh-CN
    NPM_SCRIPT: start
    CARD_SYMBOL: '简体中文卡'
    FILE_SYMBOL: 'cn'
  script:
    - apt update ; apt -y install sqlite3 
    - ./ci-scripts/generate.sh
  artifacts:
    paths:
      - output

zh-CN:
  extends: .task

en-US:
  extends: .task
  variables:
    LOCALE: en-US


ja-JP:
  extends: .task
  variables:
    LOCALE: ja-JP

ko-KR:
  extends: .task
  variables:
    LOCALE: ko-KR

env408:
  extends: .task
  variables:
    LOCALE: zh-CN
    NPM_SCRIPT: start:subenv
    DATE: '2006-05-15'
    CARD_SYMBOL: '408环境'
    FILE_SYMBOL: 'env408'
    OVERRIDE_TEXT_DB_PATH: ./extras/2012.cdb

env706:
  extends: .task
  variables:
    LOCALE: zh-CN
    NPM_SCRIPT: start:subenv
    DATE: '2011-08-20'
    CARD_SYMBOL: '706环境'
    FILE_SYMBOL: 'env706'
    OVERRIDE_TEXT_DB_PATH: ./extras/2012.cdb

upload_to_minio:
  stage: deploy
  dependencies:
    - zh-CN
    - en-US
    - ja-JP
    - ko-KR
    - env408
    - env706
  tags: 
    - linux
  script:
    - aws s3 --endpoint=https://minio.mycard.moe:9000 sync --delete output/ s3://mycard/cn-database
  only:
    - master

.deploy_server:
  stage: deploy
  dependencies:
    - env408
  tags:
    - linux
  variables:
    OUTPUT_ENV: env408-zh-CN
    SERVER_IDENTIFIER: '408'
  script:
    - apt update && apt -y install openssh-client rsync coreutils
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan koishi.momobako.com >> ~/.ssh/known_hosts
    - echo $NANAHIRA_SSH_KEY | base64 --decode > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/*
    - rsync -4cavzP --delete output/$OUTPUT_ENV/expansions/ nanahira@koishi.momobako.com:~/ygopro-$SERVER_IDENTIFIER/expansions
  only:
    - master

deploy_server_env408:
  extends: .deploy_server

deploy_server_cn:
  extends: .deploy_server
  dependencies:
    - zh-CN
  variables:
    OUTPUT_ENV: cn-zh-CN
    SERVER_IDENTIFIER: 'cn'

deploy_server_env706:
  extends: .deploy_server
  dependencies:
    - env706
  variables:
    OUTPUT_ENV: env706-zh-CN
    SERVER_IDENTIFIER: '706'

.deploy_mycard:
  stage: deploy
  dependencies:
    - env408
  tags: 
    - linux
  variables:
    username: $MYCARD_USERNAME
    password: $MYCARD_PASSWORD
    appVersion: $CI_COMMIT_SHORT_SHA
    APP_ID: ygopro-408
    OUTPUT_ENV: env408-zh-CN
  script:
    - apt update; apt -y install tar zstd curl jq
    - git clone https://code.mycard.moe/mycard/mycard-package-script
    - cd mycard-package-script
    - ./index.sh makeBuild "$APP_ID" ../output/$OUTPUT_ENV || true
    - cd ..
  only:
    - master

deploy_mycard_env408:
  extends: .deploy_mycard

deploy_mycard2_env408:
  extends: .deploy_mycard
  stage: deploy2
  variables:
    APP_ID: ygopro2-408
