Group packages to install/remove when possible for better performance (#62)

* Allow to install package without specifying any version

* Group packages to install/remove when possible for better performance
diff --git a/linux/system/package.sls b/linux/system/package.sls
index 9bda3a2..d37c3c5 100644
--- a/linux/system/package.sls
+++ b/linux/system/package.sls
@@ -5,28 +5,57 @@
   pkg.installed:
   - pkgs: {{ system.pkgs }}
 
+{%- set pkgs_groups = {
+  'latest': [],
+  'purged': [],
+  'removed': [],
+  'installed': [],
+  } %}
 {%- for name, package in system.package.iteritems() %}
 
+  {%- if package.repo is defined or package.hold is defined or package.verify is defined %}
 linux_extra_package_{{ name }}:
-  {%- if package.version == 'latest' %}
+    {%- if package.version is defined %}
+      {%- if package.version == 'latest' %}
   pkg.latest:
-  {%- elif package.version == 'purged' %}
+      {%- elif package.version == 'purged' %}
   pkg.purged:
-  {%- elif package.version == 'removed' %}
+      {%- elif package.version == 'removed' %}
   pkg.removed:
-  {%- else %}
+      {%- else %}
   pkg.installed:
   - version: {{ package.version }}
-  {%- endif %}
+      {%- endif %}
+    {%- else %}
+  pkg.installed:
+    {%- endif %}
   - name: {{ name }}
-  {%- if package.repo is defined %}
+    {%- if package.repo is defined %}
   - fromrepo: {{ package.repo }}
-  {%- endif %}
-  {%- if package.hold is defined %}
+    {%- endif %}
+    {%- if package.hold is defined %}
   - hold: {{ package.hold }}
-  {%- endif %}
-  {%- if package.verify is defined %}
+    {%- endif %}
+    {%- if package.verify is defined %}
   - skip_verify: {{ true if package.verify else false }}
+    {%- endif %}
+  {%- else %}
+    {%- if package.version is not defined %}
+      {%- do pkgs_groups['installed'].append(name) %}
+    {%- elif package.version in ('latest', 'purged', 'removed') %}
+      {%- do pkgs_groups[package.version].append(name) %}
+    {%- else %}
+      {%- do pkgs_groups['installed'].append({name: package.version}) %}
+    {%- endif %}
+  {%- endif %}
+
+{%- endfor %}
+
+{%- for pkgs_group, pkgs in pkgs_groups.iteritems() %}
+  {%- if pkgs %}
+linux_extra_packages_{{ pkgs_group }}:
+  pkg.{{ pkgs_group }}:
+    - pkgs: {{ pkgs }}
   {%- endif %}
 {%- endfor %}