#define platform variables
CURRENT_FOLDER="$(pwd)"
SOURCE_RELEASE_REPO="git@bitbucket.org:getsocial/cordova-sdk.git"
SOURCE_RELEASE_FOLDER="${CURRENT_FOLDER}/RELEASE"
DEST_RELEASE_FOLDER="${CURRENT_FOLDER}/RELEASE_SDK"
DEST_RELEASE_REPO_NAME="getsocial-cordova-sdk"
#DEST_RELEASE_REPO="git@github.com:getsocial-im/$DEST_RELEASE_REPO_NAME.git"
DEST_RELEASE_REPO="git@github.com:getsocial-im/${DEST_RELEASE_REPO_NAME}.git"

#retrieve version and commit
VERSION=$(grep "version" "${SOURCE_RELEASE_FOLDER}/release.properties" | sed "s/version=\(.*\)/\1/")
COMMIT=$(grep "commit" "${SOURCE_RELEASE_FOLDER}/release.properties" | sed "s/commit=\(.*\)/\1/")
echo "retrieved version=${VERSION} and commit=${COMMIT}"
#rm "${SOURCE_RELEASE_FOLDER}/release.properties"

#extract changelog for current version and JSON encode it
#perl -nE"say /### v${VERSION}[\W]*(\+[\W\w]*?)(?=[\n]*---)/g" -0777 "${SOURCE_RELEASE_FOLDER}/CHANGELOG.md" | perl -pe "s/\R/\\\n/g" -0777 | perl -pe "s/\"/\\\"/g" -0777 > "${SOURCE_RELEASE_FOLDER}/CHANGELOG_${VERSION}.json"
perl -nE"say /### v${VERSION}[\W]*(\+[\W\w]*?)(?=[\n]*---)/g" -0777 "${SOURCE_RELEASE_FOLDER}/CHANGELOG.md" | perl -pe "s/\R/\\\n/g" -0777 | perl -pe "s/\"/\\\"/g" -0777 > "${SOURCE_RELEASE_FOLDER}/CHANGELOG_${VERSION}.json"
CHANGE_LOG=`cat "${SOURCE_RELEASE_FOLDER}/CHANGELOG_${VERSION}.json"`
rm "${SOURCE_RELEASE_FOLDER}/CHANGELOG_${VERSION}.json"

#validate changelog
if [ ${#CHANGE_LOG} -lt 5 ]; then
   echo "missing changelog for v${VERSION}"
   exit 1
else
	echo "$CHANGE_LOG"
fi

#checkout source repo
git checkout ${COMMIT}
   
#tag with version and push to the SourceReleaseRepo 
echo "tag with version and push to the SourceReleaseRepo"
git tag -a v${VERSION} ${COMMIT} -m "Version released on `date -u +\"%Y-%m-%d %H-%M-%SZ\"`"
git push origin --tags

#clone or fetch DestReleaseRepo repo
echo "clone or fetch DestReleaseRepo repo"
mkdir ${DEST_RELEASE_FOLDER}
if [ -d "${DEST_RELEASE_FOLDER}/DestReleaseRepo" ]; then
	cd "${DEST_RELEASE_FOLDER}/DestReleaseRepo"
    git fetch origin master
else
	git clone ${DEST_RELEASE_REPO} "${DEST_RELEASE_FOLDER}/DestReleaseRepo"
    cd "${DEST_RELEASE_FOLDER}/DestReleaseRepo"
fi
git checkout master

#delete files in the DestReleaseRepo
echo "delete files in the DestReleaseRepo"
find * | grep -v .git | xargs rm -rf

#copy new files in the DestReleaseRepo
echo "copy new files in the DestReleaseRepo"
cp -R "/${SOURCE_RELEASE_FOLDER}/" "${DEST_RELEASE_FOLDER}/DestReleaseRepo/"
rm "${DEST_RELEASE_FOLDER}/DestReleaseRepo/release.properties"

#add new files and push to the DestReleaseRepo 
echo "add new files and push to the DestReleaseRepo "
git add .
git commit -m "Released version ${VERSION}"
git push origin

#tag with version and push to the DestReleaseRepo 
echo "tag with version and push to the DestReleaseRepo"
git tag -a v${VERSION} -m "Version released on `date -u +\"%Y-%m-%d %H-%M-%SZ\"`"
git push origin --tags

#create new release on GitHub
curl -X POST -H "Authorization: token 30834bb31b82868624cd2cf6f7a24d97e3286897" -d "{
  \"tag_name\": \"v${VERSION}\",
  \"name\": \"v${VERSION}\",
  \"body\": \"$CHANGE_LOG\",
  \"draft\": false,
  \"prerelease\": false
}" "https://api.github.com/repos/getsocial-im/$DEST_RELEASE_REPO_NAME/releases"
