version: 2
jobs:
  build:
    docker:
      - image: alpine:3.10.2

    steps:
      - run:
          name: Install hugo
          command: |
            apk add wget
            wget -q https://github.com/gohugoio/hugo/releases/download/v0.58.3/hugo_0.58.3_Linux-64bit.tar.gz
            apk add tar
            tar xzf hugo_0.58.3_Linux-64bit.tar.gz
            chmod a+x hugo
            mv hugo /usr/local/bin/hugo

      - run:
          name: Create site
          command: hugo new site my-site

      - run:
          name: Clone the theme from the PR
          working_directory: my-site
          command: |
            apk add git
            git clone https://github.com/StefMa/hugo-fresh themes/hugo-fresh
      - run:
          working_directory: my-site/themes/hugo-fresh
          command: |
            set +e
            isPrIfGreaterThanZero=$(expr $CIRCLE_BRANCH : "pull")
            echo $isPrIfGreaterThanZero
            if [ "$isPrIfGreaterThanZero" -gt "0" ]; then
              echo "Hello World $CIRCLE_BRANCH"
              git fetch origin ${CIRCLE_BRANCH}/head:BRANCHNAME
              git checkout BRANCHNAME
            else
              echo "Hello World else $CIRCLE_BRANCH"
              git checkout $CIRCLE_BRANCH
            fi

      - run:
          name: Replace default config
          working_directory: my-site
          command: |
            set +e
            isPrIfGreaterThanZero=$(expr $CIRCLE_BRANCH : "pull")
            echo $isPrIfGreaterThanZero
            if [ "$isPrIfGreaterThanZero" -gt "0" ]; then
              branch=${CIRCLE_SHA1}
            else
              branch=${CIRCLE_BRANCH}
            fi
            rm config.toml
            apk add curl
            curl https://codeload.github.com/stefma/hugo-fresh/tar.gz/$branch | tar -xz --strip=2 hugo-fresh-$branch/exampleSite/
      - run:
          name: Replace baseUrl in config
          working_directory: my-site
          command: |
            apk add sed
            URL=hugo-fresh-$(echo $CIRCLE_BRANCH | tr _ - | tr / -)
            if [ $CIRCLE_BRANCH == "master" ]; then
              URL=hugo-fresh
            fi
            sed -i "s,http://something-fresh.org,https://${URL}.vercel.app,g" config.yaml
            cat config.yaml

      - run:
          name: Create hugo site
          working_directory: my-site
          command: hugo

      - run:
          name: Create now.json
          working_directory: my-site/public
          command: |
            URL=hugo-fresh-$(echo $CIRCLE_BRANCH | tr _ - | tr / -)
            if [ $CIRCLE_BRANCH == "master" ]; then
              URL=hugo-fresh
            fi
            echo "{ \"version\": 2, \"name\": \"hugo-fresh\", \"alias\": \"${URL}\", \"builds\": [{ \"src\": \"**\", \"use\": \"@now/static\" }] }" >> now.json

      - run:
          name: Install now CLI
          command: |
            apk add --update npm
            npm config set unsafe-perm true
            npm install -g now@16.3.1
      - run:
          name: Publish to now.sh
          working_directory: my-site/public
          command: now --prod --token ${NOW_TOKEN}
