stages:
  - package

Update-AUR:
  image: archlinux/base
  stage: package
  only:
    - /^v(\d+\.)*\d+$/
  script:
    - cd ./pkg/aur

    - _version=$(echo $CI_COMMIT_REF_NAME | cut -c 2-)
    - echo "Pushing'v$_version'"

    - sourceURL=https://gitlab.com/ykkzde/posy-cursors/-/archive/v$_version/cursorsPosy-v$_version.tar.gz?path=themes
    - 'echo "Source: $sourceURL"'
    - echo "Checking sha256sum checksums"
    - 'sourceSHA=$(curl -sSL "$sourceURL" | sha256sum | cut -d" " -f1)'

    # Update PKGBUILD version, source URL and sha256sum
    - echo "Updating PKGBUILDS with release information..."
    - sed "s/^pkgver=.*\$/pkgver=$_version/" -i posy-cursors/PKGBUILD
    - sed "s/^source=(\"\(.*\)::.*\").*\$/source=(\"\1::$(echo $sourceURL | sed 's/\//\\\//g')\")/" -i posy-cursors/PKGBUILD
    - sed "s/^sha256sums=.*\$/sha256sums=('$sourceSHA')/" -i posy-cursors/PKGBUILD

    - echo "Installing Arch"
    - pacman -Syu --noconfirm sudo base-devel openssh git openssl

    # Makepkg and .SRCINFO
    - cd posy-cursors/
    - echo "makepkg"
    - sudo -u nobody makepkg -c
    - sudo -u nobody makepkg --printsrcinfo > .SRCINFO
    - cd ..

    # Configure SSH
    - mkdir -p /root/.ssh
    - cp ./aur.pub /root/.ssh/id_rsa.pub
    - echo "${AUR_PRIVATE}" | base64 -d > /root/.ssh/id_rsa
    - echo "Host aur.archlinux.org" >> /root/.ssh/config
    - echo "  IdentityFile /root/.ssh/aur" >> /root/.ssh/config
    - echo "  User aur" >> /root/.ssh/config
    - chmod 600 /root/.ssh/{id_rsa*,config} 
    - eval `ssh-agent -s`
    - echo -en "${SSH_PASSPHRASE}" | ssh-add /root/.ssh/id_rsa
    - ssh-keyscan -H aur.archlinux.org >> /root/.ssh/known_hosts
    - git config --global user.name "Dmitry Porunov"
    - git config --global user.email "dmitry@ykkz.de"

    # Clone, commit and push
    - git clone ssh://aur@aur.archlinux.org/posy-cursors.git aur-posy-cursors
    - cd aur-posy-cursors
    - cp ../posy-cursors/{PKGBUILD,.SRCINFO} ./
    - git add PKGBUILD .SRCINFO
    - git commit -m "Release v$_version"
    - git push
    - cd ..
