Index: mapfile.c
===================================================================
--- mapfile.c	(revision 6142)
+++ mapfile.c	(working copy)
@@ -1713,7 +1713,9 @@
 int initStyle(styleObj *style) {
   int i;
 
+  #ifdef USE_MAPSCRIPT
   MS_REFCNT_INIT(style);
+  #endif
   MS_INIT_COLOR(style->color, -1,-1,-1); /* must explictly set colors */
   MS_INIT_COLOR(style->backgroundcolor, -1,-1,-1);
   MS_INIT_COLOR(style->outlinecolor, -1,-1,-1);
@@ -1865,8 +1867,9 @@
 
 int freeStyle(styleObj *style) {
   int i;
-
+  #ifdef USE_MAPSCRIPT
   if( MS_REFCNT_IS_NOT_ZERO(style) ) { return MS_FAILURE; }
+  #endif
 
   msFree(style->symbolname);
   msFree(style->rangeitem);
@@ -1921,7 +1924,9 @@
 
   class->status = MS_ON;
   class->debug = MS_OFF;
+  #ifdef USE_MAPSCRIPT
   MS_REFCNT_INIT(class);
+  #endif
 
   initExpression(&(class->expression));
   class->name = NULL;
@@ -1959,8 +1964,10 @@
 {
   int i;
 
+  #ifdef USE_MAPSCRIPT
   if( MS_REFCNT_IS_NOT_ZERO(class) ) { return MS_FAILURE; }
   /* printf("Freeing class at %p (%s)\n", class, class->name); */
+  #endif
 
   freeLabel(&(class->label));
   freeExpression(&(class->expression));
@@ -2365,7 +2372,9 @@
     return(-1);
   }
   layer->debug = MS_OFF;
+  #ifdef USE_MAPSCRIPT
   MS_REFCNT_INIT(layer);
+  #endif
 
   layer->numclasses = 0;
   if((layer->class = (classObj **)malloc(sizeof(classObj*)*MS_MAXCLASSES)) == NULL) {
@@ -2481,7 +2490,9 @@
 int freeLayer(layerObj *layer) {
   int i;
   if (!layer) return MS_FAILURE;
+  #ifdef USE_MAPSCRIPT
   if( MS_REFCNT_IS_NOT_ZERO(layer) ) { return MS_FAILURE; }
+  #endif
   if (layer->debug)
      msDebug("freeLayer(): freeing layer at %p.\n",layer);
 
@@ -4258,7 +4269,9 @@
 int initMap(mapObj *map)
 {
   int i=0;
+  #ifdef USE_MAPSCRIPT
   MS_REFCNT_INIT(map);
+  #endif
   map->numlayers = 0;
   if((map->layers = (layerObj **)malloc(sizeof(layerObj*)*MS_MAXLAYERS)) == NULL) {
     msSetError(MS_MEMERR, NULL, "initMap()");
Index: mapscript/csharp/Makefile.in
===================================================================
--- mapscript/csharp/Makefile.in	(revision 6142)
+++ mapscript/csharp/Makefile.in	(working copy)
@@ -154,6 +154,11 @@
 THREAD_LIB=@THREAD_LIB@
 
 #
+# Mapscript support (reference counting)
+#
+MAPSCRIPT=@MAPSCRIPT_ENABLED@
+
+#
 # libiconv - Enables Internationalization
 #
 ICONV=@ICONV_ENABLED@
@@ -175,7 +180,7 @@
 	$(GD_INC) $(PDF_INC) $(PROJ_INC) $(TIFF_INC) $(JPEG_INC) $(EGIS_INC) \
 	$(SDE_INC) $(GDAL_INC) $(POSTGIS_INC) $(MYGIS_INC) \
 	$(CURL_INC) $(MING_INC) $(ORACLESPATIAL_INC) $(GEOS_INC) $(ICONV_INC) \
-	$(FASTCGI_INC) $(ZLIB_INC) $(XML2_INC)
+	$(FASTCGI_INC) $(ZLIB_INC) $(XML2_INC) $(MAPSCRIPT)
 
 CCFLAGS   = @CFLAGS@ $(FLAGS)
 
Index: mapscript/java/Makefile.in
===================================================================
--- mapscript/java/Makefile.in	(revision 6142)
+++ mapscript/java/Makefile.in	(working copy)
@@ -146,6 +146,11 @@
 THREAD_LIB=@THREAD_LIB@
 
 #
+# Mapscript support (reference counting)
+#
+MAPSCRIPT=@MAPSCRIPT_ENABLED@
+
+#
 # libiconv - Enables Internationalization
 #
 ICONV=@ICONV_ENABLED@
@@ -173,8 +178,18 @@
 	$(POSTGIS) $(MYGIS) $(THREAD) $(GD_INC) $(PDF_INC) \
 	$(PROJ_INC) $(TIFF_INC) $(JPEG_INC) $(EGIS_INC) $(SDE_INC) \
 	$(REGEX_INC) $(GDAL_INC) $(POSTGIS_INC) $(MYGIS_INC) \
-	$(CURL_INC) $(MING_INC) $(ORACLESPATIAL_INC) $(GEOS_INC) $(ICONV_INC) $(XML2_INC)
+	$(CURL_INC) $(MING_INC) $(ORACLESPATIAL_INC) $(GEOS_INC) $(ICONV_INC) $(XML2_INC) $(MAPSCRIPT)
 
+SWIG_FLAGS = @DEBUG_FLAGS@ \
+        $(IGNORE_MISSING_DATA) $(USE_POINT_Z_M) $(STRINGS) $(EPPL) $(PROJ) \
+	$(OWS) $(MING) $(ORACLESPATIAL) $(TIFF) $(JPEG) $(GD) $(AGG) $(PDF) $(SDE) \
+	$(SDERASTER) $(OGR) $(GDAL) $(GEOS) $(ICONV) $(FASTCGI) $(PHP_REGEX) \
+	$(REGEX_INC) $(POSTGIS) $(MYGIS) $(THREAD) $(ZLIB) \
+	$(GD_INC) $(AGG_INC) $(PDF_INC) $(PROJ_INC) $(TIFF_INC) $(JPEG_INC) $(EGIS_INC) \
+	$(SDE_INC) $(GDAL_INC) $(POSTGIS_INC) $(MYGIS_INC) \
+	$(CURL_INC) $(MING_INC) $(ORACLESPATIAL_INC) $(GEOS_INC) $(ICONV_INC) \
+	$(FASTCGI_INC) $(ZLIB_INC) $(XML2_INC) $(MAPSCRIPT)
+
 # Link flags and shared libs only
 SUP_LIBS =  $(GD_LIB) $(PDF_LIB) $(TIFF_LIB) $(PROJ_LIBS) \
           $(JPEG_LIB) $(SDE_LIB) $(GDAL_LIB) $(MING_LIB) $(POSTGIS_LIB) \
@@ -221,7 +236,7 @@
 # This target will be used in the release script
 mapscript_wrap.c: ../mapscript.i
 	mkdir -p edu/umn/gis/mapscript 
-	$(SWIG) -java -package edu.umn.gis.mapscript -outdir edu/umn/gis/mapscript -o mapscript_wrap.c ../mapscript.i
+	$(SWIG) $(SWIG_FLAGS) -java -package edu.umn.gis.mapscript -outdir edu/umn/gis/mapscript -o mapscript_wrap.c ../mapscript.i
 
 mapscript_so: mapscript_wrap.c
 	$(CC) -fpic -c $(CCFLAGS) $(JAVA_INCLUDE) -fno-strict-aliasing mapscript_wrap.c
Index: mapscript/python/setup.py
===================================================================
--- mapscript/python/setup.py	(revision 6142)
+++ mapscript/python/setup.py	(working copy)
@@ -108,6 +108,10 @@
 #extras.append("-static")
 #extras.append("-lgd")
 
+if not os.path.exists('mapscript_wrap.c') :
+	print('swig -python -shadow -modern %s -o mapscript_wrap.c ../mapscript.i' % " ".join(ms_macros))
+	os.system('swig -python -shadow -modern %s -o mapscript_wrap.c ../mapscript.i' % " ".join(ms_macros))
+
 setup(name = "mapscript",
       version = ms_version,
       description = "Python interface to MapServer",
Index: mapscript/ruby/extconf.rb
===================================================================
--- mapscript/ruby/extconf.rb	(revision 6142)
+++ mapscript/ruby/extconf.rb	(working copy)
@@ -25,6 +25,6 @@
 create_makefile("mapscript")
 
 make_file = File.open("Makefile", "a")
-make_file << "\nmapscript_wrap.c: ../mapscript.i\n\tswig -ruby -o mapscript_wrap.c ../mapscript.i"
+make_file << "\nmapscript_wrap.c: ../mapscript.i\n\tswig -ruby " + make_define + " -o mapscript_wrap.c ../mapscript.i"
 make_file.close
 
Index: mapscript/perl/Makefile.PL
===================================================================
--- mapscript/perl/Makefile.PL	(revision 6142)
+++ mapscript/perl/Makefile.PL	(working copy)
@@ -35,6 +35,38 @@
 print $static_libs."\n";
 print $ms_version."\n";
 
+my $swigInterfaceFile = "../mapscript.i";
+my $swigWrapperFile   = "mapscript_wrap.c";
+
+########################################################################
+
+sub runSwigAutogeneration{
+  print "Autogenerating wrappers from SWIG interface " . $swigInterfaceFile . "\n";
+  my $command = "swig -perl5 -shadow -outdir . $define -o $swigWrapperFile $swigInterfaceFile";
+  system($command);
+  if ($?){
+      print "An error occurred when generating the wrappers from the SWIG interface :\n$!\n";
+      print "The command that was run was:\n\n$command\n\n";
+      print "Perl mascript cannot be built.\n";
+      exit;
+  }
+}
+
+########################################################################
+
+sub existsAndUpToDate{
+  my ($sourceFile, $destFile) = @_;
+  if (!(-e $destFile)) { return 0; }
+  my $sourceMtime = (stat $sourceFile)[9];
+  my $destMtime = (stat $destFile)[9];
+  return $destMtime >= $sourceMtime;
+}
+
+########################################################################
+
+runSwigAutogeneration() unless
+  existsAndUpToDate($swigInterfaceFile, $swigWrapperFile);
+
 WriteMakefile(
 	      'NAME' 		=> 'mapscript',
 	      'DEFINE' 		=> $define,
Index: mapscript/mapscript.i
===================================================================
--- mapscript/mapscript.i	(revision 6142)
+++ mapscript/mapscript.i	(working copy)
@@ -30,6 +30,10 @@
 ============================================================================
 */
 
+#ifndef USE_MAPSCRIPT
+%warn "999:\n*****************************************************************\n  !!!! Attempting to build mapscript without first \n  enabling support for it in mapserver\n  to enable it re-run configure with --with-mapscript=yes\n  MAPSCRIPT WILL NOT WORK WITHOUT with-mapscript=yes !!!!\n*****************************************************************"
+#endif
+
 %module mapscript
 
 %{
Index: configure.in
===================================================================
--- configure.in	(revision 6142)
+++ configure.in	(working copy)
@@ -2202,6 +2202,31 @@
 AC_SUBST(OS_INCLUDE_DIR, $OS_INCLUDE_DIR)
 
 dnl ---------------------------------------------------------------------
+dnl Check if mapscript support was requested (RFC-24)
+dnl ---------------------------------------------------------------------
+
+MAPSCRIPT_ENABLED=""
+AC_MSG_CHECKING([if mapscript requested])
+AC_ARG_WITH(with-mapscript,
+[  --with-mapscript
+                          Since mapserver 5 it is required to specify this
+			  configure option to successfully build the following
+			  mapscripts: java],,)
+
+if test "${with_mapscript}" = "yes" ; then
+   MAPSCRIPT_ENABLED="-DUSE_MAPSCRIPT"
+   AC_MSG_RESULT([yes])
+elif test -n "$with_mapscript" -a "$with_mapscript" != "no" ; then
+   MAPSCRIPT_ENABLED="-DUSE_MAPSCRIPT"
+   AC_MSG_RESULT([yes])
+else
+   AC_MSG_RESULT([no])
+fi
+
+AC_SUBST(MAPSCRIPT_ENABLED, $MAPSCRIPT_ENABLED)
+ALL_ENABLED="$MAPSCRIPT_ENABLED $ALL_ENABLED"
+
+dnl ---------------------------------------------------------------------
 dnl Generate output files
 dnl ---------------------------------------------------------------------
 
Index: mapparser.h
===================================================================
--- mapparser.h	(revision 6142)
+++ mapparser.h	(working copy)
@@ -1,38 +1,107 @@
-#ifndef BISON_MAPPARSER_H
-# define BISON_MAPPARSER_H
+/* A Bison parser, made by GNU Bison 2.3.  */
 
-#ifndef YYSTYPE
-typedef union {
+/* Skeleton interface for Bison's Yacc-like parsers in C
+
+   Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
+   Free Software Foundation, Inc.
+
+   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, 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., 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301, USA.  */
+
+/* As a special exception, you may create a larger work that contains
+   part or all of the Bison parser skeleton and distribute that work
+   under terms of your choice, so long as that work isn't itself a
+   parser generator using the skeleton or a modified version thereof
+   as a parser skeleton.  Alternatively, if you modify or redistribute
+   the parser skeleton itself, you may (at your option) remove this
+   special exception, which will cause the skeleton and the resulting
+   Bison output files to be licensed under the GNU General Public
+   License without this special exception.
+
+   This special exception was added by the Free Software Foundation in
+   version 2.2 of Bison.  */
+
+/* Tokens.  */
+#ifndef YYTOKENTYPE
+# define YYTOKENTYPE
+   /* Put the tokens into the symbol table, so that GDB and other debuggers
+      know about them.  */
+   enum yytokentype {
+     NUMBER = 258,
+     STRING = 259,
+     ISTRING = 260,
+     TIME = 261,
+     REGEX = 262,
+     IREGEX = 263,
+     OR = 264,
+     AND = 265,
+     NOT = 266,
+     IEQ = 267,
+     IN = 268,
+     GE = 269,
+     LE = 270,
+     GT = 271,
+     LT = 272,
+     NE = 273,
+     EQ = 274,
+     RE = 275,
+     LENGTH = 276,
+     NEG = 277
+   };
+#endif
+/* Tokens.  */
+#define NUMBER 258
+#define STRING 259
+#define ISTRING 260
+#define TIME 261
+#define REGEX 262
+#define IREGEX 263
+#define OR 264
+#define AND 265
+#define NOT 266
+#define IEQ 267
+#define IN 268
+#define GE 269
+#define LE 270
+#define GT 271
+#define LT 272
+#define NE 273
+#define EQ 274
+#define RE 275
+#define LENGTH 276
+#define NEG 277
+
+
+
+
+#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
+typedef union YYSTYPE
+#line 24 "mapparser.y"
+{
   double dblval;
   int intval;  
   char *strval;  
   struct tm tmval;
-} yystype;
-# define YYSTYPE yystype
+}
+/* Line 1489 of yacc.c.  */
+#line 100 "mapparser.h"
+	YYSTYPE;
+# define yystype YYSTYPE /* obsolescent; will be withdrawn */
+# define YYSTYPE_IS_DECLARED 1
 # define YYSTYPE_IS_TRIVIAL 1
 #endif
-# define	NUMBER	257
-# define	STRING	258
-# define	ISTRING	259
-# define	TIME	260
-# define	REGEX	261
-# define	IREGEX	262
-# define	OR	263
-# define	AND	264
-# define	NOT	265
-# define	RE	266
-# define	EQ	267
-# define	NE	268
-# define	LT	269
-# define	GT	270
-# define	LE	271
-# define	GE	272
-# define	IN	273
-# define	IEQ	274
-# define	LENGTH	275
-# define	NEG	276
 
-
 extern YYSTYPE msyylval;
 
-#endif /* not BISON_MAPPARSER_H */
Index: map.h
===================================================================
--- map.h	(revision 6142)
+++ map.h	(working copy)
@@ -308,11 +308,14 @@
 #define MS_ENCRYPTION_KEY_SIZE  16   /* Key size: 128 bits = 16 bytes */
 
 #define GET_LAYER(map, pos) map->layers[pos]
+
+#ifdef USE_MAPSCRIPT
 #define MS_REFCNT_INCR(obj) obj->refcount++
 #define MS_REFCNT_DECR(obj) (--(obj->refcount))
 #define MS_REFCNT_INIT(obj) obj->refcount=1
 #define MS_REFCNT_IS_NOT_ZERO(obj) (MS_REFCNT_DECR(obj))>0
 #define MS_REFCNT_IS_ZERO(obj) (MS_REFCNT_DECR(obj))<=0
+#endif
 
 #endif
 
@@ -593,7 +596,9 @@
 #ifdef SWIG
 %immutable;
 #endif /* SWIG */
+#ifdef USE_MAPSCRIPT
   int refcount;
+#endif /* MAPSCRIPT */
 #ifdef SWIG
 %mutable;
 #endif /* SWIG */
@@ -689,7 +694,9 @@
 #ifdef SWIG
 %immutable;
 #endif /* SWIG */
+#ifdef USE_MAPSCRIPT
   int refcount;
+#endif /* MAPSCRIPT */
   struct layer_obj *layer;
 #ifdef SWIG
 %mutable;
@@ -919,8 +926,10 @@
 #ifdef SWIG
 %immutable;
 #endif /* SWIG */
+#ifdef USE_MAPSCRIPT
   /* reference counting, RFC24 */
   int refcount;
+#endif
   int numclasses;
   int index;
   struct map_obj *map;
@@ -1065,8 +1074,10 @@
 #ifdef SWIG
 %immutable;
 #endif /* SWIG */
+#ifdef USE_MAPSCRIPT
   /* reference counting, RFC24 */
   int refcount;
+#endif
   int numlayers; /* number of layers in mapfile */
 
   symbolSetObj symbolset;
Index: Makefile.in
===================================================================
--- Makefile.in	(revision 6142)
+++ Makefile.in	(working copy)
@@ -184,6 +184,11 @@
 THREAD_LIB=@THREAD_LIB@
 
 #
+# Mapscript support (reference counting)
+#
+MAPSCRIPT=@MAPSCRIPT_ENABLED@
+
+#
 # libiconv - Enables Internationalization
 #
 ICONV=@ICONV_ENABLED@
@@ -220,7 +225,7 @@
 	$(GD_INC) $(AGG_INC) $(PDF_INC) $(PROJ_INC) $(TIFF_INC) $(JPEG_INC) $(EGIS_INC) \
 	$(SDE_INC) $(GDAL_INC) $(POSTGIS_INC) $(MYGIS_INC) \
 	$(CURL_INC) $(MING_INC) $(ORACLESPATIAL_INC) $(GEOS_INC) $(ICONV_INC) \
-	$(FASTCGI_INC) $(ZLIB_INC) $(XML2_INC) 
+	$(FASTCGI_INC) $(ZLIB_INC) $(XML2_INC) $(MAPSCRIPT)
 
 CFLAGS   = @CFLAGS@ $(FLAGS)
 CXXFLAGS = @CXXFLAGS@ $(FLAGS)
@@ -337,7 +342,7 @@
 mapscriptvars:	Makefile
 	touch mapscriptvars
 	pwd > mapscriptvars
-	echo $(IGNORE_MISSING_DATA) $(USE_POINT_Z_M) $(STRINGS) $(EPPL) $(PROJ) $(TTF) $(TIFF) $(JPEG) $(GD) $(SDE) $(OGR) $(GDAL) $(GEOS) $(OWS) >> mapscriptvars
+	echo $(IGNORE_MISSING_DATA) $(USE_POINT_Z_M) $(STRINGS) $(EPPL) $(PROJ) $(TTF) $(TIFF) $(JPEG) $(GD) $(SDE) $(OGR) $(GDAL) $(GEOS) $(OWS) $(MAPSCRIPT)>> mapscriptvars
 	echo -I. $(PROJ_INC) $(GD_INC) $(TTF_INC) $(TIFF_INC) $(JPEG_INC) $(SDE_INC) $(OGR_INC) $(GDAL_INC) $(GEOS_INC) >> mapscriptvars
 	echo $(EXE_LDFLAGS) >> mapscriptvars
 	echo $(STATIC_LIBS) >> mapscriptvars
Index: layerobject.c
===================================================================
--- layerobject.c	(revision 6142)
+++ layerobject.c	(working copy)
@@ -70,7 +70,9 @@
 #else
         layer->_class[layer->numclasses]=classobj;
 #endif
+	#ifdef USE_MAPSCRIPT
 	MS_REFCNT_INCR(classobj);
+	#endif
         layer->numclasses++;
         return layer->numclasses-1;
     }
@@ -89,7 +91,9 @@
         layer->_class[nIndex]=classobj;
 #endif
 
+	#ifdef USE_MAPSCRIPT
 	MS_REFCNT_INCR(classobj);
+	#endif
         /* increment number of layers and return */
         layer->numclasses++;
         return nIndex;
@@ -125,7 +129,9 @@
         classobj=layer->_class[nIndex];
 #endif
 	classobj->layer=NULL;
+	#ifdef USE_MAPSCRIPT
 	MS_REFCNT_DECR(classobj);
+	#endif
 
         /* Iteratively copy the higher index classes down one index */
         for (i=nIndex; i<layer->numclasses-1; i++)
Index: mapobject.c
===================================================================
--- mapobject.c	(revision 6142)
+++ mapobject.c	(working copy)
@@ -77,8 +77,9 @@
 
   if(!map) return;
 
-  /*printf("msFreeMap(): maybe freeing map at %p count=%d.\n",map, map->refcount);*/
+  #ifdef USE_MAPSCRIPT
   if ( MS_REFCNT_IS_NOT_ZERO(map) ) { return; }
+  #endif
   if (map->debug)
      msDebug("msFreeMap(): freeing map at %p.",map);
   /*printf("msFreeMap(): freeing map at %p.\n",map);*/
@@ -450,7 +451,9 @@
         map->layerorder[map->numlayers] = map->numlayers;
         GET_LAYER(map, map->numlayers) = layer;
         GET_LAYER(map, map->numlayers)->index = map->numlayers;
+	#ifdef USE_MAPSCRIPT
 	MS_REFCNT_INCR(layer);
+	#endif
         map->numlayers++;
         return map->numlayers-1;
     }
@@ -482,9 +485,11 @@
             if (map->layerorder[i] >= nIndex) map->layerorder[i]++;
         }
         map->layerorder[nIndex] = nIndex;
-        
+       
+	#ifdef USE_MAPSCRIPT 
         /* increment number of layers and return */
 	MS_REFCNT_INCR(layer);
+	#endif
         map->numlayers++;
         return nIndex;
     }
@@ -541,7 +546,9 @@
         /* decrement number of layers and return copy of removed layer */
         map->numlayers--;
 	layer->map=NULL;
+	#ifdef USE_MAPSCRIPT
 	MS_REFCNT_DECR(layer);
+	#endif
         return layer;
     }
 }
Index: classobject.c
===================================================================
--- classobject.c	(revision 6142)
+++ classobject.c	(working copy)
@@ -114,7 +114,9 @@
     }
     else if (nStyleIndex < 0) { /* Insert at the end by default */
         class->styles[class->numstyles]=style;
+	#ifdef USE_MAPSCRIPT
 	MS_REFCNT_INCR(style);
+	#endif
 	style->isachild=MS_TRUE;
         class->numstyles++;
         return class->numstyles-1;
@@ -126,7 +128,9 @@
             class->styles[i+1] = class->styles[i];
         }
         class->styles[nStyleIndex]=style;
+	#ifdef USE_MAPSCRIPT
 	MS_REFCNT_INCR(style);
+	#endif
 	style->isachild=MS_TRUE;
         class->numstyles++;
         return nStyleIndex;
@@ -156,7 +160,9 @@
         }
 	class->styles[class->numstyles-1]=NULL;
         class->numstyles--;
+	#ifdef USE_MAPSCRIPT
 	MS_REFCNT_DECR(style);
+	#endif
         return style;
     }
 }

