#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2010, 2011, 2012, 2013, 2014 Zimbra, Inc.
# 
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software Foundation,
# version 2 of the License.
# 
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <http://www.gnu.org/licenses/>.
# ***** END LICENSE BLOCK *****
# 


usage() {
  echo "Usage: $0"
  exit 1
}

if [ x`whoami` != xzimbra ]; then
  echo Error: must be run as zimbra user
  exit 1
fi

source /opt/zimbra/bin/zmshutil || exit 1
zmsetvars

download_dir=${mailboxd_directory}/webapps/zimbra/downloads
index=${download_dir}/index.html

admin_dir=${mailboxd_directory}/webapps/zimbraAdmin/WEB-INF/classes/messages
zmsg=${admin_dir}/ZaMsg.properties

if [ -d "${download_dir}" ]; then
  ZCO_32BIT=$(cd ${download_dir}; ls -tr1 ZimbraConnectorOLK*x86.msi 2>/dev/null | tail -1)
  ZCO_64BIT=$(cd ${download_dir}; ls -tr1 ZimbraConnectorOLK*x64.msi 2>/dev/null | tail -1)
  PST_IMPORT=$(cd ${download_dir}; ls -tr1 ZCSPSTImportWizard* 2>/dev/null | tail -1)
  EXCHANGE_MIG_WIZARD=$(cd ${download_dir}; ls -tr1 ZCSExchangeMigrationWizard* 2>/dev/null | tail -1)
  DOMINO_MIG_WIZARD=$(cd ${download_dir}; ls -tr1 ZCSDominoMigrationWizard* 2>/dev/null | tail -1)
  GENERAL_MIG_WIZARD_32BIT=$(cd ${download_dir}; ls -tr1 ZimbraMigration_*_x86.zip 2>/dev/null | tail -1)
  GENERAL_MIG_WIZARD_64BIT=$(cd ${download_dir}; ls -tr1 ZimbraMigration_*_x64.zip 2>/dev/null | tail -1)
  CONNECTOR_UNSIGNED_32_DOWNLOAD=$(cd ${download_dir}; ls -tr1 ZimbraConnectorOLK*x86-UNSIGNED.msi 2>/dev/null | tail -1)
  CONNECTOR_UNSIGNED_64_DOWNLOAD=$(cd ${download_dir}; ls -tr1 ZimbraConnectorOLK*x64-UNSIGNED.msi 2>/dev/null | tail -1)
  GROUPWISE_MIG_WIZ_DOWNLOAD=$(cd ${download_dir}; ls -tr1 ZCSGroupwiseMigrationWizard* 2>/dev/null | tail -1)


  cat ${index}.in | \
    sed -e "s/@@ZCO_32BIT@@/${ZCO_32BIT}/" \
    -e "s/@@ZCO_64BIT@@/${ZCO_64BIT}/" \
    -e "s/@@PST_IMPORT@@/${PST_IMPORT}/" \
    -e "s/@@EXCHANGE_MIG_WIZARD@@/${EXCHANGE_MIG_WIZARD}/" \
    -e "s/@@GENERAL_MIG_WIZARD_32BIT@@/${GENERAL_MIG_WIZARD_32BIT}/" \
    -e "s/@@GENERAL_MIG_WIZARD_64BIT@@/${GENERAL_MIG_WIZARD_64BIT}/" \
    -e "s/@@DOMINO_MIG_WIZARD@@/${DOMINO_MIG_WIZARD}/" > ${index}


  if [ -f "${zmsg}" ]; then
      sed -i -e "s#ZimbraConnectorOLK_.*x86.msi#${ZCO_32BIT}#" \
      -e "s#ZCSExchangeMigrationWizard.*.zip#${EXCHANGE_MIG_WIZARD}#" \
      -e "s#ZCSPSTImportWizard.*.zip#${PST_IMPORT}#" \
      -e "s#ZimbraMigration_.*_x64.zip#${GENERAL_MIG_WIZARD_64BIT}#" \
      -e "s#ZimbraMigration_.*_x86.zip#${GENERAL_MIG_WIZARD_32BIT}#" \
      -e "s#ZCSDominoMigrationWizard.*.zip#${DOMINO_MIG_WIZARD}#" \
      -e "s#ZimbraConnectorOLK_.*x86-UNSIGNED.msi#${CONNECTOR_UNSIGNED_32_DOWNLOAD}#" \
      -e "s#ZimbraConnectorOLK_.*x64-UNSIGNED.msi#${CONNECTOR_UNSIGNED_64_DOWNLOAD}#" \
      -e "s#ZCSGroupwiseMigrationWizard.*.exe#${GROUPWISE_MIG_WIZ_DOWNLOAD}#" \
      -e "s#ZimbraConnectorOLK_.*x64.msi#${ZCO_64BIT}#" ${zmsg}
  fi
fi

exit 0

