set context user creator;
verb on;

tcl;
eval {
	proc addCommandToMenu {menuName commandName} {
		if {[adminExists command $commandName] != 1} {
			puts "Cannot add command $commandName to menu $menuName; menu doesn't exist!"
			return -1
		}
		if {[isItemInMenu $menuName $commandName] != 1} {
			mql mod menu $menuName add command $commandName
		}
		return 0
	}

	# Returns 1 if the given menu contains item
	proc isItemInMenu { menuName itemName } {
		if { $menuName == "" } {
			return 1
		}
		if {[adminExists menu $menuName] != 1} {
			return 1
		}
		if { [lsearch [split [mql pri menu $menuName select child dump |] |] $itemName] == -1 } {
			return 0
		}
		return 1
	}

	# Returns 1 if the given administrative object exists, otherwise 0
	proc adminExists { sAdminType sName } {
		if { $sName == "" } {
			return 1
		}
		if { $sAdminType == "systable" } {
			if { [lsearch [split [mql list table system] "\n"] $sName] == -1} {
				return 0
			}
			return 1
		}
		if { [lsearch [split [mql list $sAdminType] "\n"] $sName] == -1 } {
			return 0
		} else {
			return 1
		}
	}
	
	proc getVPMReferenceCategoryMenuNameByMxVersion {} {
		set sVersion [mql version]
		set versionTokens [split $sVersion " "]
		set mainVersion [lindex $versionTokens 1]
		set mainVersionTokens [split $mainVersion "Rx"]
		set sMainVersionNumber [lindex $mainVersionTokens 1]
		set vpmReferenceCategoryMenuName "type_VPMReference" 
		
		if {$sMainVersionNumber >= 2021} {
			set vpmReferenceCategoryMenuName "type_PLMReference"
		}
		puts "Evaluated VPMReference category menuName: $vpmReferenceCategoryMenuName"
		return $vpmReferenceCategoryMenuName
	}


	# Start transaction
	########################################
	mql start transaction

	# Create common Helium category command
	########################################
	set heCommandName "LP-HELIUM-VIEW"
	puts -nonewline "Installing command $heCommandName ..."
	if {[adminExists command $heCommandName] != 1} {
		mql add command $heCommandName label "Helium View (LP)" href "\$\{ROOT_DIR\}/goto/foo.jsp?embed=true" setting "Target Location" "content"
		puts "Created."
	} else {
		mql mod command $heCommandName label "Helium View (LP)" href "\$\{ROOT_DIR\}/goto/foo.jsp?embed=true"
		puts "Updated."
	}

	# Install TVC Home command
	########################################
	set commandName "LP-TVC-Homepage"
	set menuName "My Enovia"
	puts -nonewline "Installing command $commandName ..."
	if {[adminExists command $commandName] != 1} {
		mql add command $commandName label "TVC Homepage (LP)" href "\$\{ROOT_DIR\}/tvc-action/menuBasedTabPage/foo.jsp?menu=tvc:menu:launchpad:common/TVCTabs.xml" setting "Target Location" "content"
		puts "Created."
	} else {
		mql mod command $commandName label "TVC Homepage (LP)" href "\$\{ROOT_DIR\}/tvc-action/menuBasedTabPage/foo.jsp?menu=tvc:menu:launchpad:common/TVCTabs.xml"
		puts "Updated."
	}
	puts -nonewline "Adding command $commandName to menu $menuName..."
	if {[addCommandToMenu $menuName $commandName] != 0} {
		mql abort transaction
		return 1
	}
	puts "Ok."

	# Install Issue category commands
	########################################
	set menuName "type_Issue"
	puts -nonewline "Adding command $heCommandName to menu $menuName..."
	if {[addCommandToMenu $menuName $heCommandName] != 0} {
		mql abort transaction
		return 1
	}
	puts "Ok."

	# Install Document category commands
	########################################
	set menuName "type_DOCUMENTS"
	puts -nonewline "Adding command $heCommandName to menu $menuName..."
	if {[addCommandToMenu $menuName $heCommandName] != 0} {
		mql abort transaction
		return 1
	}
	puts "Ok."

	# Install VPMReference category commands
	########################################
	set commandName "LP-UPS"
	set menuName [getVPMReferenceCategoryMenuNameByMxVersion]
	puts -nonewline "Installing command $commandName ..."
	if {[adminExists command $commandName] != 1} {
		mql add command $commandName label "UPS Dashboard (LP)" href "\$\{ROOT_DIR\}/tvc-action/dashboard/foo.jsp?config=tvc:dashboard:launchpad:ups/UPSDashboard.xml" setting "Target Location" "content"
		puts "Created."
	} else {
		mql mod command $commandName label "UPS Dashboard (LP)" href "\$\{ROOT_DIR\}/tvc-action/dashboard/foo.jsp?config=tvc:dashboard:launchpad:ups/UPSDashboard.xml"
		puts "Updated."
	}
	puts -nonewline "Adding command $commandName to menu $menuName..."
	if {[addCommandToMenu $menuName $commandName] != 0} {
		mql abort transaction
		return 1
	}
	puts "Ok."

	set commandName "LP-UPS-Navigate"
	puts -nonewline "Installing command $commandName ..."
	if {[adminExists command $commandName] != 1} {
		mql add command $commandName label "UPS Navigate (LP)" href "\$\{ROOT_DIR\}/tvc-action/menuBasedTabPage/foo.jsp?menu=tvc:menu:launchpad:ups/tab/NavigateUPS.xml" setting "Target Location" "content"
		puts "Created."
	} else {
		mql mod command $commandName label "UPS Navigate (LP)" href "\$\{ROOT_DIR\}/tvc-action/menuBasedTabPage/foo.jsp?menu=tvc:menu:launchpad:ups/tab/NavigateUPS.xml"
		puts "Updated."
	}
	puts -nonewline "Adding command $commandName to menu $menuName..."
	if {[addCommandToMenu $menuName $commandName] != 0} {
		mql abort transaction
		return 1
	}
	puts "Ok."

	puts -nonewline "Adding command $heCommandName to menu $menuName..."
	if {[addCommandToMenu $menuName $heCommandName] != 0} {
		mql abort transaction
		return 1
	}
	puts "Ok."

	# Install Part category commands
	##########################################
	set commandName "LP-EBOM"
	set menuName "type_Part"
	puts -nonewline "Installing command $commandName ..."
	if {[adminExists command $commandName] != 1} {
		mql add command $commandName label "EBOM (LP)" href "\$\{ROOT_DIR\}/tvc-action/dashboard/foo.jsp?config=tvc:dashboard:launchpad:ebom/EBOMDashboard.xml" setting "Target Location" "content"
		puts "Created."
	} else {
		mql mod command $commandName label "EBOM (LP)" href "\$\{ROOT_DIR\}/tvc-action/dashboard/foo.jsp?config=tvc:dashboard:launchpad:ebom/EBOMDashboard.xml"
		puts "Updated."
	}
	puts -nonewline "Adding command $commandName to menu $menuName..."
	if {[addCommandToMenu $menuName $commandName] != 0} {
		mql abort transaction
		return 1
	}
	puts "Ok."

	puts -nonewline "Adding command $heCommandName to menu $menuName..."
	if {[addCommandToMenu $menuName $heCommandName] != 0} {
		mql abort transaction
		return 1
	}
	puts "Ok."

	# Install Project category commands
	##########################################
	set commandName "LP-Schedule"
	set menuName "type_ProjectSpace"
	puts -nonewline "Installing command $commandName ..."
	if {[adminExists command $commandName] != 1} {
		mql add command $commandName label "Schedule (LP)" href "\$\{ROOT_DIR\}/tvc-action/dashboard/foo.jsp?config=tvc:dashboard:launchpad:schedule/ScheduleDashboard.xml" setting "Target Location" "content"
		puts "Created."
	} else {
		mql mod command $commandName label "Schedule (LP)" href "\$\{ROOT_DIR\}/tvc-action/dashboard/foo.jsp?config=tvc:dashboard:launchpad:schedule/ScheduleDashboard.xml"
		puts "Updated."
	}
	puts -nonewline "Adding command $commandName to menu $menuName..."
	if {[addCommandToMenu $menuName $commandName] != 0} {
		mql abort transaction
		return 1
	}
	puts "Ok."
	
	set commandName "LP-Bookmarks"
	puts -nonewline "Installing command $commandName ..."
	if {[adminExists command $commandName] != 1} {
		mql add command $commandName label "Bookmarks (LP)" href "\$\{ROOT_DIR\}/tvc-action/dashboard/foo.jsp?config=tvc:dashboard:launchpad:folder/FolderDashboard.xml" setting "Target Location" "content"
		puts "Created."
	} else {
		mql mod command $commandName label "Bookmarks (LP)" href "\$\{ROOT_DIR\}/tvc-action/dashboard/foo.jsp?config=tvc:dashboard:launchpad:folder/FolderDashboard.xml"
		puts "Updated."
	}
	puts -nonewline "Adding command $commandName to menu $menuName..."
	if {[addCommandToMenu $menuName $commandName] != 0} {
		mql abort transaction
		return 1
	}
	puts "Ok."
	
	puts -nonewline "Adding command $heCommandName to menu $menuName..."
	if {[addCommandToMenu $menuName $heCommandName] != 0} {
		mql abort transaction
		return 1
	}
	puts "Ok."
	
	# Install Model Version command
	########################################
	set menuName "type_Products"
	puts -nonewline "Adding command $heCommandName to menu $menuName..."
	if {[addCommandToMenu $menuName $heCommandName] != 0} {
		mql abort transaction
		return 1
	}
	puts "Ok."

	#
	# commit transaction
	puts -nonewline "Commiting transaction ..."
	mql commit transaction
	puts "Ok."
}