stages:
  - prepare
  - prepare2
  - pack
  - deploy
  - deploy2

variables:
  GIT_DEPTH: "1"

fail_if_banned_files:
  stage: prepare
  dependencies: []
  tags:
    - linux
  script: |
    banned_files=(script/procedure.lua script/utility.lua script/constant.lua)
    for file in "${banned_files[@]}"; do
      if [ -f "$file" ]; then
        echo "Banned file found: $file"
        exit 1
      fi
    done

merge_databases:
  stage: prepare
  dependencies: []
  tags:
    - linux
  script:
    - apt update --allow-insecure-repositories && apt -y install sqlite3
    - ls -1 *.cdb | sed -e '/test-update.*\.cdb$/d' -e '/script-fix.*\.cdb$/d' | xargs -I '{}' sqlite3 '{}' .dump | sqlite3 test-release.cdb || true
    - ls -1 test-update*.cdb | xargs -I '{}' sqlite3 '{}' .dump | sqlite3 test-update.cdb || true
  artifacts:
    paths:
      - test-release.cdb
      - test-update.cdb

add_script_diff:
  stage: prepare
  dependencies: []
  tags:
    - linux
  script:
    - git clone https://code.moenext.com/mycard/ygopro-scripts-888
    - cd ygopro-scripts-888
    - git remote add stable https://github.com/mycard/ygopro-scripts
    - git fetch stable refs/heads/master
    # now FETCH_HEAD is the commit of stable/master
    - |
      copy_out() {
        target=../script/$1
        if [ -f "$target" ]; then
          echo "File $target already exists, skipping copy."
        elif [ -f "$1" ]; then
          echo "Copying $1 to $target"
          cp "$1" ../script/
        else
          echo "File $1 not found, skipping."
        fi
      }
      for file in $(git diff --name-only --diff-filter=A FETCH_HEAD -- '*.lua'); do
        copy_out "$file"
      done
      for file in $(git diff --name-only --diff-filter=M FETCH_HEAD -- '*.lua'); do
        if [ -f "$file" ]; then
          current_time=$(git log -1 --format="%ct" HEAD -- "$file")
          stable_time=$(git log -1 --format="%ct" FETCH_HEAD -- "$file")
          if [ -n "$current_time" ] && [ -n "$current_time" ] && [ "$current_time" -lt "$stable_time" ]; then
            echo "File $file is older than stable/master, skipping copy."
          else
            copy_out "$file"
          fi
        fi
      done
    - cd ..
  artifacts:
    paths:
      - script

.json:
  stage: prepare2
  dependencies:
    - merge_databases
  tags:
    - linux
  image: git-registry.moenext.com/mycard/ygopro-cdb-descgen:master-x86
  variables:
    YGOPRO_PIC_URL_PREFIX: 'https://cdntx.moecube.com/ygopro-super-pre/data/pics/'
    YGOPRO_PIC_URL_SUFFIX: '.jpg?version=15.13'
    FIELDS: 'name,desc,overallString,picUrl'
    GIT_DEPTH: '1000'
    OUTPUT_FILENAME: test-release.json
    REF_COMMIT: '18.14' # 更新的时候改这里，修改成上一个tag的版本号
  script:
    - apt update;apt -y install wget
    - currentPath=$PWD
    - cd /usr/src/app
    - wget -O strings.conf https://code.moenext.com/mycard/ygopro-database/-/raw/master/locales/zh-CN/strings.conf
    - env GIT_PATH=$currentPath node dist $currentPath/test-release.cdb ./strings.conf $currentPath/test-strings.conf > $currentPath/$OUTPUT_FILENAME
    - cd $currentPath
  artifacts:
    paths:
      - test-release.json

json:
  extends: .json

json2:
  extends: .json
  variables:
    FIELDS: 'id,name,desc,overallString,picUrl,createTime,createCommit,updateTime,updateCommit,created,updated'
    OUTPUT_FILENAME: test-release-v2.json
  artifacts:
    paths:
      - test-release-v2.json

redtext:
  stage: prepare2
  dependencies:
    - add_script_diff
  tags:
    - linux
  image: git-registry.moenext.com/nanahira/srvpro:lite
  script:
    - apt update && apt -y install sqlite3
    - mv /ygopro-server/ygopro .
    - mkdir dist
    - cd ygopro
    - rm -rf expansions
    - ln -s ../ ./expansions
    - ls -1 ../*.cdb | xargs -I '{}' sqlite3 '{}' "select id from datas where type != 0x11 and (type & 0x4000) = 0;" | xargs -I {} -P $(nproc) bash -c './ygopro {} 2>&1 | tee ../redtext-{}.txt'
    - cd ..
    - cat redtext-*.txt > redtext.txt
    # if there are any lines in redtext.txt fail the job
    - exit $(wc -l < redtext.txt)

neos:
  stage: pack
  dependencies:
    - merge_databases
  tags: 
    - linux
  script:
    - apt update && apt -y install sqlite3
    - mkdir neos
    - sqlite3 test-release.cdb .dump >> ygopro-super-pre-neos.sql
    - sqlite3 test-update.cdb .dump >> ygopro-super-pre-neos.sql
    - wget -O cards.cdb https://code.moenext.com/mycard/ygopro-database/-/raw/master/locales/zh-CN/cards.cdb
    - sqlite3 cards.cdb .dump >> ygopro-super-pre-neos.sql
    - cat ygopro-super-pre-neos.sql | sqlite3 neos/ygopro-super-pre-neos.cdb || true
  artifacts:
    paths:
      - neos

pack:
  stage: pack
  dependencies:
    - merge_databases
    - add_script_diff
    - json
    - json2
  tags: 
    - linux
  script:
    - mkdir -p data archive
    - mv test-release.cdb data/
    - mv test-update.cdb data/
    - mv pics data/
    - mv script data/
    - mv *.conf data/
    - mv pack data/
    - mv *.ini data/
    - mv test-release.json data/
    - mv test-release-v2.json data/
    - git show -s --format=%ct $CI_COMMIT_SHA > data/version.txt
    # 仅在 tag 构建时写入 latest-tag.txt
    - |
      if [ -n "$CI_COMMIT_TAG" ]; then
        echo "https://cdntx.moecube.com/ygopro-super-pre/archive/ygopro-super-pre-${CI_COMMIT_TAG}.ypk" > data/latest-tag.txt
      fi
    - cd data
    - 7z a -mx9 ../archive/ygopro-super-pre.zip pics script pack *.cdb *.conf *.ini
    - cd ..
    - mv archive/ygopro-super-pre.zip archive/ygopro-super-pre.ypk
    - cp archive/ygopro-super-pre.ypk archive/ygopro-super-pre-$CI_COMMIT_REF_NAME.ypk
  artifacts:
    paths:
      - data
      - archive

server:
  stage: pack
  dependencies:
    - merge_databases
    - add_script_diff
  tags: 
    - linux
  script:
    - mkdir server-dist
    - mv test-release.cdb server-dist
    - mv test-update.cdb server-dist
    - mv *.conf server-dist
    - mv script server-dist
    # merge ygopro-pre-data
    # - git clone --depth=1 https://code.moenext.com/mycard/ygopro-pre-data
    # - mv ygopro-pre-data/expansions/* server-dist/
    # - mv ygopro-pre-data/script/* server-dist/script/
  artifacts:
    paths:
      - server-dist

server_clear:
  extends: server
  dependencies:
    - merge_databases

.minio:
  stage: deploy
  dependencies:
    - pack
  tags: 
    - linux
  script:
    - aws s3 --endpoint=https://minio.moenext.com:9000 sync $uploadDir/ s3://mycard/ygopro-super-pre/$uploadDir

upload_archive:
  extends: .minio
  variables:
    uploadDir: archive
  only:
    - tags

upload_data:
  extends: .minio
  stage: deploy2
  variables:
    uploadDir: data
  only:
    - tags

upload_neos:
  extends: .minio
  variables:
    uploadDir: neos
  dependencies:
    - neos
  only:
    - master

upload_version_json:
  extends: .minio
  script:
    - aws s3 --endpoint=https://minio.moenext.com:9000 cp data/version.txt s3://mycard/ygopro-super-pre/versions/$CI_COMMIT_REF_NAME/version.txt
    - aws s3 --endpoint=https://minio.moenext.com:9000 cp data/test-release.json s3://mycard/ygopro-super-pre/versions/$CI_COMMIT_REF_NAME/test-release.json
    - aws s3 --endpoint=https://minio.moenext.com:9000 cp data/test-release-v2.json s3://mycard/ygopro-super-pre/versions/$CI_COMMIT_REF_NAME/test-release-v2.json
  only:
    - tags
    - master

deploy:
  stage: deploy
  dependencies:
    - pack
  tags: 
    - linux
  variables:
    username: $MYCARD_USERNAME
    password: $MYCARD_PASSWORD
    appVersion: $CI_COMMIT_REF_NAME
  script:
    - apt update; apt -y install tar zstd curl jq
    - git clone https://code.moenext.com/mycard/mycard-package-script
    - cd mycard-package-script
    - ./index.sh makeBuild ygopro-super-pre ../data || true
    - ./index.sh makeBuild ygopro2-super-pre ../data || true
    - ./index.sh makeBuild mdpro3-super-pre ../data || true
    - cd ..
  only:
    # - master
    - tags

.deploy_to_server:
  stage: deploy
  tags:
    - linux
  dependencies:
    - server
  variables:
    SERVER_PATH: ~/ygopro-super-pre/expansions
  script:
    - apt update && apt -y install openssh-client rsync coreutils
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan $SERVER_HOST >> ~/.ssh/known_hosts
    - echo $NANAHIRA_SSH_KEY | base64 --decode > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/*
    - rsync -4cavzP --exclude=pics --delete ./server-dist/ $SERVER_USER@$SERVER_HOST:$SERVER_PATH
  only:
    - master
    - tags

deploy_to_tiramisu:
  extends: .deploy_to_server
  variables:
    SERVER_HOST: tiramisu.moenext.com
    SERVER_USER: mycard

deploy_to_koishi:
  extends: .deploy_to_server
  variables:
    SERVER_HOST: koishi.momobako.com
    SERVER_USER: nanahira

deploy_to_koishi_clear:
  extends: deploy_to_koishi
  dependencies:
    - server_clear
  variables:
    SERVER_PATH: ~/ygopro-super-pre-clear/expansions
