$umask=0600;
while (@ARGV) {
        $filename = shift @ARGV;
        ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
         $atime,$mtime,$ctime,$blksize,$blocks)
                = stat($filename);
        $bakfile = $filename . ".bak";
        unless (rename $filename, $bakfile) {
                warn "$0: cannot rename $filename to $bakfile, $!\n";
                next;
        }
        unless (open(IN,$bakfile)) {
                warn "$0: cannot open $bakfile for reading, $!\n";
                next;
        }
        unless (open(OUT,">$filename")) {
                warn "$0: cannot open $filename for writing, $!\n";
                next;
        }

        while (<IN>) {

# JClass obsolete classes
	        &commentobsclass(ButtonCellEditor);
	        &commentobsclass(ButtonCellRenderer);
	        &commentobsclass(JCFile);
	        &commentobsclass(JCTitleLayout);

# JClass convertible obsolete classes
                &renameid('jclass.contrib.ContribApplet', 'javax.swing.JApplet');
                &renameid('jclass.contrib.ContribFrame', 'com.klg.jclass.util.swing.JCExitFrame');
                &renameid('jclass.contrib.JCDialog', 'javax.swing.JDialog');
		&renameid('jclass.cell.InitialEvent', 'java.awt.AWTEvent');
                &renameid('jclass.util.JCString', 'java.lang.String');
		&renameid('jclass.util.JCVector', 'java.util.Vector');
		&renameid('jclass.util.JCImageCreator', 'com.klg.jclass.util.swing.JCIconCreator');
	        &renameobs(ContribApplet, JApplet);
		&renameobs(ContribFrame, JCExitFrame);
                &renameobs(JCDialog, JDialog);
        	&renameobs(InitialEvent, AWTEvent);
	        &renameobs(JCString, String);
	        &renameobs(JCVector, Vector);
	        &renameobs(JCImageCreator, JCIconCreator);

# JClass obsolete methods
		&commentobs(setFrameLayout, setLayout);

# JClass classes moved to another packages
                &renameid('jclass.cell.editors.FeatherweightCellEditor', 'com.klg.jclass.cell.JCCellEditor');
		&renameid(FeatherweightCellEditor, JCCellEditor);

# JClass Cell classes
		&renameid(CellEditor, JCCellEditor);
		&renameid(CellEditorListener, JCCellEditorListener);
		&renameid(CellEditorSupport, JCCellEditorSupport);
		&renameid(CellInfo, JCCellInfo);
		&renameid(CellRenderer, JCLightCellRenderer);
	        &renameid(CheckboxCellEditor, JCCheckBoxCellEditor);
	        &renameid(CheckboxCellRenderer, JCCheckBoxCellRenderer);
	        &renameid(DateCellEditor, JCDateCellEditor);
	        &renameid(DoubleCellEditor, JCDoubleCellEditor);
	        &renameid(StringCellRenderer, JCStringCellRenderer);
	        &renameid(CheckboxCellRenderer, JCCheckBoxCellRenderer);
	        &renameid(CheckboxCellEditor, JCCheckBoxCellEditor);
# not all editors, renderers included!
		&renameid(KeyModifier, JCKeyModifier);

# JClass classes
		&renameid(EditableTableData, EditableTableDataModel);
		&renameid(TableDataListener, JCTableDataListener);
		&renameid(TableDataEvent, JCTableDataEvent);
		&renameid(TableCellInfo, TableCellInfoModel);
		&renameid(CellEditorEvent, JCCellEditorEvent);

# LiveTable obsolete classes
	        &commentobsclass(LabelTrigger);
	        &commentobsclass(JCAdjustable);
	        &commentobsclass(TableScrollbar);

# LiveTable convertible obsolete classes
                &renameid('jclass.table3.JCTable', 'com.klg.jclass.table.Table');
                &renameid('jclass.table3.JCValueEvent', 'com.klg.jclass.table.JCCellDisplayEvent');
                &renameid('jclass.table3.JCCellValueListener', 'com.klg.jclass.table.JCCellDisplayListener');
                &renameid('jclass.table3.JCLabelValueListener', 'com.klg.jclass.table.JCCellDisplayListener');
                &renameobs(JCTable, Table);
                &renameobs(JCValueEvent, JCCellDisplayEvent);
                &renameobs(JCCellValueListener, JCCellDisplayListener);
                &renameobs(JCLabelValueListener, JCCellDisplayListener);

# LiveTable obsolete methods
		&commentobs(setStyled);
		&commentobs(setSelectionMode);
		&commentobs(setCells);
		&commentobs(setColumnLabels);
		&commentobs(setRowLabels);
		&commentobs(setColumnTrigger);
		&commentobs(setRowTrigger);

# LiveTable classes moved to another packages
                &renameid('jclass.table3.TableDataSupport', 'com.klg.jclass.table.data.AbstractDataSource');
                &renameid('jclass.table3.AppletDataSource', 'com.klg.jclass.table.data.JCAppletDataSource');
                &renameid('jclass.table3.BeanFileDataSource', 'com.klg.jclass.table.data.JCBeanFileDataSource');
                &renameid('jclass.table3.CachedDataSource', 'com.klg.jclass.table.data.JCCachedDataSource');
                &renameid('jclass.table3.EditableCachedDataSource', 'com.klg.jclass.table.data.JCEditableCachedDataSource');
                &renameid('jclass.table3.EditableFileDataSource', 'com.klg.jclass.table.data.JCEditableFileDataSource');
                &renameid('jclass.table3.EditableVectorDataSource', 'com.klg.jclass.table.data.JCEditableVectorDataSource');
                &renameid('jclass.table3.FileDataSource', 'com.klg.jclass.table.data.JCFileDataSource');
                &renameid('jclass.table3.InputStreamDataSource', 'com.klg.jclass.table.data.JCInputStreamDataSource');
                &renameid('jclass.table3.ResultSetDataSource', 'com.klg.jclass.table.data.JCResultSetDataSource');
                &renameid('jclass.table3.URLDataSource', 'com.klg.jclass.table.data.JCURLDataSource');
                &renameid('jclass.table3.VectorDataSource', 'com.klg.jclass.table.data.JCVectorDataSource');
                &renameid('jclass.table3.LiveTable', 'com.klg.jclass.table.bean.LiveTable');
                &renamemoved(TableDataSupport, AbstractDataSource, 'com.klg.jclass.table.data');
                &renamemoved(AppletDataSource, JCAppletDataSource, 'com.klg.jclass.table.data');
                &renamemoved(BeanFileDataSource, JCBeanFileDataSource, 'com.klg.jclass.table.data');
                &renamemoved(CachedDataSource, JCCachedDataSource, 'com.klg.jclass.table.data');
                &renamemoved(EditableCachedDataSource, JCEditableCachedDataSource, 'com.klg.jclass.table.data');
                &renamemoved(EditableFileDataSource, JCEditableFileDataSource, 'com.klg.jclass.table.data');
                &renamemoved(EditableVectorDataSource, JCEditableVectorDataSource, 'com.klg.jclass.table.data');
                &renamemoved(FileDataSource, JCFileDataSource, 'com.klg.jclass.table.data');
                &renamemoved(InputStreamDataSource, JCInputStreamDataSource, 'com.klg.jclass.table.data');
                &renamemoved(ResultSetDataSource, JCResultSetDataSource, 'com.klg.jclass.table.data');
                &renamemoved(URLDataSource, JCURLDataSource, 'com.klg.jclass.table.data');
		&renamemoved(VectorDataSource, JCVectorDataSource, 'com.klg.jclass.table.data');
		&renamemoved(LiveTable, LiveTable, 'com.klg.jclass.table.bean');

# LiveTable classes and interfaces
		&renameid(JCTblEnum, JCTableEnum);
		&renameid(StandardCellBorder, JCCellBorder);
		&renameid(TableData, TableDataModel);

# LiveTable methods
		&renameid(addToApplet, add);
		&renameid(setSpans, setSpannedRanges);
		&renameid(setAppletLayout, setLayout);
		&renameid(setSpans, setSpannedRanges);
#		&renameid(setAllowSelection, setCancelled);	too tricky

# JClass packages
		&renamepack('jclass.util', 'com.klg.jclass.util');
		&renamepack('jclass.beans', 'com.klg.jclass.beans');
		&renamepack('jclass.cell', 'com.klg.jclass.cell');
		&renamepack('jclass.chart', 'com.klg.jclass.chart');
		&renamepack('jclass.contrib', 'com.klg.jclass.util.swing');
		&renamepack('jclass.datasource', 'com.klg.jclass.datasource');
		&renamepack('jclass.field', 'com.klg.jclass.field');
		&renamepack('jclass.higrid', 'com.klg.jclass.higrid');
		&renamepack('jclass.table3', 'com.klg.jclass.table');
		
                print OUT;
        }
        close IN;
        close OUT;
	chmod 0644,$bakfile;
	system("del $bakfile");
        chmod $mode,$filename;
}

sub renameid
{
	s/(^\s*|\s+|[!=;,:<>\|\[\]\(\)\*\+\.\^\/\\]+)($_[0])(\s+|[=;,:<>\|\[\]\(\)\*\+\.\^\/\\]+)/$1$_[1]$3/g;
}

sub renamecomm
{
	if (s/(^\s*|\s+|[!=;,:<>\|\[\]\(\)\*\+\.\^\/\\]+)($_[0])(\s+|[=;,:<>\|\[\]\(\)\*\+\.\^\/\\]+)/$1$_[1]$3/g) {
		s/\n/$_[2]\n/;
	}
}

sub renamemoved
{
	if (s/(^\s*|\s+|[!=;,:<>\|\[\]\(\)\*\+\.\^\/\\]+)($_[0])(\s+|[=;,:<>\|\[\]\(\)\*\+\.\^\/\\]+)/$1$_[1]$3/g) {
		s/\n/ \/\/ $_[0] is converted into $_[2]\.$_[1]. See documentation for details.\n/;
	}
}

sub renameobs
{
	if (s/(^\s*|\s+|[!=;,:<>\|\[\]\(\)\*\+\.\^\/\\]+)($_[0])(\s+|[=;,:<>\|\[\]\(\)\*\+\.\^\/\\]+)/$1$_[1]$3/g) {
		s/\n/ \/\/ $_[0] is an obsolete class. Converted tentatively into $_[1]. See documentation for details.\n/;
	}
}

sub commentobs
{
	if (!(/^\/\//) & /(^\s*|\s+|[!=;,:<>\|\[\]\(\)\*\+\.\^\/\\]+)($_[0])(\s+|[=;,:<>\|\[\]\(\)\*\+\.\^\/\\]+)/) {
		s/(^.*)(\n)/\/\/ $1 This is an obsolete method. Cannot be converted. See documentation for details.\n/;
	}
}

sub commentobsclass
{
	if (/(^\s*|\s+|[!=;,:<>\|\[\]\(\)\*\+\.\^\/\\]+)($_[0])(\s+|[=;,:<>\|\[\]\(\)\*\+\.\^\/\\]+)/) {
		s/(^.*)(\n)/\/\/ $1 $_[0] is an obsolete class. Cannot be converted. See documentation for details.\n/;
	}
}

sub renamepack
{
	s/(^.*[^\.])($_[0])(\s+|[=;,:<>\|\[\]\(\)\*\+\.\^\/\\]+)/$1$_[1]$3/g;
}


# JCLightCellRenderer
#   old: public Dimension getPreferredSize(JCCellInfo info, Object o)
#   new: public Dimension getPreferredSize(Graphics gc, JCCellInfo info, Object o)
#
# JCCellInfo
#   old: public FontMetrics getFontMetrics()
#   new: getFontMetrics() from Graphics object must be used instead
#
#