pages:
  stage: deploy
  before_script:
  -  eval $(ssh-agent -s)
  -  echo "${RUB_SSH_PRIVATE_KEY}" | tr -d '\r' | ssh-add -
  script:
  - rm -rf public/en/medien
  - cp -R slides public 
  - cp -R data public
  # Debugging SSH connection:
  # - echo "${RUB_SSH_PRIVATE_KEY}" > temporary_key
  # - chmod 600 temporary_key
  # - ssh -v -i temporary_key ${RUB_SSH_USER}@${RUB_SSH_HOST}
  # - rm -f temporary_key
  # Actual upload command: 
  - lftp -u ${RUB_SSH_USER}, -e "set sftp:connect-program 'ssh -a -x -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'; mirror -R ${CI_PROJECT_DIR}/public/ ${RUB_TARGET_DIR}/; quit" sftp://${RUB_SSH_HOST}
  artifacts:
    paths:
    - public
  only:
  - main
  tags:
  - pages

fetch_calendar:
  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - echo "$GIT_SSH_PRIV_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - export GIT_SSH_COMMAND="ssh -v -o StrictHostKeyChecking=no"
  script:
    - git init
    - git fetch origin
    - git checkout main
    - git remote set-url origin git@git.noc.ruhr-uni-bochum.de:makerspace/homepage.git
    - git pull --rebase origin main
    - mkdir -p docs/data
    - mkdir -p public/data
    - curl -o docs/data/calendar.ics https://mail.exchange.ruhr-uni-bochum.de/owa/calendar/c35486a95ce0458084511f72258a0ae2@it-services.ruhr-uni-bochum.de/ebd8081528364481b4fe87794a3e8776659663570677531713/calendar.ics
    - cp docs/data/calendar.ics public/data/
    - git config user.email "makerspace@ruhr-uni-bochum.de"
    - git config user.name "Makerspace-GitLab CI"
    - git add docs/data/calendar.ics public/data/calendar.ics
    - git commit -m "Update calendar" || echo "No changes to commit"
    - git push origin main
    - cp -R slides public
  only:
    - schedules
  tags:
    - pages