stages:
  - build
  - deploy

# Don't have develop yet hehe
# develop-build-job:
#   stage: build
#   tags:
#     - hugo
#   script:
#     - echo "Building configuration"
#     - hugo
#   only:
#     - develop

main-build-job:
  stage: build
  # image: jojomi/hugo
  tags:
    - hugo
  before_script:
    - echo "Ititialising"
    - BIN_DIR=/bin
    - mkdir -p $BIN_DIR
    - echo "Installing hugo"
    - HUGO_VERSION=0.107.0
    - curl -LJO https://github.com/gohugoio/hugo/releases/download/v0.107.0/hugo_extended_${HUGO_VERSION}_Linux-64bit.tar.gz;
    - mkdir -p hugo_extended
    - tar -xvf hugo_extended_${HUGO_VERSION}_Linux-64bit.tar.gz -C hugo_extended;
    - mv hugo_extended/hugo $BIN_DIR
    - rm -rf hugo_extended*;
    - hugo version
  script:
    - echo "Building configuration"
    - hugo
    - ls
  artifacts:
    paths:
      - public/
  only:
    - main

deploy-job:
  stage: deploy
  when: manual
  tags:
    - hugo
  before_script:
  - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y)'
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" >> ~/.ssh/config'
  script:
    - tar zcf ../inkletblot-com.tar.gz ./public
    - scp -o 'ProxyJump "$Live_Server_User"@"$Live_Server_IP"' -o StrictHostKeyChecking=no ../inkletblot-com.tar.gz "$Live_Server_User"@"$Live_Server_Target":/var/www/html
    - ssh -J "$Live_Server_User"@"$Live_Server_IP" "$Live_Server_User"@"$Live_Server_Target" "rm -Rf /var/www/html/inkletblot-com_old && mv /var/www/html/inkletblot-com /var/www/html/inkletblot-com_old && mkdir /var/www/html/inkletblot-com && mkdir /var/www/html/inkletblot-com_build && tar zxf /var/www/html/inkletblot-com.tar.gz -C /var/www/html/inkletblot-com_build && mv /var/www/html/inkletblot-com_build/public/* /var/www/html/inkletblot-com/ && rm -Rf /var/www/html/inkletblot-com_build && chmod -R 755 /var/www/html/inkletblot-com && exit"
  only:
    - main
