#! /bin/bash
# Summary: Cisco Unified Communications Locale Installer for Cisco Unified IP Phones 
# Name: po-locale-pt_BR
# Version: k3
# Build: 10.3.2.1000
# Copyright: Copyright (C) 2015 by Cisco Systems, Inc.
# Requires: bash
# Vendor: Cisco Systems, Inc.
# URL: http://www.cisco.com
# Packager: Jenny Chen <qingche@cisco.com>
# ExclusiveOS: linux
#       -[k]eep existing COP files and don't add CSV file content to the database - used during an L2 migration

printf "\n\n---------- Cisco Unified Communications Locale Installer Post Install Script ----------\n\n"

# Determine if there is enough free space for locale files:
# logic: Free space in / file system (/usr/local/cm) >= .tar file size) * 2
UNUSED_COMMON_BLOCKS=`/bin/df  / | grep / | awk '{print $4 }'`
let UNUSED_COMMON=$UNUSED_COMMON_BLOCKS*1024
tarFileSize=`ls -l /common/download|grep "po-locale-pt_BR-k3-10.3.2.1000-1.tar"|awk '{print $5}'`
let NEED_SPACE=$tarFileSize*2

if [ $UNUSED_COMMON -le $NEED_SPACE ]; then
    printf "Locale installation cannot start as / does not have enough space. Exiting from locale installation...\n"
    printf "Free space available in / is $UNUSED_COMMON; Required space for this local is $NEED_SPACE\n"
    exit 1
fi

# Determine if the server is a publisher or subscriber
isPublisher=1    #default value
if [ -f /usr/local/cm/bin/pryftool ]
then
    testPublisher=`/usr/local/cm/bin/pryftool --system cm --hive dbl --get='publisher'`

    if [[ "$testPublisher" == "" ]]
    then
        isPublisher=1
        printf "[LOCALE] This server is a PUBLISHER.\n\n"
    else
        isPublisher=0
        printf "[LOCALE] This server is a SUBSCRIBER.\n\n"
        printf "[LOCALE] Publisher = .\n\n"
    fi
fi


# Create the new path for placement of installed COP files
if [[ ! -d /var/log/active/installed_options ]]; then
	printf "[LOCALE] This version of Cisco Unified Communications Manager does not have a migration path.\n\n"
	printf "[LOCALE] Creating migration path...\n\n"
        mkdir /var/log/active/installed_options
	printf "[LOCALE] Migration path created.\n\n"
fi

printf "[LOCALE] Adding Portuguese (Brazil) to Cisco Unified Communications Manager...\n\n"

# Check for presence of the CSV Installer, without it there is no point in continuing with the installation
if [[ -x /usr/local/cm/bin/installdb ]]
then
	installCSV=0
	printf "[LOCALE] CSV file installer /usr/local/cm/bin/installdb located - the installation can proceed.\n\n"
	. /usr/local/cm/db/informix/local/ids.env  # env source for installdb


	printf "[LOCALE] Extracting localised files...\n\n"
	if nice -n 19 /bin/tar --directory=/ -xpPf /common/download/po-locale-pt_BR-k3-10.3.2.1000-1.tar; then
		printf "[LOCALE] Successfully extracted localised files.\n\n"
	else
		printf "[LOCALE] Extraction of localised files failed.\n\n"
		exit 1
	fi

	printf "[LOCALE] Setting file permissions...\n\n"

	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/7905-dictionary.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/7905-dictionary.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/7905-kate.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/7905-kate.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/7960-dictionary.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/7960-dictionary.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/7960-dictionary-ext.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/7960-dictionary-ext.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/7960-font.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/7960-font.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/7960-kate.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/7960-kate.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/SCCP-dictionary.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/SCCP-dictionary.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/SCCP-dictionary-ext.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/SCCP-dictionary-ext.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/SCCP-dictionary.utf-8.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/SCCP-dictionary.utf-8.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/7920-dictionary.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/7920-dictionary.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/7920-font.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/7920-font.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/7920-kate.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/7920-kate.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/7921-dictionary.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/7921-dictionary.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/7921-font.dat; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/7921-font.dat
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/7921-kate.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/7921-kate.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/7921-kate.utf-8.xml; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/7921-kate.utf-8.xml
	chown tomcat:tomcat /usr/local/cm/application_locale/ccmcip/portuguese_brazil/helpfiles/appid0/7960-i-button-help.xml; chmod 0644 /usr/local/cm/application_locale/ccmcip/portuguese_brazil/helpfiles/appid0/7960-i-button-help.xml
	chown tomcat:tomcat /usr/local/cm/application_locale/ccmcip/portuguese_brazil/helpfiles/appid0/7960-i-button-help.utf-8.xml; chmod 0644 /usr/local/cm/application_locale/ccmcip/portuguese_brazil/helpfiles/appid0/7960-i-button-help.utf-8.xml
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/td-sccp.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/td-sccp.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/td-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/td-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/tc-sccp.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/tc-sccp.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/tc-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/tc-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/mk-sccp.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/mk-sccp.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/mk-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/mk-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/ipc-sccp.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/ipc-sccp.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/ipc-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/ipc-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/ld-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/ld-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/gp-sccp.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/gp-sccp.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/gp-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/gp-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/rp-sccp.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/rp-sccp.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/be-sccp.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/be-sccp.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/be-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/be-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/rtl-sccp.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/rtl-sccp.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/rtl-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/rtl-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/cin-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/cin-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/gb-sccp-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/gb-sccp-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/gd-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/gd-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/gh-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/gh-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/lk-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/lk-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/sl-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/sl-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/sp-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/sp-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/sl-be-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/sl-be-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/sl-bev-sip.jar; chmod 0775 /usr/local/cm/tftp/portuguese_brazil/sl-bev-sip.jar
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/CIPC_Locale.loads; chmod 0755 /usr/local/cm/tftp/portuguese_brazil/CIPC_Locale.loads
	chown ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil/CIPC_Locale.zip; chmod 0755 /usr/local/cm/tftp/portuguese_brazil/CIPC_Locale.zip
	chown root:root /var/log/active/installed_options/localeinstaller/migrate_locales.bash; chmod 0755 /var/log/active/installed_options/localeinstaller/migrate_locales.bash
	chown root:root /usr/local/cm/script/locale_version.sql; chmod 0755 /usr/local/cm/script/locale_version.sql
	chown -R ctftp:ccmbase /usr/local/cm/tftp/brazil
	chmod -R 775 /usr/local/cm/tftp/brazil
	chown -R ctftp:ccmbase /usr/local/cm/tftp/portuguese_brazil
	chmod -R 775 /usr/local/cm/tftp/portuguese_brazil
	chown database:ccmbase /usr/local/cm/db/csv/products/brazil_network_locale.csv; chmod 0644 /usr/local/cm/db/csv/products/brazil_network_locale.csv
	chown ccmservice:ccmbase /usr/local/cm/application_locale/localeinstaller/portuguese_brazil/localeversion.txt; chmod 0755 /usr/local/cm/application_locale/localeinstaller/portuguese_brazil/localeversion.txt
	chown root:root /usr/local/cm/application_locale/ccmadmin/phoneInfo; chmod 0755 /usr/local/cm/application_locale/ccmadmin/phoneInfo
 
	if [[ $1 != '-k' ]] && [[ "$isPublisher" == 1 ]]
	then
	  if [[ -f /usr/local/cm/db/csv/products/brazil_network_locale.csv ]]
		then
			printf "[LOCALE] Attempting to add network locale information to the database...\n\n"
			/usr/local/cm/bin/installdb -c /usr/local/cm/db/csv/products/brazil_network_locale.csv
			printf "\n\n"
		else
			printf "\n[LOCALE] File not found: brazil_network_locale.csv, the network locale has not been added to the database.\n\n"
			installCSV=1
		fi
		
		# adding PO LI version to db
		if [[ -f /usr/local/cm/script/locale_version.sql ]]
			then
				printf "\n[LOCALE] Attempting to add Phone locale version number to the database...\n\n"
				su - -c "/usr/local/cm/bin/installdb -s /usr/local/cm/script/locale_version.sql" informix
				printf "\n\n"
				rm -f /usr/local/cm/script/locale_version.sql
			else
				printf "[LOCALE] File not found: /usr/local/cm/script/locale_version.sql, the user locale version has not been added to the database\n\n"
				installCSV=1
		fi		
	fi

		if [[ $1 != '-k' ]]; then
			printf "[LOCALE] Locating and removing old and legacy COP files related to this locale...\n\n"
			/usr/bin/find /var/log/active/installed_options/ -name "po-locale-pt_BR-*.cop" | xargs rm -f
			/usr/bin/find /var/log/active/installed_options/ -name "cm-locale-portuguese_brazil-*.cop" | xargs rm -f
		fi

		# Copy COP file to new migratory path
		if [[ -f /common/download/po-locale-pt_BR-k3-10.3.2.1000-1.cop ]]; then
			if /bin/cp /common/download/po-locale-pt_BR-k3-10.3.2.1000-1.cop /var/log/active/installed_options/; then
				printf "[LOCALE] Copied po-locale-pt_BR-k3-10.3.2.1000-1.cop to migratory path.\n\n"
			else
				printf "[LOCALE] Could not copy po-locale-pt_BR-k3-10.3.2.1000-1.cop to migratory path. This locale will not be migrated during an upgrade!\n\n"
			fi
		elif [[ -f /common/installed_options/po-locale-pt_BR-k3-10.3.2.1000-1.cop ]]; then
			if /bin/cp  /common/installed_options/po-locale-pt_BR-k3-10.3.2.1000-1.cop /var/log/active/installed_options/; then
				printf "[LOCALE] Copied po-locale-pt_BR-k3-10.3.2.1000-1.cop to migratory path.\n\n"
			else
				printf "[LOCALE] Could not copy po-locale-pt_BR-k3-10.3.2.1000-1.cop to migratory path. This locale will not be migrated during an upgrade!\n\n"
			fi
		else
			printf "[LOCALE] Could not locate po-locale-pt_BR-k3-10.3.2.1000-1.cop. This locale will not be migrated during an upgrade!\n\n"
		fi
		
		# [qingche] 201309 sleep 10 seconds - wait for db script to finish before printing the installation is complete message.
		sleep 10
		printf "\n\n\n\n########################################################################\n\n"
		printf "ATTENTION: Installation of Portuguese (Brazil) is complete!\n\n"
		printf "########################################################################\n\n"
		printf "In order to activate changes please select the following option from the Cisco Unified Communications Operating System Administration menu:\n\n"
		printf "    Settings > Version > Restart\n\n"
		printf "If desired, more locales may be added or updated prior to restarting the server.\n\n"
		printf "########################################################################\n\n"
		printf "WARNING: Locale changes MAY NOT BE FULLY AVAILABLE until the server has been restarted!\n\n"
		printf "########################################################################\n\n"
		sleep 10

else
	printf "[LOCALE] CSV file installer /usr/local/cm/bin/installdb is not present or not executable.\n\n"
	printf "[LOCALE] Addition of Portuguese (Brazil) to the database has failed!\n\n"
fi
