if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
if [ "$TRAVIS_BRANCH" == "alphas" ]; then
  echo -e "Creating a release\n"
  curl -X POST -H "Authorization: token ${GH_TOKEN}" \
      -d '{"tag_name": "alpha-'${TRAVIS_BUILD_NUMBER}'", "target_commitish": "master", "name": "Alpha release number '${TRAVIS_BUILD_NUMBER}'", "body": "Automatic alpha release generated by Travis CI", "draft": false, "prerelease": true}' "https://api.github.com/repos/WagicProject/wagic/releases" > json.txt
  IDDI=`cat json.txt | jq '.id'`

  echo -e "Uploading Core resources\n"
  curl -X POST -H "Authorization: token ${GH_TOKEN}" \
     -H "Accept: application/vnd.github.manifold-preview" \
     -H "Content-Type: application/zip" \
     --data-binary @core.zip \
     "https://uploads.github.com/repos/WagicProject/wagic/releases/${IDDI}/assets?name=Wagic-core.zip"

  echo -e "Uploading android package\n"
  curl -X POST -H "Authorization: token ${GH_TOKEN}" \
     -H "Accept: application/vnd.github.manifold-preview" \
     -H "Content-Type: application/zip" \
     --data-binary @projects/mtg/Android/bin/Wagic-debug.apk \
     "https://uploads.github.com/repos/WagicProject/wagic/releases/${IDDI}/assets?name=Wagic-android.apk"

  echo -e "Uploading PSP package\n"
  curl -X POST -H "Authorization: token ${GH_TOKEN}" \
     -H "Accept: application/vnd.github.manifold-preview" \
     -H "Content-Type: application/zip" \
     --data-binary @projects/mtg/psprelease.zip \
     "https://uploads.github.com/repos/WagicProject/wagic/releases/${IDDI}/assets?name=Wagic-psp.zip"

#  echo -e "Uploading Windows package\n"
#  curl -X POST -H "Authorization: token ${GH_TOKEN}" \
#     -H "Accept: application/vnd.github.manifold-preview" \
#     -H "Content-Type: application/zip" \
#     --data-binary @build/win-cross/win-cross.zip \
#     "https://uploads.github.com/repos/WagicProject/wagic/releases/${IDDI}/assets?name=Wagic-windows.zip"

  echo -e "Done uploading\n"
fi
fi

if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
if [ "$TRAVIS_BRANCH" == "master" ]; then

  # get info about all releases
  echo -e "Getting info about previous releases"
  curl -X GET -H "Authorization: token ${GH_TOKEN}" \
       "https://api.github.com/repos/WagicProject/wagic/releases" > json.txt

  # extract info only about only "latest-release" tag
  cat json.txt |jq  'map(select (.tag_name == "latest-master"))' > latest.txt

  # get id of release
  ID_TO_DELETE=`cat latest.txt |jq '.[0].id'`

  # delete previous release
  echo -e "Deleting release number ${ID_TO_DELETE}"
  curl -X DELETE -H "Authorization: token ${GH_TOKEN}" \
     "https://api.github.com/repos/WagicProject/wagic/releases/${ID_TO_DELETE}"

  # delete previous tag
  curl -X DELETE -H "Authorization: token ${GH_TOKEN}" \
    "https://api.github.com/repos/WagicProject/wagic/git/refs/tags/latest-master"


  echo -e "Creating a release\n"
  curl -X POST -H "Authorization: token ${GH_TOKEN}" \
      -d '{"tag_name": "latest-master", "target_commitish": "master", "name": "master-'${TRAVIS_BUILD_NUMBER}'", "body": "Automatic release based on latest commit to master branch generated by Travis CI", "draft": false, "prerelease": true}' "https://api.github.com/repos/WagicProject/wagic/releases" > json.txt
  IDDI=`cat json.txt | jq '.id'`

  echo -e "Uploading Core resources\n"
  curl -X POST -H "Authorization: token ${GH_TOKEN}" \
     -H "Accept: application/vnd.github.manifold-preview" \
     -H "Content-Type: application/zip" \
     --data-binary @core.zip \
     "https://uploads.github.com/repos/WagicProject/wagic/releases/${IDDI}/assets?name=Wagic-core.zip"

  echo -e "Uploading android package\n"
  curl -X POST -H "Authorization: token ${GH_TOKEN}" \
     -H "Accept: application/vnd.github.manifold-preview" \
     -H "Content-Type: application/zip" \
     --data-binary @projects/mtg/Android/bin/Wagic-debug.apk \
     "https://uploads.github.com/repos/WagicProject/wagic/releases/${IDDI}/assets?name=Wagic-android.apk"

  echo -e "Uploading PSP package\n"
  curl -X POST -H "Authorization: token ${GH_TOKEN}" \
     -H "Accept: application/vnd.github.manifold-preview" \
     -H "Content-Type: application/zip" \
     --data-binary @projects/mtg/psprelease.zip \
     "https://uploads.github.com/repos/WagicProject/wagic/releases/${IDDI}/assets?name=Wagic-psp.zip"

#  echo -e "Uploading Windows package\n"
#  curl -X POST -H "Authorization: token ${GH_TOKEN}" \
#     -H "Accept: application/vnd.github.manifold-preview" \
#     -H "Content-Type: application/zip" \
#     --data-binary @build/win-cross/win-cross.zip \
#     "https://uploads.github.com/repos/WagicProject/wagic/releases/${IDDI}/assets?name=Wagic-windows.zip"

  echo -e "Done uploading\n"
fi
fi
