---
- name: ensure the testing directory is cleared before setting up test
  win_file:
    path: '{{win_find_dir}}'
    state: absent

# while most of the setup can be done with modules, it is quicker to do them
# all in bulk than with with_items to save each round trip over WinRM
- name: set test files and folders
  win_shell: |
    $directories = @(
        "nested",
        "single",
        "link-dest\sub-link",
        "hard-link-dest",
        "junction-link-dest",
        "broken-link-dest",
        "nested\sub-nest",
        "shared\folder",
        "hidden",
        "date",
        "emptynested\nest\dir1",
        "emptynested\nest\dir2"
    )

    $tmp_dir = '{{ win_find_dir }}'
    foreach ($directory in $directories) {
        New-Item -Path "$tmp_dir\$directory" -ItemType Directory
    }

    $normal_content = "abcdefg1234567"
    $normal_files = @(
        "nested\file.ps1",
        "nested\test.ps1",
        "nested\out.log",
        "nested\archive.log",
        "nested\sub-nest\test.ps1",
        "nested\sub-nest\readonly.txt",
        "link-dest\link.ps1",
        "single\test.ps1",
        "single\hidden.ps1",
        "single\out_20161101-091005.log",
        "hidden\out_20161101-091005.log",
        "hard-link-dest\file-abc.log"
    )
    foreach ($file in $normal_files) {
        New-Item -Path "$tmp_dir\$file" -ItemType File
        [System.IO.File]::WriteAllText("$tmp_dir\$file", $normal_content)
    }

    New-Item -Path "$tmp_dir\single\small.ps1" -ItemType File
    [System.IO.File]::WriteAllText("$tmp_dir\single\small.ps1", "a")

    New-Item -Path "$tmp_dir\date\new.ps1" -ItemType File
    [System.IO.File]::WriteAllText("$tmp_dir\date\new.ps1", "random text for new date")

    New-Item -Path "$tmp_dir\date\old.ps1" -ItemType File
    [System.IO.File]::WriteAllText("$tmp_dir\date\old.ps1", "random text for old date")

    New-Item -Path "$tmp_dir\single\large.ps1" -ItemType File
    Set-Content -LiteralPath "$tmp_dir\single\large.ps1" -Value ('abcdefghijklmnopqrstuvwxyz' * 10000)

    $share_stat = Get-WmiObject -Class Win32_Share -Filter "name='folder-share'"
    if ($share_stat) {
        $share_stat.Delete()
    }
    $wmi = [wmiClass] 'Win32_Share'
    $wmi.Create("$tmp_dir\shared\folder", "folder-share", 0)

    cmd.exe /c mklink /D "$tmp_dir\nested\link" "$tmp_dir\link-dest"
    cmd.exe /c mklink /D "$tmp_dir\broken-link" "$tmp_dir\broken-link-dest"
    cmd.exe /c mklink /H "$tmp_dir\hard-link-dest\hard-link.log" "$tmp_dir\hard-link-dest\file-abc.log"
    cmd.exe /c mklink /J "$tmp_dir\junction-link" "$tmp_dir\junction-link-dest"

    $date = Get-Date -Year 2016 -Month 11 -Day 1 -Hour 7 -Minute 10 -Second 5 -Millisecond 0
    Set-Location -LiteralPath $tmp_dir
    Get-ChildItem -Recurse | Where-Object { $_.Name -ne "new.ps1" } | ForEach-Object {
        $_.CreationTime = $date
        $_.LastAccessTime = $date
        $_.LastWriteTime = $date
    }
    Pop-Location

    $attributes = @{
        "hidden" = "Hidden"
        "date" = "Hidden"
        "nested\archive.log" = "Archive"
        "nested\sub-nest\readonly.txt" = "ReadOnly"
        "single\hidden.ps1" = "Hidden"
    }
    foreach ($attribute in $attributes.GetEnumerator()) {
        $item = Get-Item -LiteralPath "$tmp_dir\$($attribute.Name)"
        $file_attributes = $item.Attributes -split ','
        if ($file_attributes -notcontains $attribute.Value) {
            $file_attributes += $attribute.Value
        }
        $item.Attributes = $file_attributes -join ','
    }

    Remove-Item -LiteralPath "$tmp_dir\broken-link-dest" -Force

- block:
  - include_tasks: tests.yml

  always:
  - name: remove test user
    win_user:
      name: '{{test_win_find_username}}'
      state: absent

  - name: remove testing folder
    win_file:
      path: '{{win_find_dir}}'
      state: absent
