&"$PSScriptRoot\..\vars.ps1"
&"$PSScriptRoot\..\vars-config.ps1"

Write-Host ""

$dirSess = (Get-PhpIniKey "session.save_path" $PHPdir) -replace '"','' 
$dirUpload = (Get-PhpIniKey "upload_tmp_dir" $PHPdir) -replace '"',''
$dirCache = (Get-PhpIniKey "soap.wsdl_cache_dir" $PHPdir) -replace '"',''
$errorLog = (Get-PhpIniKey "error_log" $PHPdir) -replace '"',''

Write-Host "PHP Path : session.save_path : $dirSess"
Write-Host "PHP Path : upload_tmp_dir : $dirUpload"
Write-Host "PHP Path : soap.wsdl_cache_dir : $dirCache"
Write-Host "PHP Path : error_log : $errorLog"
Write-Host ""

[void](New-Item -Path $dirSess -ItemType Directory -ErrorAction Continue -Force)
[void](New-Item -Path $dirUpload -ItemType Directory -ErrorAction Continue -Force)
[void](New-Item -Path $dirCache -ItemType Directory -ErrorAction Continue -Force)
[void](New-Item -Path $errorLog -ItemType File -ErrorAction Continue -Force)

if ($null -eq $CEDAR_INSTALL_USERS -Or [string]::IsNullOrEmpty($CEDAR_INSTALL_USERS)) {
    $CEDAR_INSTALL_USERS = @("IUSR", "IIS_IUSRS", $ENV:UserName)
}

Write-Host "Setting permissions to these users"
Write-Host $CEDAR_INSTALL_USERS

$p = (Get-Item $dirSess).FullName
$acl = Get-Acl $p
ForEach ($usr in $CEDAR_INSTALL_USERS)
{
    $a = New-Object System.Security.AccessControl.FileSystemAccessRule($usr, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
    $acl.AddAccessRule($a)
}
$acl.SetAccessRuleProtection($false, $true)
Set-Acl $p $acl

$p = (Get-Item $dirUpload).FullName
$acl = Get-Acl $p
ForEach ($usr in $CEDAR_INSTALL_USERS)
{
    $a = New-Object System.Security.AccessControl.FileSystemAccessRule($usr, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
    $acl.AddAccessRule($a)
}
$acl.SetAccessRuleProtection($false, $true)
Set-Acl $p $acl

$p = (Get-Item $dirCache).FullName
$acl = Get-Acl $p
ForEach ($usr in $CEDAR_INSTALL_USERS)
{
    $a = New-Object System.Security.AccessControl.FileSystemAccessRule($usr, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
    $acl.AddAccessRule($a)
}
$acl.SetAccessRuleProtection($false, $true)
Set-Acl $p $acl

$p = (Get-Item $errorLog).FullName
$acl = Get-Acl $p
ForEach ($usr in $CEDAR_INSTALL_USERS)
{
    $a = New-Object System.Security.AccessControl.FileSystemAccessRule($usr, "FullControl", "Allow")
    $acl.AddAccessRule($a)
}
$acl.SetAccessRuleProtection($false, $true)
Set-Acl $p $acl

Write-Host "Permission setting complete"
Write-Host ""
