#!/bin/bash

# Starting the script
echo "Starting aptRepoUpdater authored by Abishek V Ashok with love for FOSSASIA"
echo "..."

# Create the new directory to work in, these will get cleaned by Travis so no worries
mkdir working_root
cd working_root

# Adding configurations for git
git config --global user.email "no-reply@travis-ci.org"
git config --global user.name "apt-bot"

# Clone the git repository of meilix project (the gh-pages branch only)
git clone -b gh-pages --single-branch https://apt-bot:$APT_SOURCE_UPDATER@github.com/fossasia/meilix.git
cd meilix

# Switching branch
git checkout gh-pages

# Copy the contents from the four folders and force replace them with the current
cp -rf ../../conf ./
cp -rf ../../db ./
cp -rf ../../dists ./
cp -rf ../../incoming ./
cp -rf ../../pool ./

# Adding everything
git add .

# Checking for staged changes
git status --porcelain|grep "M"
# if there are changes
if [ "$?" = 0 ]; then
  git commit --message "Apt source update for build:$TRAVIS_BUILD_NUMBER | Generated by Travis CI"
  git remote add origin-pages https://apt-bot:$APT_SOURCE_UPDATER@github.com/fossasia/meilix.git
  git pull --rebase origin-pages gh-pages
  git push --quiet --set-upstream origin-pages gh-pages
  echo "Commit has been made, Happy coding :)"
else # NO Changes fallback...
  echo "No changes detected.... Not commiting... Happy coding :)"
fi
