- hosts: localhost
  gather_facts: no
  vars:
    release_dir: "./ansible_release"
    release_date: "{{lookup('pipe', 'date +\"%m-%d-%Y\"')}}"
    rpm_spec_line: |

      * {{lookup('pipe', 'date +"%a %b %d %Y"')}} Ansible, Inc. <support@ansible.com> - {{ansible_release_version}}-{{ansible_release_string}}
      - Release {{ansible_release_version}}-{{ansible_release_string}}
    deb_changelog_line: |

      ansible ({{ansible_release_version}}) unstable; urgency=low
      
        * {{ansible_release_version}}
      
       -- Ansible, Inc. <support@ansible.com>  {{lookup('pipe', 'date -R')}}
    versions:
       - 2.2:
         code_name: "TBD"
         releases:
           - 0: ""
       - 2.1:
         code_name: "The Song Remains the Same"
         releases:
           - 0: "05-25-2016"
       - 2.0:
         code_name: "Over the Hills and Far Away"
         releases:
           - 2: "04-19-2016"
           - 1: "02-24-2016"
           - 0: "01-12-2016"
       - 1.9:
         code_name: "Dancing In the Streets"
         releases:
           - 6: "04-15-2016"
           - 5: "03-21-2016"
           - 4: "10-09-2015"
           - 3: "09-03-2015"
           - 2: "06-24-2015"
           - 1: "04-27-2015"
           - 0: "03-25-2015"
       - 1.8:
         code_name: "You Really Got Me"
         releases:
           - 4: "02-19-2015"
           - 3: "02-17-2015"
           - 2: "12-04-2014"
           - 1: "11-26-2014"
           - 0: "?"
       - 1.7:
         code_name: "Summer Nights"
         releases:
           - 2: "09-24-2014"
           - 1: "08-14-2014"
           - 0: "08-06-2014"
       - 1.6:
         code_name: "The Cradle Will Rock"
         releases:
           - 10: "07-25-2014"
           - 9: "07-24-2014"
           - 8: "07-22-2014"
           - 7: "07-21-2014"
           - 6: "07-01-2014"
           - 5: "06-25-2014"
           - 4: "06-25-2014"
           - 3: "06-09-2014"
           - 2: "05-23-2014"
           - 1: "05-07-2014"
           - 0: "05-05-2014"
       - 1.5:
         code_name: "Love Walks In"
         releases:
           - 5: "04-18-2014"
           - 4: "04-01-2014"
           - 3: "03-13-2014"
           - 2: "03-11-2014"
           - 1: "03-10-2014"
           - 0: "02-28-2014"
       - 1.4:
         code_name: "Could This Be Magic?"
         releases:
           - 5: "02-12-2014"
           - 4: "01-06-2014"
           - 3: "12-20-2013"
           - 2: "12-18-2013"
           - 1: "11-27-2013"
           - 0: "11-21-2013"
       - 1.3:
         code_name: "Top of the World"
         releases:
           - 4: "10-29-2013"
           - 3: "10-09-2013"
           - 2: "09-19-2013"
           - 1: "09-16-2013"
           - 0: "09-13-2013"
       - 1.2:
         code_name: "Right Now"
         releases:
           - 3: "08-21-2013"
           - 2: "07-05-2013"
           - 1: "07-04-2013"
           - 0: "06-10-2013"
       - 1.1:
         code_name: "Mean Street"
         releases:
           - 0: "04-02-2013"
       - 1.0:
         code_name: "Eruption"
         releases:
           - 0: "02-01-2013"
       - 0.9:
         code_name: "Dreams"
         releases:
           - 0: "11-30-2012"
       - 0.8:
         code_name: "Cathedral"
         releases:
           - 0: "10-19-2012"
       - 0.7:
         code_name: "Panama"
         releases:
           - 0: "09-06-2012"
       - 0.6:
         code_name: "Cabo"
         releases:
           - 0: "08-06-2012"
       - 0.5:
         code_name: "Amsterdam"
         releases:
           - 0: "07-04-2012"
       - 0.4:
         code_name: "Unchained"
         releases:
           - 0: "05-23-2012"
       - 0.3:
         code_name: "Baluchitherium"
         releases:
           - 0: "04-23-2012"
       - 0.2:
         code_name: "Untitled"
         releases:
           - 0: "?"
       - 0.1:
         code_name: "Untitled"
         releases:
           - 0: "?"
       - 0.0:
         code_name: "Untitled"
         releases:
           - 2: "?"
           - 1: "?"

  vars_prompt:
  - name: ansible_release_branch
    prompt: "Enter the release branch"
    private: no
  - name: ansible_release_version
    prompt: "Enter the release version"
    private: no
  - name: ansible_release_string
    prompt: "Enter the release string (ie. 0.1.beta1, or just 1 for final releases)"
    private: no
  #- name: ansible_release_codename
  #  prompt: "Enter the release code name (only used if doing a final release)"
  #  default: ""
  #  private: no
  - name: is_final
    prompt: "Is this a final release (not a beta/rc)?"
    default: "no"
    private: no
  - name: do_push
    prompt: "Push repositories upstream when done?"
    default: "no"
    private: no
  tasks:
  - name: create a combined version string from the specified values
    set_fact: 
      new_version: "v{{ansible_release_version}}-{{ansible_release_string}}"

  - name: assert certain variables are defined
    assert:
      that:
      - ansible_release_branch is defined
      - ansible_release_version is defined
      - ansible_release_string is defined

  - name: Remove ansible_release (if it exists)
    file:
      path: "{{release_dir}}/"
      state: absent

  - name: Clone the official repo
    git:
      repo: "git@github.com:ansible/ansible.git"
      dest: "{{release_dir}}"
      version: "{{ansible_release_branch}}"
      recursive: yes

  - name: get the latest version
    shell:
      _raw_params: git tag | tail -1
      chdir: "{{release_dir}}"
    register: latest_version

  - name: "assert the specified version ({{new_version}}) is greater than the latest version ({{latest_version.stdout}})"
    assert:
      that:
      - new_version|version_compare(latest_version.stdout, "gt")
    ignore_errors: yes

  - name: Update the VERSION file for the main repo
    copy:
      dest: "{{release_dir}}/VERSION"
      content: "{{ansible_release_version}} {{ansible_release_string}}\n"

  - name: Update the library version
    lineinfile:
      dest: "{{release_dir}}/lib/ansible/release.py"
      regexp: "^__version__ ="
      line: "__version__ = '{{ansible_release_version}}'"

  - block:
    - name: Update the spec file release list
      lineinfile:
        dest: "{{release_dir}}/packaging/rpm/ansible.spec"
        regexp: "^- Release {{ansible_release_version}}-{{ansible_release_string}}"
        line: "{{rpm_spec_line.rstrip()}}"
        insertafter:  "^%changelog"
    - name: Update the deb changelog file
      lineinfile:
        dest: "{{release_dir}}/packaging/debian/changelog"
        regexp: "^ansible ({{ansible_release_version}})"
        line: "{{deb_changelog_line}}"
        insertafter: "-- Ansible, Inc. <support@ansible.com>  %DATE%"
    #- name: Update RELEASES.txt
    #  lineinfile:
    #    dest: "{{release_dir}}/RELEASES.txt"
    #    regexp: "^{{ansible_release_version}}"
    #    line: '{{ansible_release_version}} "{{ansible_release_codename}}" {{release_date}}'
    #    insertafter: ""
    when: is_final|bool

  - name: "Make sure modules are checked out to {{ansible_release_branch}}"
    shell:
      _raw_params: "git checkout {{ansible_release_branch}}"
      chdir: "{{release_dir}}/lib/ansible/modules/{{item}}/"
    with_items:
    - core
    - extras

  - name: Update the VERSION file for the modules
    copy:
      dest: "{{release_dir}}/lib/ansible/modules/{{item}}/VERSION"
      content: "{{ansible_release_version}} {{ansible_release_string}}\n"
    with_items:
    - core
    - extras

  - name: Add and commit the updated files for the core modules
    shell:
      _raw_params: "git add ./ && git commit -m 'New release {{new_version}}'"
      chdir: "{{release_dir}}/lib/ansible/modules/{{item}}/"
    with_items:
    - core
    - extras

  - name: Add and commit the updated files for the main repo
    shell:
      _raw_params: "git add ./ && git commit -m 'New release {{new_version}}'"
      chdir: "{{release_dir}}/"

  - name: Tag the release
    shell:
      _raw_params: "git tag -fa {{new_version}} -m 'New release {{new_version}}'"
      chdir: "{{release_dir}}/"

  - name: update git config for submodules
    lineinfile:
      dest: "{{release_dir}}/.git/modules/lib/ansible/modules/{{item}}/config"
      regexp: "submodule_upstream"
      line: |
        [remote "submodule_upstream"]
                url = git@github.com:ansible/ansible-modules-{{item}}.git
                fetch = +refs/heads/*:refs/remotes/origin/*
    with_items:
    - core
    - extras

  - block:
    - pause:
        prompt: "Ready to push, this is the last chance to abort..."

    - name: Push the submodule repos
      shell:
        _raw_params: "git push submodule_upstream {{ansible_release_branch}}"
        chdir: "{{release_dir}}/lib/ansible/modules/{{item}}/"
      with_items:
      - core
      - extras

    - name: Push the updates and/or tag
      shell:
        _raw_params: "git push --tags origin {{ansible_release_branch}}"
        chdir: "{{release_dir}}/lib/ansible/modules/{{item}}/"
    when: do_push|bool