#!/usr/bin/env bash
# Copyright 2008-2011 VMware, Inc.  All rights reserved.
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# 


export TEXTDOMAINDIR=/opt/vmware/lib/locale
export TEXTDOMAIN=install-appliance-manifest

thisRandom="$RANDOM"

SOURCEFILE="/opt/vmware/etc/appliance-manifest.xml"
DESTFILE="/opt/vmware/var/lib/vami/update/data/info/manifest-installed.xml"

if [ ! -f "$SOURCEFILE" ]; then
  echo `gettext "VM manifest file missing"`
  exit 1
fi

if [ -f "$DESTFILE" ]; then
  echo `gettext "WARNING: Overwriting exiting VM manifest"`
fi

#get a list of current packages; encode xml reserved charcters before writing into file
if which rpm > /dev/null 2>&1; then
  rpm -qa --queryformat='%{NAME} %{EPOCH}:%{VERSION}-%{RELEASE} %{ARCH}\n' | grep -v 'gpg-pubkey' | grep -v 'vmware-studio-provagent' | awk '{ gsub(/&/, "\\&amp;"); gsub(/</, "\\&lt;"); gsub(/>/, "\\&gt;");  gsub(/"/, "\\&quot;"); printf("<package name=\"%s\" version=\"%s\" arch=\"%s\" size=\"0\" type=\"rpm\"><checksum type=\"sha1\">0</checksum><location>none</location></package>\n", $1, $2, $3) }' > "/tmp/install_manifest_packages.$thisRandom"
fi
if which dpkg-query > /dev/null 2>&1; then
   dpkg-query -W -f='${Package} ${Version} ${Architecture}\n' | grep -v 'vmware-studio-provagent' | awk '{ gsub(/&/, "\\&amp;"); gsub(/</, "\\&lt;"); gsub(/>/, "\\&gt;");  gsub(/"/, "\\&quot;"); printf("<package name=\"%s\" version=\"%s\" arch=\"%s\" size=\"0\" type=\"deb\">\n  <checksum type=\"sha1\">0</checksum>\n  <location>none</location>\n</package>\n", $1, $2, $3) }' >> "/tmp/install_manifest_packages.$thisRandom"
fi

cat "$SOURCEFILE" | sed 's=</update>=='  >  "/tmp/install_manifest_work.$thisRandom"


echo '<packageGroups>'                            >> "/tmp/install_manifest_work.$thisRandom"
echo '<group>'                                    >> "/tmp/install_manifest_work.$thisRandom"
cat "/tmp/install_manifest_packages.$thisRandom"  >> "/tmp/install_manifest_work.$thisRandom"
echo '</group>'                                   >> "/tmp/install_manifest_work.$thisRandom"
echo '</packageGroups>'                           >> "/tmp/install_manifest_work.$thisRandom"
echo '</update>'                                  >> "/tmp/install_manifest_work.$thisRandom"

mkdir -p /opt/vmware/var/lib/vami/update/data/info/

mv "/tmp/install_manifest_work.$thisRandom" "$DESTFILE"

rm "/tmp/install_manifest_packages.$thisRandom"


