#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/stat.h>

/* modsql.c
 *
 * Copyright (c) 1999 Chris Studholme <chris.studholme@utoronto.ca>
 *
 * May be copied or modified under the terms of the GNU General Public
 * License.  See COPYING for more information.
 */

/*#define JAVAPATH     "/opt/java/bin/java"*/
#define JAVAPATH     "/opt/java/bin/java"
#define MEMORYOPTION "-mx32m"

extern char** environ;

main(int argc, char*argv[]) {
  char* class;
  char* classfile;
  char* classpath;
  char**newargv;
  struct stat buf;
  int i;

  if (argc<1) {
    fprintf(stderr,"unknown java class\n");
    return 1;
  }

  /* see if we can find the class file */
  classfile = malloc(strlen(argv[0])+7);
  strcpy(classfile,argv[0]);
  strcat(classfile,".class");

  /* figure out class name and classpath */
  classpath = argv[0];
  class = (char*)strrchr(argv[0],'/');
  if (class!=0)
    *class++=0;
  else {
    class=classpath;
    classpath=0;
  }
  if (classpath==0)
    classpath=".";

  /* if class file is not found and CLASSPATH exists in environment,
   * then use it */
  if ((stat(classfile,&buf)!=0)&&(getenv("CLASSPATH")))
    classpath=getenv("CLASSPATH");

  /* create new argument list for execvp() */
  newargv = malloc(sizeof(char*)*(5+argc));
  newargv[0]=class;
  newargv[1]=MEMORYOPTION;
  newargv[2]="-classpath";
  newargv[3]=classpath;
  newargv[4]=class;
  for (i=1; i<argc; ++i)
    newargv[4+i]=argv[i];
  argv[4+argc]=0;

  /* run java */
  execvp(JAVAPATH,newargv);
  fprintf(stderr,"exec() failed\n");
}
