rm -rf __export/
rm -rf __s3/
rm -rf __changelog/
rm -rf www/

mkdir __export/
mkdir __s3/
mkdir __changelog/
mkdir www/

cd www/ && git clone https://github.com/SocialEngine/phpv4.git .
cd ../

cp -R www/ __export/product/
cd __export/product/
cd ../../ 

rm -rf __export/product/application/languages/index.html
rm -rf __export/product/application/packages/
mkdir __export/product/application/packages/
touch __export/product/application/packages/index.html

cd www/
git diff --name-only $OLD_VERSION...origin/$GIT_BRANCH > ../__changelog/files.txt
touch ../__changelog/change.log
IFS=$'\n'
for j in $(cat ../__changelog/files.txt)
do
    if [ -f $j ]; then
        echo "- $j" >> ../__changelog/change.log
        git log --follow --oneline $OLD_VERSION...origin/$GIT_BRANCH -- $j >> ../__changelog/change.log
    fi
done
cd ../

php build.php
rm -rf __export/product/application/themes/sunshine
rm -rf __export/product/application/themes/fbclone
rm -rf __export/product/application/themes/serenity
rm -rf __export/product/application/themes/charity
rm -rf __export/product/application/themes/elpis
rm -rf __export/product/application/themes/harmony
ZIP_NAME="socialengine$NEW_VERSION.zip"
cd __export/product/
zip --recurse-paths --quiet -9 $ZIP_NAME .
cp $ZIP_NAME ../../__s3/$ZIP_NAME

UPGRADE_NAME="social-engine-php-upgrade-4-x-$NEW_VERSION.tar"
cd ../../__export/upgrade/
rm -rf theme-serenity-$NEW_VERSION.tar
rm -rf theme-pulse-$NEW_VERSION.tar
rm -rf theme-inspira-$NEW_VERSION.tar
rm -rf theme-twitterclone-$NEW_VERSION.tar
rm -rf theme-prism-$NEW_VERSION.tar
rm -rf theme-charity-$NEW_VERSION.tar
tar -zcf ../../__s3/$UPGRADE_NAME .

cd ../../__s3/ \
  && mkdir socialengine-mobile-$NEW_VERSION/ \
  && mv module-sesandroidapp-$NEW_VERSION.tar socialengine-mobile-$NEW_VERSION/module-sesandroidapp-$NEW_VERSION.tar \
  && mv module-sesiosapp-$NEW_VERSION.tar socialengine-mobile-$NEW_VERSION/module-sesiosapp-$NEW_VERSION.tar \
  && mv module-sesapi-$NEW_VERSION.tar socialengine-mobile-$NEW_VERSION/module-sesapi-$NEW_VERSION.tar \
  && cd socialengine-mobile-$NEW_VERSION/ \
  && tar -zcf ../socialengine-mobile-$NEW_VERSION.tar . \
  && cd ../ \
  && rm -rf socialengine-mobile-$NEW_VERSION/


# export NEW_VERSION="6.1.1" && export OLD_VERSION="6.2.0" && export GIT_BRANCH="develop" && ./build.sh