#!/bin/sh
# log will display in /var/log/install.log
DBGLOG=/dev/stdout
DAT=`date`
echo "$0 $DAT" >> $DBGLOG
export >> $DBGLOG

TM_DIR="/Library/Application Support/TrendMicro"
TMP_DIR="/var/tmp/TrendMicro"
PTN_NORMAL_DIR=Ptn_Normal
PTN_MAC_DIR=Ptn_Mac
UNINSTALL_LOCK=/var/tmp/tmccuninstall.lock
UPGRADE_LOCK=/var/tmp/tmccupgrade.lock
INSTALL_LOCK=/var/tmp/tmccinstall.lock
FRESH_INSTALL_LOCK=/var/tmp/tmccfreshinstall.lock

# Daemon will write install time or upgrade time to EPStatus.plist by the locks
INSTALL_TIME_LOCK=/var/tmp/tmccinstalltime.lock
UPGRADE_TIME_LOCK=/var/tmp/tmccupgradetime.lock

UIMGMT_APP="/Library/Application Support/TrendMicro/TmccMac/UIMgmt.app"

clean_time_locks ()
{
	rm -rf "$INSTALL_TIME_LOCK"
	rm -rf "$UPGRADE_TIME_LOCK"
}

is_fresh_install ()
{
	# if the previous installation completed normally, $UIMGMT_APP exists and $INSTALL_LOCK is removed
	if [ -d "$UIMGMT_APP" ]; then
		if [ ! -f "$INSTALL_LOCK" ]; then
			# upgrade case
			return 0
		else
			# installation incomplete case
			rm -rf "$UPGRADE_LOCK"
		fi
	fi

	# fresh install case
	return 1
}

# log for automation test
TMLOG_DIR="/var/log/TrendMicro"
INSTALL_LOG="${TMLOG_DIR}/TMSM_Install.log"
print_install_result ()
{
	mkdir -m 777 -p $TMLOG_DIR
	echo "[InstallResult]" > $INSTALL_LOG
	date -u "+StartTime=%Y-%m-%dT%H:%M:%SZ" >> $INSTALL_LOG
}

# Install

clean_and_create_lock ()
{
	date -u "+StartTime=%Y-%m-%dT%H:%M:%SZ" > "$INSTALL_LOCK"
	rm -f "$UPGRADE_LOCK"
	# create the file to indicate it's a fresh installation
	date -u "+StartTime=%Y-%m-%dT%H:%M:%SZ" > "$FRESH_INSTALL_LOCK"

	# create the file to indicate daemon need to write install time to EPStatus.plist
	touch "$INSTALL_TIME_LOCK"
	chmod 744 "$INSTALL_TIME_LOCK"
	
	return 0
}

uninstall_tmsm_sa ()
{
	uninst_sh="$TM_DIR/uninstall/uninstall.sh"
	if [ -f "$uninst_sh" ]; then
		sh "$uninst_sh" 2>/dev/null 1>/dev/null
		ret=$?
		echo "uninstalled tmsm-sa rc=$ret" >> $DBGLOG
	fi
	return 0
}

clear_configure ()
{
	rm -rf "${TMP_DIR}"
	return 0
}

unload_core_service ()
{
	/sbin/SystemStarter stop iCoreService
	echo "Unload iCoreService 1.5 (rc=$?)" >>$DBGLOG
	
	launchctl unload "/Library/LaunchDaemons/com.trendmicro.tmsm.launcher.plist"
	launchctl unload "/Library/LaunchDaemons/com.trendmicro.tmsm.plugin.plist"
	launchctl unload "/Library/LaunchDaemons/com.trendmicro.icore.wp.plist"
	launchctl unload "/Library/LaunchDaemons/com.trendmicro.icore.av.plist"
	launchctl unload "/Library/LaunchDaemons/com.trendmicro.icore.main.plist"

	# avoid install failure return 0
	return 0
}

unload_uimgmt_service ()
{
	# if $UPGRADE_LOCK exist, tmsm is installed by mpm upgrade flow
	if [ ! -f "$UPGRADE_LOCK" ]; then
		LoginUser=$(ls -l /dev/console | awk '{print $3}')
		LoginUID=$(id -u $LoginUser)
		#echo "Login user: ${LoginUser}"
		launchctl asuser $LoginUID launchctl unload "/Library/LaunchAgents/com.trendmicro.TmLoginMgr.plist"
		
		killall TmLoginMgr 2>/dev/null >/dev/null
		echo "Terminating TmLoginMgr(rc=$?)" >>$DBGLOG
	fi

	# enable lock for TmLoginMgr
	touch "$UNINSTALL_LOCK"
	chmod 744 "$UNINSTALL_LOCK"
	killall UIMgmt 2>/dev/null
	ret=$?
	echo "stop quick menu rc=$ret" >> $DBGLOG
	sleep 2
	rm -f "$UNINSTALL_LOCK"
	rm -f "$UIMGMT_PLIST" 2>/dev/null >/dev/null

	# avoid install failure return 0
	return 0
}

stop_mainui ()
{
	killall "Trend Micro Common Client for Mac" 2>/dev/null >/dev/null
	killall "MainUI" 2>/dev/null >/dev/null
	killall "TrendMicroSecurity" 2>/dev/null >/dev/null
	return 0
}

remove_env_flag ()
{
	rm -f ${UNINSTALL_LOCK}
}

# Upgrade

create_lock ()
{
	date -u "+StartTime=%Y-%m-%dT%H:%M:%SZ" > "$INSTALL_LOCK"
	# remove the file to indicate it's a upgrade installation
	rm -f "$FRESH_INSTALL_LOCK"

	# create the file to indicate daemon need to write upgrade time to EPStatus.plist
	touch "$UPGRADE_TIME_LOCK"
	chmod 744 "$UPGRADE_TIME_LOCK"
	
	return 0
}

backup_configure ()
{
	rm -rf "${TMP_DIR}"
	if [ ! -d "${TM_DIR}/common/conf" ]; then
		return 0;
	fi

	mkdir -p "${TMP_DIR}"
	mkdir -p "${TMP_DIR}/conf"
	mkdir -p "${TMP_DIR}/vsapi"
	mkdir -p "${TMP_DIR}/CRC"
	mkdir -p "${TMP_DIR}/DCE"
	
	# backup vsapi pattern
	for i in ${PTN_NORMAL_DIR} ${PTN_MAC_DIR} ; do \
		ptn_src="${TM_DIR}/common/lib/vsapi/$i"
		ptn_dst="${TMP_DIR}/vsapi/$i"
		mkdir -p "${ptn_dst}"
		cp -f  "${ptn_src}/"* "$ptn_dst"
	done

	# backup icrc pattern
	mkdir -p "${TMP_DIR}/CRC/ptn"
	cp -f "${TM_DIR}/common/lib/CRC/ptn/"* "${TMP_DIR}/CRC/ptn"

	# backup idce pattern
	mkdir -p "${TMP_DIR}/DCE/ptn"
	cp -f "${TM_DIR}/common/lib/DCE/ptn/"* "${TMP_DIR}/DCE/ptn"

	cp -f "${TM_DIR}/common/lib/vsapi/ssaptn"* "${TMP_DIR}/vsapi/"
	cp -f "${TM_DIR}/common/conf/EPStatus.plist" "${TMP_DIR}/conf"
	cp -f "${TM_DIR}/common/conf/tmccmac.conf.plist" "${TMP_DIR}/conf"
	cp -f "${TM_DIR}/common/conf/tmcccore.conf.plist" "${TMP_DIR}/conf"
	
	return 0
}

preupgrade_uimgmt_service ()
{
	# if $UPGRADE_LOCK does not exist, tmsm is upgraded by installer instead of icore
	if [ ! -f "$UPGRADE_LOCK" ]; then
		LoginUser=$(ls -l /dev/console | awk '{print $3}')
		LoginUID=$(id -u $LoginUser)
		#echo "Login user: ${LoginUser}"
		launchctl asuser $LoginUID launchctl unload "/Library/LaunchAgents/com.trendmicro.TmLoginMgr.plist"
		
		killall TmLoginMgr 2>/dev/null >/dev/null
		echo "Terminating TmLoginMgr(rc=$?)" >>$DBGLOG
	fi
	killall UIMgmt 2>/dev/null >/dev/null
	# avoid install failure return 0
	return 0
}

PKG_LIST="tmcoreinst.pkg tmsecurity.pkg tmsecurityextra.pkg tmappextra.pkg autostart.pkg core.pkg corekext.pkg"
remove_receipt_pkgs ()
{
	for i in $PKG_LIST ; do
		rm -rf "/Library/Receipts/$i"
	done
	return 0
}

# main
clean_time_locks
is_fresh_install
toInstall=$?
if [ 0 -eq $toInstall ]; then
	echo " ==== Warning: TMSM is installed ==== "
fi
echo " ==== Fresh Install ==== "
for funct in print_install_result clean_and_create_lock uninstall_tmsm_sa clear_configure stop_mainui remove_env_flag unload_uimgmt_service unload_core_service; do
	echo " == start executing $funct == "
	$funct
	ret=$?
	if [ $ret -ne 0 ]; then	
		echo "Status=1" >> $INSTALL_LOG
		echo "Error=$ret" >> $INSTALL_LOG
		date -u "+FinishTime=%Y-%m-%dT%H:%M:%SZ" >> $INSTALL_LOG
		echo "Reason=$funct error" >> $INSTALL_LOG
		exit $ret
	fi
done
exit 0
