#!/bin/sh

# Clone the source repository
rm -rf source_data
git clone https://github.com/martenson/disposable-email-domains.git source_data && \
# Create what would be generated by source_data
python bin/parse_source_data > disposable_email_domains/__init__.py && \

# See if anything has changed
git diff --exit-code

CHANGES=$?

if [ $TRAVIS_EVENT_TYPE = 'cron' ] && [ $CHANGES != 0 ]; then
    # Git setup
    git config --global user.email 'dee-eye@users.noreply.github.com'
    git config --global user.name "Dee Eye"
    git remote rm origin
    git remote add origin https://dee-eye:$OAUTH_TOKEN@github.com/di/disposable-email-domains.git

    # Bump the version
    pip install bump
    VERSION=`bump`

    # Add changes, push and make a PR
    git checkout -b version-$VERSION
    git commit -am "Version $VERSION"
    git push origin HEAD
    curl --show-error --fail -u "dee-eye:$OAUTH_TOKEN" --data '{"title": "'"Version $VERSION"'", "head": "'"version-$VERSION"'", "base": "master"}' https://api.github.com/repos/di/disposable-email-domains/pulls
    exit $?
else
    exit $CHANGES
fi
