#!/bin/bash
# ---------------------------------------------------------
# Creates a OSX flat installer package from a Linux
# environment. You need xar and bomutils in your $PATH
# ---------------------------------------------------------
#
# kudos to SchizoDuckie for putting this gist together
#
# https://gist.github.com/SchizoDuckie/2a1a1cc71284e6463b9a
# https://krypted.com/mac-os-x/inspecting-creating-mac-installer-packages-linux/
# ----------------------------------------------------------


# The following variables need to be overriden by environment vars

: "${VERSION:=0.0.1}"
: "${APPNAME:=TestApp}"
: "${IDENTIFIER:=se.leap.bitmask.installer}"

# ----------------------------------------------------------

BUILD_DIR="../dist"
BASE_DIR="../build/osx"
BACKGROUND="../assets/osx-background.png"

# ----------------------------------------------------------

initialize () {
  rm -rf "$BASE_DIR/darwin"
  mkdir -p "$BASE_DIR/darwin/flat/Resources/en.lproj"
  mkdir -p "$BASE_DIR/darwin/flat/base.pkg/"
  mkdir -p "$BASE_DIR/darwin/root/Applications"
  mkdir -p "$BASE_DIR/darwin/scripts"
  cp -R $BUILD_DIR/*.app $BASE_DIR/darwin/root/Applications
  cp -R scripts/* $BASE_DIR/darwin/scripts/
  cp $BACKGROUND $BASE_DIR/darwin/flat/Resources/en.lproj/background.png
  NUM_FILES=$(find ${BASE_DIR}/darwin/root | wc -l)
  INSTALL_KB_SIZE=$(du -k -s ${BASE_DIR}/darwin/root | awk "{print $1}")
}

# TODO for localization, these files should be taken from transifex, etc.
# TODO hardcoding a foundation for now.
writeInstallerDocs () {
  cat <<EOF > ${BASE_DIR}/darwin/flat/Resources/en.lproj/welcome.html
<html>
<body>
<font face="helvetica">
<h1>${APPNAME} installer</h1>
This will guide you through the steps needed to install ${APPNAME} in your computer.

<hr/>

<p>
<b>${APPNAME}</b> is a <i>simple, fast and secure VPN</i> developed by the Bitmask team. This app is configured to connect to a single trusted VPN provider.
</p>

<hr/>
<p>The service is expensive to run. Please donate at <a href="https://riseup.net/vpn/donate">https://riseup.net/vpn/donate</a></p>

</font>
</body>
</html>
EOF

}

writePackageInfo () {
  cat <<EOF > ${BASE_DIR}/darwin/flat/base.pkg/PackageInfo
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<pkg-info overwrite-permissions="true" relocatable="false" identifier="${IDENTIFIER}" postinstall-action="none" version="${VERSION}" format-version="2" generator-version="InstallCmds-502 (14B25)" auth="root">
 <payload numberOfFiles="${NUM_FILES}" installKBytes="${INSTALL_KB_SIZE}"/>
 <bundle-version>
 <bundle id="${IDENTIFIER}" CFBundleIdentifier="${IDENTIFIER}" path="./Applications/${APPNAME}.app" CFBundleVersion="1.3.0"/>
 </bundle-version>
 <update-bundle/>
 <atomic-update-bundle/>
 <strict-identifier/>
 <relocate/>
 <scripts>
   <preinstall file="preinstall"/>
   <postinstall file="postinstall"/>
 </scripts>
</pkg-info>
EOF
}

writeDistribution () {
  cat <<EOF > ${BASE_DIR}/darwin/flat/Distribution
<?xml version="1.0" encoding="utf-8"?>
<installer-gui-script minSpecVersion="1">
 <title>${APPNAME} ${VERSION}</title>
 <options customize="never" allow-external-scripts="no"/>
 <domains enable_anywhere="true"/>
 <background file="background.png" mime-type="image/png" scaling="tofit" />
 <background-darkAqua file="background.png" mime-type="image/png" scaling="tofit" />
 <welcome file="welcome.html" mime-type="text/html"/>
 <installation-check script="pm_install_check();"/>
 <script>function pm_install_check() {
 if(!(system.compareVersions(system.version.ProductVersion,'10.5') >= 0)) {
 my.result.title = "Failure";
 my.result.message = "You need at least Mac OS X 10.5 to install ${APPNAME}.";
 my.result.type = "Fatal";
 return false;
 }
 return true;
 }
 </script>
 <choices-outline>
 <line choice="choice1"/>
 </choices-outline>
 <choice id="choice1" title="base">
 <pkg-ref id="${IDENTIFIER}.base.pkg"/>
 </choice>
 <pkg-ref id="${IDENTIFIER}.base.pkg" installKBytes="${INSTALL_KB_SIZE}" version="${VERSION}" auth="Root">#base.pkg</pkg-ref>
</installer-gui-script>
EOF
}

createPackage () {
 PKG_NAME="${APPNAME}-${VERSION}_unsigned.pkg"
 PKG_LOCATION="../../${PKG_NAME}"
 PKG_LOCATION_REL="${BASE_DIR}/${PKG_NAME}"
 PKG_FINAL="${BUILD_DIR}/${PKG_NAME}"
 ( cd ${BASE_DIR}/darwin/root && find . | cpio -o --format odc --owner 0:80 | gzip -c ) > ${BASE_DIR}/darwin/flat/base.pkg/Payload
 ( cd ${BASE_DIR}/darwin/scripts && find . | cpio -o --format odc --owner 0:80 | gzip -c ) > ${BASE_DIR}/darwin/flat/base.pkg/Scripts
 mkbom -u 0 -g 80 ${BASE_DIR}/darwin/root ${BASE_DIR}/darwin/flat/base.pkg/Bom
 ( cd ${BASE_DIR}/darwin/flat/ && xar --compression none -cf "${PKG_LOCATION}" * )
 cp ${PKG_LOCATION_REL} ${PKG_FINAL}
 echo "[+] OSX package has been built: ${PKG_FINAL}"
}

initialize
writeInstallerDocs
writePackageInfo
writeDistribution
createPackage