{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "maharaCommon": {
            "metadata": {
                "description": "Common Mahara values"
            },
            "type": "object"
        },
	"siteFQDN": {
            "metadata": {
                "description": "FQDN of public IP address"
            },
            "type": "string"
       }

    },
    "resources": [
        {
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "apiVersion": "2017-03-30",
            "location": "[parameters('maharaCommon').location]",
            "name": "[concat(parameters('maharaCommon').ctlrVmName,'/','install_mahara')]",
            "properties": {
                "autoUpgradeMinorVersion": true,
                "publisher": "Microsoft.Azure.Extensions",
                "settings": {
                    "fileUris": [
                        "[variables('scriptUri')]",
                        "[parameters('maharaCommon').commonFunctionsScriptUri]"
                    ]
                },
                "protectedSettings":{
                    "commandToExecute": "[concat('bash ', parameters('maharaCommon').maharaInstallScriptFilename, ' ', parameters('maharaCommon').maharaVersion, ' ', concat(parameters('maharaCommon').gfsNameRoot, '0'), ' ', 'data', ' ', parameters('maharaCommon').siteURL, ' ', parameters('maharaCommon').httpsTermination, ' ', parameters('maharaCommon').dbDNS, ' ', parameters('maharaCommon').maharaDbName, ' ', parameters('maharaCommon').maharaDbUser, ' ', parameters('maharaCommon').maharaDbPass, ' ', parameters('maharaCommon').maharaAdminPass, ' ', concat(parameters('maharaCommon').dbLogin, '@', parameters('maharaCommon').dbServerType, '-', parameters('maharaCommon').resourcesPrefix), ' ', parameters('maharaCommon').dbLoginPassword, ' ', parameters('maharaCommon').storageAccountName, ' ', listKeys(variables('storageAccountId'), '2017-06-01').keys[0].value, ' ', parameters('maharaCommon').maharaDbUserAzure, ' ', parameters('maharaCommon').elasticVm1IP, ' ', parameters('maharaCommon').dbServerType, ' ', parameters('maharaCommon').fileServerType , ' ', parameters('maharaCommon').thumbprintSslCert, ' ', parameters('maharaCommon').thumbprintCaCert, ' ', parameters('maharaCommon').searchType, ' ' , parameters('siteFQDN'))]"
                },
                "type": "CustomScript",
                "typeHandlerVersion": "2.0"
            },
            "tags": {
                "displayName": "install_mahara"
            }
        }
    ],
    "variables": {
        "documentation01": "This sub-template applies a specific post-deployment script to the controller vm",
        "documentation02": "It expects certain values in the 'common' datastructure.",
        "documentation03": "    scriptLocation              - web URI",
        "documentation04": "    maharaInstallScriptFilename - name of script file",
        "documentation05": "    siteURL                     - URL of the website",
        "documentation06": "    gfsNameRoot                 - nameroot of gluster farm - note that the code applies a 0 to get to the first node",
        "documentation07": "    ctlrVmName                  - name of the controller/jumpb ox VM",
        "documentation08": "    dbServerType                - postgres or mysql",
        "documentation09": "    maharaDbName                - database name for mahara",
        "documentation10": "    maharaDbUser                - database user for mahara",
        "documentation11": "    maharaDbPass                - database password for maharaDbUser",
        "documentation12": "    maharaAdminPass             - password for mahara admin user",

        "scriptUri": "[concat(parameters('maharaCommon').scriptLocation,parameters('maharaCommon').maharaInstallScriptFilename,parameters('maharaCommon').artifactsSasToken)]",
        "storageAccountId": "[resourceId('Microsoft.Storage/storageAccounts', parameters('maharaCommon').storageAccountName)]"
    }
}
