#!/bin/bash

set -euxo pipefail

BITBUCKET_DOWNLOAD_URL=http://product-downloads.atlassian.com/software/stash/downloads/atlassian-bitbucket-10.2.6.tar.gz
BITBUCKET_DOWNLOAD_DEST=/tmp/atlassian-bitbucket-10.2.6.tar.gz
BITBUCKET_DOWNLOAD_CHECKSUM=d3602f13deecf172b8c8ed2b3b29f3d0e9998a7443d5bfbc40cf31a02c41cf1d

BITBUCKET_CATALINA=/opt/atlassian/bitbucket
BITBUCKET_PATCH=/opt/atlassian/atlassian-bitbucket.patch

wget -c $BITBUCKET_DOWNLOAD_URL -O $BITBUCKET_DOWNLOAD_DEST
echo -n "$BITBUCKET_DOWNLOAD_CHECKSUM $BITBUCKET_DOWNLOAD_DEST" | sha256sum -c -

rm -rf $BITBUCKET_CATALINA
mkdir -p $BITBUCKET_CATALINA
tar zxf $BITBUCKET_DOWNLOAD_DEST -C $BITBUCKET_CATALINA --strip-components=1

cat $BITBUCKET_PATCH | patch -p1 -d /
chmod a+x $BITBUCKET_CATALINA/bin/start-bitbucket.sh
chmod a+x $BITBUCKET_CATALINA/bin/stop-bitbucket.sh
find $BITBUCKET_CATALINA -type f -name '*.so' -exec chrpath -d {} \;
find $BITBUCKET_CATALINA -type f -name '*.bak' -delete
find $BITBUCKET_CATALINA -type f -name '*.orig' -delete
find $BITBUCKET_CATALINA -type f -name '*.rej' -delete
fdupes -qnrps $BITBUCKET_CATALINA

chown -Rf bitbucket:bitbucket $BITBUCKET_CATALINA
chmod 0700 $BITBUCKET_CATALINA

#DEBHELPER#

exit 0
