#!/bin/bash

BOOTSTRAP_START_LOG=/var/log/vmware/upgrade/bootstrap.log

mkdir -p /var/log/vmware/upgrade

UPGRADE_RUNNER_DIR=/usr/lib/vmware/cis_upgrade_runner
if [ ! -d $UPGRADE_RUNNER_DIR ]; then
   echo "$(date): Could not find upgrade runner directory $UPGRADE_RUNNER_DIR" | tee -a $BOOTSTRAP_START_LOG
   exit 1
fi

### Invoke Export ###
# Determines the components to upgrade
components=`/bin/install-parameter upgrade.components`
# Determine cleanup preference
skipCleanup=`/bin/install-parameter upgrade.skipCleanup`
# Determine silent preference
silent=`/bin/install-parameter upgrade.silent`
# Determine client locale preference
locale=`/bin/install-parameter clientlocale`
# In dev mode default to skipCleanup=True
if [ ! $skipCleanup ] && [ $components ]; then
   skipCleanup="True"
fi

/usr/bin/env -i /bin/bash -lc "cd $UPGRADE_RUNNER_DIR && python bootstrap_scripts/run-upgrade-export.py \
   --outputFile=/var/log/vmware/upgrade/export.json\
   $(if [ $components ]; then echo " --component=${components}"; fi)\
   $(if [ $silent ]; then echo " --silent=${silent}"; fi)\
   $(if [ $locale ]; then echo " --locale=${locale}"; fi)\
   $(if [ $skipCleanup ]; then echo " --skipCleanup=${skipCleanup}"; fi)" 2>&1 | tee -a $BOOTSTRAP_START_LOG

exit ${PIPESTATUS[0]}
