---
# Derived from the IMS_Install.sh bash script
# Things worth doing:
#   - add tags to avoid building SER and FHoSS every time
#   - pull out chunks of this that would be useful for other things (i.e. java install and setup)
- hosts: local
  vars: 
   # Oracle Java variables
   jdk_url: "http://download.oracle.com/otn-pub/java/jdk/7u40-b43/jdk-7u40-linux-x64.tar.gz"
   jdk_file: "jdk-7u40-linux-x64.tar.gz"
   jdk_extract_dir: "jdk1.7.0_40"
   jdk_header: "Cookie:gpw_e24=http%3A%2F%2Fwww.oracle.com"
   jdk_install_dir: "/usr/lib/jvm"
   jdk_priority: 9001 # over 9000 :P

   # sip express router and FHoSS variables
   oims_dir: "/opt/OpenIMSCore"
   ser_dir: "/opt/OpenIMSCore/ser_ims"
   ser_repo: "http://svn.berlios.de/svnroot/repos/openimscore/ser_ims/trunk"
   fhoss_dir: "/opt/OpenIMSCore/FHoSS"
   fhoss_repo: "http://svn.berlios.de/svnroot/repos/openimscore/FHoSS/trunk"

   # mysql variables
   mysql_root_password: "arg"

  tasks:
  - name: ensure ims dependencies are installed
    apt: pkg={{ item }} state=latest update_cache=yes cache_valid_time=3600 
    sudo: yes
    with_items:
    - vim
    - bison
    - libcurl4-gnutls-dev
    - libxml2-dev 
    - byobu                # huh? 
    - debhelper
    - cdbs
    - lintian
    - build-essential
    - fakeroot
    - devscripts
    - ipsec-tools
    - pbuilder
    - dh-make
    - debootstrap
    - dpatch
    - flex
    - libmysqlclient-dev
    - ant
    - docbook-to-man
    - bind9
    - tmux
    - subversion
    - htop
    - python-mysqldb
    - mysql-server

  - name: get the oracle jdk from their lame server
    command: wget --quiet --no-cookies --header {{ jdk_header }} {{ jdk_url }} creates={{ jdk_file }}
  - name: extract jdk
    command: tar -zxf {{ jdk_file }} creates={{ jdk_extract_dir }}
  - name: install (move) jdk to jvm dir 
    command: mv {{ jdk_extract_dir }} {{ jdk_install_dir }} creates={{ jdk_install_dir }}/{{jdk_extract_dir}}
    sudo: yes
  - name: set default java 
    command: update-alternatives --install {{ item.java_bin }} {{ item.java_bin_name }} {{ jdk_install_dir }}/{{ jdk_extract_dir }}/bin/{{ item.java_bin_name }} {{ jdk_priority }}
    sudo: yes
    with_items:
    - { java_bin: "/usr/bin/java", java_bin_name: "java" }
    - { java_bin: "/usr/bin/javac", java_bin_name: "javac" }
    - { java_bin: "/usr/bin/javaws", java_bin_name: "javaws" }
  - name: link java for programs that arent smart enough to know where it is (FHoSS)
    file: dest=/bin/java src=/usr/bin/java state=link 
    sudo: yes
  
  - name: ensure IMS directories exist
    file: path={{ item }} state=directory
    sudo: yes
    with_items:
    - /opt
    - "{{ oims_dir }}"

  - name: checkout SER code 
    subversion: repo={{ ser_repo }} dest={{ ser_dir }}
    sudo: yes
  - name: checkout HSS mgmt code 
    subversion: repo={{ fhoss_repo }} dest={{ fhoss_dir }}
    sudo: yes

  - name: make Sip Express Router
    command: make install-libs all chdir={{ ser_dir }}
    sudo: yes

  - name: compile and build FHoSS (not idempodent)
    shell: ant {{ item }} -Dfile.encoding=UTF8 chdir={{ fhoss_dir }}
    sudo: yes
    with_items:
    - compile
    - deploy

  - name: fix FHoSS directory permissions
    file: path={{ fhoss_dir }}/deploy state=directory recurse=yes mode=0777 
    sudo: yes

  - name: start mysql service
    service: name=mysql state=started

  - name: ensure mysql .my.cnf file deployed (root password credentials)
    template: src=files/my.cnf dest=/root/.my.cnf owner=root mode=0600
    sudo: yes
    notify:
    - restart mysql

  - name: update mysql root password for all root accounts
    mysql_user: 
      host={{ item }}
      login_user=root
      login_password={{ mysql_root_password }} 
      name=student 
      password={{ mysql_root_password }} 
      state=present 
      priv=*.*:ALL,GRANT
    sudo: yes
    with_items:
    - "{{ ansible_hostname }}"
    - 127.0.0.1
    - ::1
    - localhost

  #- name: import sql databases
  #  mysql_db: 
  #    login_user=root
  #    login_password={{ mysql_root_password }}
  #    login_host=localhost
  #    state=import 
  #    name={{ item.db }} 
  #    target={{ item.sql }}
  #  with_items:
  #  - { db: icscf, sql: "{{ ser_dir }}/cfg/icscf.sql" }
  #  - { db: hss_db, sql: "{{ fhoss_dir }}/scripts/hss_db.sql" }
  #  - { db: hss_db, sql: "{{ fhoss_dir }}/scripts/userdata.sql" }

  - name: give up on the ansible module and just pipe sql into mysql
    shell: mysql -u student --password={{ mysql_root_password }} < {{ item }}
    with_items:
    - "{{ ser_dir }}/cfg/icscf.sql"
    - "{{ fhoss_dir }}/scripts/hss_db.sql"
    - "{{ fhoss_dir }}/scripts/userdata.sql"

  - name: grab all cfg xml and sh files into OpenIMSCore
    shell: cp {{ item }} . chdir={{ oims_dir }}
    sudo: yes
    with_items:
    - "{{ ser_dir }}/cfg/*.cfg"
    - "{{ ser_dir }}/cfg/*.xml"
    - "{{ ser_dir }}/cfg/*.sh"

  - name: add openims zone file to bind
    command: cp /opt/OpenIMSCore/ser_ims/cfg/open-ims.dnszone /etc/bind
    sudo: yes
    notify: 
    - restart bind

  - lineinfile:  
      dest=/etc/bind/named.conf.local 
      regexp="^zone "
      line='zone "open-ims.test"  { type master; file "/etc/bind/open-ims.dnszone"; };'
      state=present
    sudo: yes
    notify:
    - restart bind

  - name: make student conf directories
    file: path={{item}} state=directory
    sudo: yes
    with_items:
    - /etc/studentconf
    - /etc/studentconf/openimscore 

  - name: copy launchers and scripts into student folder
    copy: src={{ item.src}} dest={{ item.dest }} 
    sudo: yes
    with_items:
    - { src: files/imsCore.sh, dest: /etc/studentconf/openimscore }
    - { src: files/kill_tmux.desktop, dest: /usr/share/applications }
    - { src: files/StartIMScore.desktop, dest: /usr/share/applications }

  handlers:
  - name: restart mysql
    service: name=mysql state=restarted
    sudo: yes

  - name: restart bind
    service: name=bind9 state=restarted
    sudo: yes
