#!/bin/bash

set -euxo pipefail

JIRA_DOWNLOAD_URL=http://product-downloads.atlassian.com/software/jira/downloads/atlassian-jira-core-9.12.34.tar.gz
JIRA_DOWNLOAD_DEST=/tmp/atlassian-jira-core-9.12.34.tar.gz
JIRA_DOWNLOAD_CHECKSUM=639fdbc144d975da1ab299d402d1d415cebc507f9c07013a2172a577facd97b9

JIRA_CATALINA=/opt/atlassian/jira
JIRA_PATCH=/opt/atlassian/atlassian-jira-core.patch

wget -c $JIRA_DOWNLOAD_URL -O $JIRA_DOWNLOAD_DEST
echo -n "$JIRA_DOWNLOAD_CHECKSUM $JIRA_DOWNLOAD_DEST" | sha256sum -c -

rm -rf $JIRA_CATALINA
mkdir -p $JIRA_CATALINA
tar zxf $JIRA_DOWNLOAD_DEST -C $JIRA_CATALINA --strip-components=1

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

chown -Rf jira:jira $JIRA_CATALINA
chmod 0700 $JIRA_CATALINA

#DEBHELPER#

exit 0
