#/usr/bin/perl
#
#       @(#)closedoc     Version:6.2.8           30-10-1997
#
#       Gert Großmann, SAP AG
#
# usage: closedoc [-e editorname] file [ file ... ]
#	-e editorname	: name of the used editor
#
#
#    ========== licence begin  GPL
#    Copyright (C) 2001 SAP AG
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2
#    of the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#    ========== licence end
#

package CloseDoc;
use Env;
use Getopt::Std;
use Win32;
use OLE;

if ( getopts('e:') && @ARGV == 0 ) { die "usage: closedoc -e editorname file [ file ... ]\n" }

if ( !defined($opt_e) ) {
	die "environment %EDITOR% not used!\n" unless defined($EDITOR);
	$opt_e = $EDITOR;
}

$returncode = 0;
if ( $opt_e =~ /msdev|devenv/i ) {

	# Developer Studio OLE
	local $editor;
    if ($opt_e =~ /msdev/i ) {
	    $editor = GetObject OLE 'MSDEV.APPLICATION';
    } else {
    	$editor = GetObject OLE 'VisualStudio.DTE';
    }
	if ( defined($editor)) {
		foreach $file (@ARGV) {
			$file =~ tr[/][\\]; $file = lc($file);
			local $Documents = $editor->{Documents};
			for (local $i=1; $i <= $Documents->{Count}; $i++) {
				local $openfile = lc($Documents->Item($i)->{Fullname});
				if ( $openfile eq $file ) {
					if ( ! $Documents->Item($i)->{Saved} ) {
						$msg = "Save the Document ".$file."!";
						Win32::MsgBox($msg, , MB_ICONSTOP);
						$returncode++;
					}
					$Documents->Item($i)->Close() if !$returncode
				}
			}
		}
	}
}
caller() ? $returncode : exit $returncode;

