#!/bin/sh

OS_ARCH=$(uname -m)

SERVICE_INSTALL_PATH="/opt"

BOOLEAN_TRUE="true"
BOOLEAN_FALSE="false"
MATCH_PHRASE_64BIT="64"
MATCH_PHRASE1_ARM="arm"
MATCH_PHRASE2_ARM="aarch"
QUIET="-q"
NOT_QUIET="-nq"

PRINT_OUTPUT=$NOT_QUIET

DOWNLOAD_URL=""
PACKAGE_ZIP=""

IS_ARM=$BOOLEAN_FALSE
IS_NOTARM=$BOOLEAN_FALSE
IS_32BIT=$BOOLEAN_FALSE
IS_64BIT=$BOOLEAN_FALSE

PrintToTerminal() {
	if [ $PRINT_OUTPUT = $NOT_QUIET ]; then
		echo "$@"
	fi
}

############### AppManager ###############

PACKAGE_NAME="AppManagerDataExporter"
SUPPORT_MAILID="appmanager-support@manageengine.com"
LICENSE_KEY="${APPMANAGER_LICENSE_KEY}"

SetDownloadDomain() {

	DOWNLOAD_DOMAIN="https://www.manageengine.com"

}

SetDownloadPath() {

	DOWNLOAD_PATH="products/applications_manager/54974026"

}

SetPackageZip() {

    #Have only true check
	if [ "$IS_NOTARM" = "$BOOLEAN_TRUE" ] && [ "$IS_32BIT" = "$BOOLEAN_TRUE" ]; then
		PACKAGE_ZIP="AppManagerDataExporter_386.zip"
	elif [ "$IS_NOTARM" = "$BOOLEAN_TRUE" ] && [ "$IS_64BIT" = "$BOOLEAN_TRUE" ]; then
		PACKAGE_ZIP="AppManagerDataExporter_amd64.zip"
	elif [ "$IS_ARM" = "$BOOLEAN_TRUE" ] && [ "$IS_32BIT" = "$BOOLEAN_TRUE" ]; then
		PACKAGE_ZIP="AppManagerDataExporter_arm.zip"
	elif [ "$IS_ARM" = "$BOOLEAN_TRUE" ] && [ "$IS_64BIT" = "$BOOLEAN_TRUE" ]; then
		PACKAGE_ZIP="AppManagerDataExporter_arm64.zip"
	else
		PrintToTerminal "Info: $OS_ARCH not supported in this version"
		ContactSupport
	fi

}

SetDownloadURL() {
    PrintToTerminal "Action: Setting Download url"

    SetDownloadDomain
    SetDownloadPath
    SetPackageZip

    DOWNLOAD_URL="${DOWNLOAD_DOMAIN}/${DOWNLOAD_PATH}/${PACKAGE_ZIP}"

    PrintToTerminal "Info: Download URL set as $DOWNLOAD_URL"
}

##########################################

ContactSupport() {
	PrintToTerminal "Info: Installation failed, Kindly contact support $SUPPORT_MAILID"
	exit 1
}

CheckFlags() {
	for arg in "$@"; do
		if [ "$arg" = $QUIET ]; then
			PRINT_OUTPUT=$QUIET
		fi
	done
}

CheckBit() {
	PrintToTerminal "Action: Checking if Operating System is 32 or 64 bit"

	if echo "${OS_ARCH}" | grep -i -q "${MATCH_PHRASE_64BIT}"; then
		IS_64BIT=$BOOLEAN_TRUE
		PrintToTerminal "Info: Detected as 64bit"
	else
		IS_32BIT=$BOOLEAN_TRUE
		PrintToTerminal "Info: Detected as 32bit"
	fi
}

CheckARM() {
	PrintToTerminal "Action: Checking if ARM achitecture"

	if echo "${OS_ARCH}" | grep -i -q "${MATCH_PHRASE1_ARM}"; then
		IS_ARM=$BOOLEAN_TRUE
		PrintToTerminal "Info: Detected as ARM"
	elif echo "${OS_ARCH}" | grep -i -q "${MATCH_PHRASE2_ARM}"; then
		IS_ARM=$BOOLEAN_TRUE
		PrintToTerminal "Info: Detected as ARM"
	else
		IS_NOTARM=$BOOLEAN_TRUE
		PrintToTerminal "Info: Detected as not ARM"
	fi
}

DownloadPackage() {
	PrintToTerminal "Action: Downloading package"
	cd "$SERVICE_INSTALL_PATH" || {
		PrintToTerminal "Change Directory failed, check directory name"
		exit 1
	}

	wget -O "$PACKAGE_ZIP" "$DOWNLOAD_URL"
}

RemoveDownloads() {
	PrintToTerminal "Action: Removing downloaded files"

	#condition is added for safety, to avoid unwanted removal of necesssary files
	case "${PACKAGE_ZIP}" in
	${PACKAGE_NAME}*)
		ZIP_LOCATION="$INSTALL_PATH/$PACKAGE_ZIP"
		rm "$ZIP_LOCATION"
		;;
	*)
		PrintToTerminal "Error: Unable to remove downloaded files"
		;;
	esac
}

VerifyChecksum() {
	PrintToTerminal "Action: Verifying checksum"

	ZIP_LOCATION="$INSTALL_PATH/$PACKAGE_ZIP"
	ZIP_CHECKSUM=$(sha256sum "$ZIP_LOCATION" | awk '{print $1}')

	CHECKSUM_URL="${DOWNLOAD_URL}.sha256"
	LIVE_CHECKSUM="$(curl -sL $CHECKSUM_URL | awk '{print $1}')"

	PrintToTerminal "Info: Checksum of downloaded agent: $ZIP_CHECKSUM"
	PrintToTerminal "Info: Checksum of live agent: $LIVE_CHECKSUM"

	if [ "$ZIP_CHECKSUM" != "$LIVE_CHECKSUM" ]; then
		PrintToTerminal "Status: Checksum validation failed. Please retry after some time"
		RemoveDownloads
		ContactSupport
	else
		PrintToTerminal "Status: Checksum verification successful"
	fi
}

UnzipPackage() {
	PrintToTerminal "Action: Unzipping package"

	cd "$SERVICE_INSTALL_PATH" || {
		PrintToTerminal "Info: Change Directory failed, check directory name"
		exit 1
	}
	unzip -o "$PACKAGE_ZIP" || {
		PrintToTerminal "Info: Unzip failed"
		exit 1
	}
}

InstallPackage() {
	PrintToTerminal "Action: Installing package"

	sh "$SERVICE_INSTALL_PATH/$PACKAGE_NAME/bin/service.sh" install "$@"
}

SetLicenseKey() {

	PrintToTerminal "Action: Checking for Licensekey"

	if [ -z "$LICENSE_KEY" ]; then

		for arg in "$@"; do
			PARAM=$arg
			VALUE="$2"

			case $PARAM in
			-lk | -license.key)
				LICENSE_KEY="$VALUE"
				;;
			esac
			shift
		done

		if [ -n "$LICENSE_KEY" ]; then
			PrintToTerminal "Info: License key updated from CLI"
		fi
	else
		PrintToTerminal "Info: License key updated from ENV"
	fi

	if [ -z "$LICENSE_KEY" ]; then
		PrintToTerminal "Info: License key not found in ENV / CLI"
		ContactSupport
	fi
}

main() {
	CheckFlags "$@"
	SetLicenseKey "$@"
	CheckBit
	CheckARM
	SetDownloadURL
	DownloadPackage
	VerifyChecksum
	UnzipPackage
	RemoveDownloads
	InstallPackage "$@"
}

main "$@"
