#!/bin/bash
#
# Copyright 2017 Red Hat, Inc.
#
# NAME
#     rht-vmicons - create new "view desktop" icons
#
# SYNOPSIS
#     rht-vmicons
#
# DESCRIPTION
#     This tool removes existing RHT desktop icons and creates new
#     "view desktop" icons.
#
# CHANGELOG
#   * Fri Mar 24 2017 Robert Locke <rlocke@redhat.com>
#   - adjust error message when RHT_VMS is blank
#   * Wed Mar  1 2017 Robert Locke <rlocke@redhat.com>
#   - create the rht-vmctl.desktop too
#   * Wed May 11 2016 Robert Locke <rlocke@redhat.com>
#   - add support for RHT_ENROLLMENT -ge 512
#   * Fri Sep 25 2015 Robert Locke <rlocke@redhat.com>
#   - add rht-gethosts
#   * Fri Nov 14 2014 Robert Locke <rlocke@redhat.com>
#   - make .desktop filenames more specific for easier cleanup
#   * Tue Aug 19 2014 Robert Locke <rlocke@redhat.com>
#   - add --gui to rht-vmctl for later visualization update
#   * Thu Jul 10 2014 Robert Locke <rlocke@redhat.com>
#   - shift f0 infrastructure VMs from RHT_VMS to RHT_VM0
#   * Wed Jun 11 2014 Robert Locke <rlocke@redhat.com>
#   - shift desktop icons to use hidden "viewstart" option in rht-vmctl
#   * Wed May  7 2014 Robert Locke <rlocke@redhat.com>
#   - original code

PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin

# Source some variables that we need - should provide:
# RHT_VMS="desktop server"
source /etc/rht
# Validate
if [[ -z "${RHT_VMS}" ]]; then
  echo "There are no VMs listed to create icons from."
  exit 1
fi

# Source library of functions
source /usr/local/lib/rhttool.shlib
# trap on_exit EXIT

desktop_dir=/home/kiosk/Desktop

# Remove existing desktop icons
rm -f ${desktop_dir}/rht-view-*.desktop
rm -f ${desktop_dir}/rht-vmctl.desktop

[[ ! -d ${desktop_dir} ]] && mkdir -p ${desktop_dir}

# If does not exist, create rht-vmctl.desktop
filename=${desktop_dir}/rht-vmctl.desktop
cat > ${filename} <<-EOF
#!/usr/bin/env xdg-open
[Desktop Entry]
Name=Manage VMs
GenericName=Manage VMs
X-GNOME-FullName=Manage VMs
Exec=/usr/local/lib/rht-vmctl.zenity
Terminal=false
Icon=virt-manager
Type=Application
StartupNotify=true
Categories=GTK;GNOME;Utility;Accessibility;
Name[en_US]=Manage VMs
EOF
chmod +x ${filename}

# If on foundation0 then create composite list of VMS
[[ -n "${RHT_VM0}" ]] && my_vms="${RHT_VM0} ${RHT_VMS}" || my_vms=${RHT_VMS}

# For each my_vms, create a desktop icon.
for vm_name in ${my_vms} ; do
  cat > ${desktop_dir}/rht-view-${vm_name}.desktop <<-EOF
#!/usr/bin/env xdg-open
[Desktop Entry]
Name=View ${vm_name,,}
GenericName=View ${vm_name,,}
X-GNOME-FullName=${vm_name,,} VM
Exec=rht-vmctl --gui viewstart ${vm_name,,}
Terminal=false
Icon=virt-viewer
Type=Application
StartupNotify=true
Categories=GTK;GNOME;Utility;Accessibility;
Name[en_US]=View ${vm_name,,}
EOF
  chmod +x ${desktop_dir}/rht-view-${vm_name}.desktop
done

# Retrieve the private hosts in to /etc/hosts
rht-gethosts

exit 0
