#!/usr/bin/env bash
#
# Copyright (c) 2007-2010 VMware, Inc.  All rights reserved.
#

# Manages the configuration and deconfiguration of vmxnet and hgfs.

# Must come before set -e.

set -e

markerBegin='# Beginning of the block added by the VMware OSP software'
markerEnd='# End of the block added by the VMware OSP software'

append_blocks()
{
   [ -d /usr/lib/vmware-tools/insert ] || return 0

   # Find files under /usr/lib/vmware-tools/insert, and append them to
   # the file under the same path starting at /.
   find /usr/lib/vmware-tools/insert/ -type f |
   while read -r filename; do
      real_filename=$(echo $filename | \
	              sed -e "s|/usr/lib/vmware-tools/insert||")
      {
         echo "$markerBegin"
         cat "$filename"
         echo "$markerEnd"
      } >> $real_filename
   done
}

remove_blocks()
{
   [ -d /usr/lib/vmware-tools/insert ] || return 0

   # Find files under /usr/lib/vmware-tools/insert, and remove
   # any blocks marked as ours.
   find /usr/lib/vmware-tools/insert/ -type f |
   while read -r filename; do
      real_filename=$(echo $filename | \
	              sed -e "s|/usr/lib/vmware-tools/insert||")
      sed -i -e "/^${markerBegin}\$/, /^${markerEnd}\$/d" $real_filename
   done
}

# See how we were called.
case "$1" in
   install)
      append_blocks
      ;;
   uninstall)
      remove_blocks
      ;;
   *)
      error "Usage: `basename "$0"` {install|uninstall}"
      ;;
esac
