#!/bin/sh
#
######################################################
# Build script for Core 11                          #
#                                                    #
# See .info for details                              #
######################################################

# This application will use a python3 virtual environment because it has
# specific version dependencies on other python packages.  If we used separate
# extensions for each python package, and those packages were later upgraded
# to newer versions, this application would probably stop working.  By using
# a virtual environment, we can freeze the dependencies just for this
# application's running environment.

######################################################
# Prepare extension creation                         #
######################################################
tce-load -iw python3.6
tce-load -wi python-pip.tcz
tce-load -iw python3.6-dev.tcz
tce-load -wi libffi-dev.tcz
tce-load -iw getlocale.tcz
tce-load -wi compiletc.tcz
tce-load -wi squashfs-tools.tcz
tce-load -wi tree.tcz

######################################################
# Configure extension creation parameters            #
######################################################
# Variables
TODAY=`date +%Y/%m/%d`
PACKAGE="octoprint"
VERSION="1.4.0"
DESCRIPTION="OctoPrint provides a snappy web interface for controlling consumer 3D printers"
AUTHORS="OctoPrint"
HOMEPAGE="https://github.com/OctoPrint/OctoPrint"
LICENSE="GNU Affero General Public License V3"
ME="Snackman8"
TAGS="octoprint 3d printer"
TMPDIR=/tmp/submit/${PACKAGE}
DOWNLOAD="https://github.com/OctoPrint/OctoPrint/archive/${VERSION}.tar.gz"

######################################################
# Create the virtual env                             #
######################################################
# we must create the virtual env where it will be located in the deployed extension
# because venv hardcodes paths into the virtual environment
sudo python3.6 -m venv /usr/local/lib/${PACKAGE}_venv --system-site-packages
source /usr/local/lib/${PACKAGE}_venv/bin/activate

######################################################
# Download Source Code                               #
######################################################
# Workdir
sudo rm -r /tmp/${PACKAGE} 2>/dev/null
mkdir /tmp/${PACKAGE}
cd /tmp/${PACKAGE}

# Source
wget ${DOWNLOAD} -O ${PACKAGE}-${VERSION}.tar.gz
tar xf ${PACKAGE}-${VERSION}.tar.gz 
cd OctoPrint-${VERSION}

######################################################
# Compile extension                                  #
######################################################
# download and install the English UTF-8 locale since this application won't run using the "C" locale
sudo mkdir -p /usr/lib/locale
sudo localedef -i en_US -f UTF-8 en_US.UTF-8
export LC_ALL=en_US.utf8
tree -ifx / > /tmp/${PACKAGE}/files_before_install.txt
sudo pip install .
tree -ifx / > /tmp/${PACKAGE}/files_after_install.txt

######################################################
# Prepare for Packaging                              #
######################################################
sudo rm -r ${TMPDIR}* 2>/dev/null
mkdir -p ${TMPDIR}/usr/local/bin
mkdir -p ${TMPDIR}/usr/lib/locale
mkdir -p ${TMPDIR}/usr/local/lib
mkdir -p ${TMPDIR}/usr/local/tce.installed
cp -r /usr/lib/locale ${TMPDIR}/usr/lib/
cp -r /usr/local/lib/octoprint_venv ${TMPDIR}/usr/local/lib/
# change the default setting of octoprint to use the user's home directory to install plugins so TC can persiste them
sed -i 's/pip_force_user=False/pip_force_user=True/g' ${TMPDIR}/usr/local/lib/octoprint_venv/lib/python3.6/site-packages/octoprint/plugins/pluginmanager/__init__.py
# create an octoprint in /usr/local/bin which will activate the virtual environment and call the octoprint in
# the virtual enviroment.  It will also make sure ~/.local/lib/python3.6/site-packages exists.  We do this at runtime
# since we don't know which user will be running this application during installation 
echo "mkdir -p ~/.local/lib/python3.6/site-packages && LC_ALL=en_US.utf8 /usr/local/lib/octoprint_venv/bin/octoprint" > ${TMPDIR}/usr/local/bin/octoprint
chmod +x ${TMPDIR}/usr/local/bin/octoprint 

###################################################
# Create info file                                #
###################################################
cd /tmp/submit/
cat <<EOF> ${PACKAGE}.tcz.info
Title:          ${PACKAGE}.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${TAGS}
Comments:       OctoPrint provides a snappy web interface for controlling consumer 3D printers
Change-log:     ${TODAY} first verion, ${VERSION}
Current:        ${TODAY} first verion, ${VERSION}
EOF

###################################################
# Create .dep file                                #
###################################################
cat <<EOF> ${PACKAGE}.tcz.dep
python3.6.tcz
expat2.tcz
libffi.tcz
EOF

###################################################
# Create install script file                      #
###################################################
cat <<EOF> $TMPDIR/usr/local/tce.installed/${PACKAGE}
#!/bin/sh
EOF

find $TMPDIR/ -type d | xargs chmod -v 755;

cd $TMPDIR
find $TMPDIR -perm 777 -exec chmod 755 {} \;
find $TMPDIR -perm 555 -exec chmod 755 {} \;
find $TMPDIR -perm 444 -exec chmod 644 {} \;
find $TMPDIR -perm 666 -exec chmod 644 {} \;
find $TMPDIR -perm 664 -exec chmod 644 {} \;
sudo chown -R root:root $TMPDIR
sudo chown -R root:staff $TMPDIR/usr/local/tce.installed
sudo chmod -R 775 $TMPDIR/usr/local/tce.installed

cd /tmp/submit/

mksquashfs $TMPDIR ${PACKAGE}.tcz

cd $TMPDIR
sudo sh -c "find usr -not -type d > ${PACKAGE}.tcz.list"
sudo mv ../${PACKAGE}.tcz .
sudo mv ../${PACKAGE}.tcz.dep .
sudo mv ../${PACKAGE}.tcz.info .

# Create md5 file
sudo sh -c "md5sum ${PACKAGE}.tcz > ${PACKAGE}.tcz.md5.txt"

# Cleanup temp directory
sudo rm -r -f usr
