stages:
  - pack
  - deploy

variables:
  GIT_DEPTH: "1"

redtext:
  stage: pack
  dependencies: []
  tags:
    - linux
  image: git-registry.mycard.moe/nanahira/srvpro:lite
  variables:
    DATABASE_FILE: expansions/pre-release.cdb
  script:
    - apt update && apt -y install sqlite3
    - mv script expansions/
    - mv /ygopro-server/ygopro .
    - mkdir dist
    - cd ygopro
    - mv ../expansions .
    - echo "# Result of redtext test of commit $CI_COMMIT_SHA"
    - echo "select id from datas where type != 0x10 and (type & 0x4000) = 0;" | sqlite3 $DATABASE_FILE | xargs -I {} ./ygopro {} 2>&1 | tee ../dist/redtext.txt
    - cd ..
    - exit $(cat dist/redtext.txt | wc -l)

pack:
  stage: pack
  dependencies: []
  tags: 
    - linux
  script:
    - mv field pics/
    - mv pics expansions/
    - mv script expansions/
    - mkdir dist
    - cd expansions
    - 7z a -mx9 ../dist/ygopro-pre.zip
    - cd ..
    - mv dist/ygopro-pre.zip dist/ygopro-pre.ypk
  artifacts:
    paths:
      - dist

server:
  stage: pack
  dependencies: []
  tags: 
    - linux
  script:
    - mv script expansions/
    - mv expansions dist
  artifacts:
    paths:
      - dist

deploy:
  stage: deploy
  dependencies:
    - pack
  tags: 
    - linux
  variables:
    username: $MYCARD_USERNAME
    password: $MYCARD_PASSWORD
    appVersion: $CI_COMMIT_TAG
  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 ygopro-pre ../dist || true
    #- ./index.sh makeBuild ygopro-pre-koishi ../dist || true
    - ./index.sh makeBuild ygopro-pre-pro2 ../dist || true
    - cd ..
  only:
    - tags

.deploy_to_server:
  stage: deploy
  dependencies:
    - server
  tags:
    - linux
  script:
    - apt update && apt -y install openssh-client rsync coreutils
    - mkdir ~/.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 ./dist/ $SERVER_USER@$SERVER_HOST:~/ygopro-pre-live
  only:
    - master

deploy_to_tiramisu:
  extends: .deploy_to_server
  variables:
    SERVER_HOST: tiramisu.mycard.moe
    SERVER_USER: mycard

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