#!/bin/sh

# Billing microupdates installer.
# you should place it into customer's machine with name
# MU<version>-billing.sh
# Script will define microupdate version by its name.
# And unpack archive with MU<version>-billing.zip
#
# E.x.
#   get mu version: MU33-billing.sh -> MU33
#   and unpack: unzip MU33-billing.zip

prog="`basename $0`"
billing_d="/opt/plesk-billing"
src_d="/usr/local/psa/tmp"

mu="`echo $prog | awk -F '-' '{print $1}'`"
mu_archive="${mu}-billing.zip"
mu_flag="$src_d/${mu}-billing_installed.flag"
cache_flag="/var/lib/plesk-billing/tmp/cache.need_clear"

echo -n "Trying to unpack billing fixes from $mu_archive... "

if [ ! -d "$billing_d" ]; then
	echo "billing not installed. Skipping..."
	exit 0
fi

if [ -f "$mu_flag" ]; then
	echo "already installed"
	exit 0
fi

unzip -q -o $src_d/$mu_archive -d $billing_d && flag=1
touch $cache_flag

[ -n "$flag" ] && echo "done" && exit 0

echo "failed"
exit 0
