---

- name: ensure temp directory exists
  win_file:
    path: '{{ temp_directory }}'
    state: directory

- name: download bleachbit
  win_get_url:
    url: '{{ bleachbit_download_url }}'
    dest: '{{ temp_directory }}\BleachBit-portable.zip'
  register: download_bleachbit
  until: download_bleachbit is success
  delay: 3
  retries: 5
  when: bleachbit_download_url is defined

- name: unzip bleachbit
  win_unzip:
    src: '{{ temp_directory }}\BleachBit-portable.zip'
    dest: '{{ ansible_user_dir }}'
  when: download_bleachbit is success

# This is needed on 2008 for bleachbit to work
- name: install the visual C libraries
  win_package:
    path: https://download.microsoft.com/download/5/D/8/5D8C65CB-C849-4025-8E95-C3966CAFD8AE/vcredist_x86.exe
    product_id: '{9BE518E6-ECC6-35A9-88E4-87755C07200F}'
    arguments: '/qb!'
  when: "'Windows Server 2008' in ansible_distribution"

- name: stop windows update service
  win_service:
    name: wuauserv
    state: stopped
  ignore_errors: yes

- name: delete update directory
  win_file:
    path: C:\Windows\SoftwareDistribution\Download
    state: absent
  ignore_errors: yes

- name: remove windows update settings
  win_regedit:
    path: HKLM:\Software\Microsoft\Windows\CurrentVersion\WindowsUpdate
    name: "{{ item }}"
    state: absent
  loop:
    - SusClientId
    - PingID
    - AccountDomainSid

- name: start windows update service
  win_service:
    name: wuauserv
    state: started
  ignore_errors: yes

- name: create update directory
  win_file:
    path: C:\Windows\SoftwareDistribution\Download
    state: directory
  ignore_errors: yes

- name: reset windows update
  win_shell: wuauclt /resetauthorization /detectnow
  ignore_errors: yes

- name: clean with bleachbit
  win_shell: >
    '{{ ansible_user_dir }}\BleachBit-Portable\bleachbit_console.exe --clean deepscan.backup
    deepscan.ds_store deepscan.thumbs_db deepscan.tmp internet_explorer.cookies internet_explorer.forms
    internet_explorer.history internet_explorer.temporary_files system.clipboard system.custom system.logs
    system.memory_dump system.muicache system.prefetch system.recycle_bin system.tmp system.updates
    windows_defender.history windows_explorer.mru windows_explorer.recent_documents windows_explorer.run
    windows_explorer.search_history windows_explorer.thumbnails > NUL'
  args:
    executable: cmd
  when:
    - bleachbit_clean|bool
    - download_bleachbit is success
  ignore_errors: yes

- name: create temp directory
  win_file:
    path: '{{ temp_directory }}\win_build'
    state: directory

- name: download ultradefrag
  win_get_url:
    url: '{{ ultradefrag_download_url }}'
    dest: '{{ temp_directory }}\win_build\ultradefrag.zip'
  register: download_ultradefrag
  until: download_ultradefrag is success
  delay: 3
  retries: 5

- name: unzip ultradefrag
  win_unzip:
    src: '{{ temp_directory }}\win_build\ultradefrag.zip'
    dest: '{{ temp_directory }}\win_build'

- name: set udefrag extract directory
  set_fact:
    udefrag_dir: '{{ temp_directory }}\win_build\ultradefrag-portable-7.1.3.amd64'

- name: defrag with ultradefrag
  win_shell: '{{ udefrag_dir }}\udefrag.exe --optimize --repeat C:'
  args:
    executable: cmd

- name: download sdelete
  win_get_url:
    url: '{{ sdelete_download_url }}'
    dest: '{{ temp_directory }}\win_build\SDelete.zip'
  register: download_sdelete
  until: download_sdelete is success
  delay: 3
  retries: 5
  when: sdelete_download_url is defined

- name: copy sdelete
  win_copy:
    src: SDelete.zip
    dest: '{{ temp_directory }}\win_build\SDelete.zip'
  when: sdelete_download_url is undefined

- name: unzip sdelete
  win_unzip:
    src: '{{ temp_directory }}\win_build\SDelete.zip'
    dest: '{{ temp_directory }}\win_build'

- name: accept sdelete eula
  win_regedit:
    path: HKCU:\Software\Sysinternals\SDelete
    name: EulaAccepted
    data: 1
    type: dword

- name: compact with sdelete
  win_shell: '{{ temp_directory }}\win_build\sdelete.exe -q -z C:'
  args:
    executable: cmd

- name: remove temp files
  win_file:
    path: '{{ temp_directory }}\win_build'
    state: absent

- name: free disk space with bleachbit
  win_shell: '{{ ansible_user_dir }}\BleachBit-Portable\bleachbit_console.exe --clean system.free_disk_space'
  args:
    executable: cmd
  when:
    - bleachbit_free_disk_space|bool
    - download_bleachbit is success
  ignore_errors: yes

- name: remove bleachbit files
  win_file:
    path: '{{ ansible_user_dir }}\BleachBit-Portable'
    state: absent
  when: download_bleachbit is success