- name: make sure symlink file does not exist
  test_symlink_file:
    src: '{{win_stat_dir}}\file-link.txt'
    state: absent

- name: remove win_stat testing directories for clean slate
  win_file:
    path: '{{win_stat_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: |
    $ErrorActionPreference = "Stop"
    $directories = @(
        "folder",
        "folder space",
        "nested\nested",
        "shared",
        "hidden",
        "link-dest",
        "junction-dest"
    )

    foreach ($directory in $directories) {
        New-Item -Path "{{win_stat_dir}}\$directory" -ItemType Directory
    }

    $normal_content = "abc"
    $normal_files = @(
        "nested\file.ps1",
        "nested\hard-target.txt",
        "nested\read-only.ps1",
        "nested\archive.ps1",
        "nested\hidden.ps1",
        "nested\nested\file.ps1",
        "folder space\file.ps1",
        "link-dest\file.txt"
    )
    foreach ($file in $normal_files) {
        New-Item -Path "{{win_stat_dir}}\$file" -ItemType File
        [System.IO.File]::WriteAllText("{{win_stat_dir}}\$file", $normal_content)
    }

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

    cmd.exe /c mklink /D "{{win_stat_dir}}\link" "{{win_stat_dir}}\link-dest"
    cmd.exe /c mklink /H "{{win_stat_dir}}\nested\hard-link.ps1" "{{win_stat_dir}}\nested\hard-target.txt"
    cmd.exe /c mklink /J "{{win_stat_dir}}\junction-link" "{{win_stat_dir}}\junction-dest"
    cmd.exe /c mklink /D "{{win_stat_dir}}\nested\nested\link-rel" "..\..\link-dest"
    cmd.exe /c mklink /D "{{win_stat_dir}}\outer-link" "{{win_stat_dir}}\nested\nested\link-rel"

    $date = Get-Date -Year 2016 -Month 11 -Day 1 -Hour 7 -Minute 10 -Second 5 -Millisecond 0
    Get-ChildItem -Path "{{win_stat_dir}}" -Recurse | ForEach-Object {
        $_.CreationTime = $date
        $_.LastAccessTime = $date
        $_.LastWriteTime = $date
    }

    $attributes = @{
        "hidden" = "Hidden"
        "nested\read-only.ps1" = "ReadOnly"
        "nested\archive.ps1" = "Archive"
        "nested\hidden.ps1" = "Hidden"
    }

    foreach ($attribute in $attributes.GetEnumerator()) {
        $item = Get-Item -Path "{{win_stat_dir}}\$($attribute.Name)"
        $file_attributes = $item.Attributes -split ','
        if ($file_attributes -notcontains $attribute.Value) {
            $file_attributes += $attribute.Value
        }
        $item.Attributes = $file_attributes -join ','
    }

    # weird issue, need to access the file in anyway to get the correct date stats
    Test-Path {{win_stat_dir}}\nested\hard-link.ps1

# mklink.exe and win_file cannot do this right now so we need a custom module
- name: create file symlink
  test_symlink_file:
    src: '{{win_stat_dir}}\file-link.txt'
    target: '{{win_stat_dir}}\nested\file.ps1'
    state: present

- block:
  - include_tasks: tests.yml

  always:
  - name: make sure symlink file does not exist
    test_symlink_file:
      src: '{{win_stat_dir}}\file-link.txt'
      state: absent

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

  - name: ensure test user is deleted
    win_user:
      name: '{{win_stat_user}}'
      state: absent

  - name: ensure test user profile is deleted
    win_shell: rmdir /S /Q {{profile_dir_out.stdout_lines[0]}}
    args:
      executable: cmd.exe
    when: win_stat_user in profile_dir_out.stdout_lines[0]
