sdmmd_return_t SDMMD_AMDeviceSecureInstallApplication(SDMMD_AMConnectionRef conn, SDMMD_AMDeviceRef device, CFURLRef path, CFDictionaryRef options, CallBack installCallback, void *unknown)
{
	sdmmd_return_t result = kAMDSuccess;
	SDMMD_AMConnectionRef connection = NULL;
	bool hasConnection = (conn ? true : false);
	if (device) {
		result = SDMMD_AMDeviceSecureStartService(device, CFSTR(AMSVC_INSTALLATION_PROXY), NULL, &connection);
		if (result == 0) {
			hasConnection = true;
		}
		else {
			printf("%s: Was unable to start the install service on the device 0x%x.\n", __FUNCTION__, result);
		}
	}
	if (hasConnection) {
		CFStringRef lastComp = CFURLCopyLastPathComponent(path);
		if (CFStringGetLength(lastComp) == 0) {
			CFSafeRelease(lastComp);
			CFURLRef pathURL = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, path);
			lastComp = CFURLCopyLastPathComponent(pathURL);
			CFSafeRelease(pathURL);
		}
		if (lastComp) {
			CFStringRef format = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("PublicStaging/%@"), lastComp);
			if (format) {
				char *appName = SDMCFStringGetString(format);
				printf("%s: Attempting install of %s.\n", __FUNCTION__, appName);
				result = SDMMD_perform_command(connection, CFSTR("Install"), 0, installCallback, 4, unknown, CFSTR("PackagePath"), format, CFSTR("ClientOptions"), options);
				if (result) {
					printf("%s: Old style of install failed for (%s).\n", __FUNCTION__, appName);
				}
				Safe(free, appName);
			}
			else {
				printf("%s: Unable to create CFString!\n", __FUNCTION__);
			}
			CFSafeRelease(format);
		}
		else {
			char *lastCompString = SDMCFStringGetString(lastComp);
			printf("%s: Could not copy last path component from url %s.\n", __FUNCTION__, lastCompString);
			Safe(free, lastCompString);
		}
		CFSafeRelease(lastComp);
	}
	CFSafeRelease(connection);
	char *pathString = SDMCFURLGetString(path);
	printf("%s: Installation of package %s returned 0x%x.\n", __FUNCTION__, pathString, result);
	Safe(free, pathString);
	return result;
}
sdmmd_return_t SDMMD_AMDeviceInstallApplication(SDMMD_AMDeviceRef device, CFStringRef path, CFDictionaryRef options, CallBack installCallback, void* unknown) {
	sdmmd_return_t result = kAMDSuccess;
	uint32_t socket = -1;
	SDMMD_AMConnectionRef conn = SDMMD__CreateTemporaryServConn(socket, 0);
	if (conn) {
		CFURLRef url = SDMMD__AMDCFURLCreateFromFileSystemPathWithSmarts(path);
		char *urlString = SDMCFURLGetString(url);
		if (url) {
			result = SDMMD_AMDeviceSecureInstallApplication(conn, device, url, options, installCallback, unknown);
		}
		else {
			printf("%s: SDMMD_AMDCFURLCreateFromFileSystemPathWithSmarts failed on %s.\n",__FUNCTION__,urlString);
		}
		Safe(free, urlString);
		CFSafeRelease(url);
		CFSafeRelease(conn);
	}
	else {
		result = kAMDUndefinedError;
	}
	return result;
}