package CygwinShellInterface;
/* CygwinShellInterface package Java source file
 * Last Change: Mon Aug 19 08:00 PM 2002 EDT
 *  **Development version**
 * Eventual release will be in the top-package name "com.DeeperSkills"
 * (so that if you wish to install the software now in a forward-looking
 * manner, do [optionally]):
 *     $mkdir "com/DeeperSkills/CygwinShellInterface"
 * under your CLASSPATH root directory, _and_ change the first line of
 * this file to:
 *    "package com.DeeperSkills.CygwinShellInterface"
 * _before_ running the java compiler.
 *
 * (C)2002 Soren Andersen <libertador@flashmail.com> All Rights Reserved.
 * This file is Free Software. It may only be used, modified, or
 * redistributed in compliance with the terms set forth in the GNU LGPL
 * (Library General Public License) -- see www.fsf.org for more info.
 * CygwinShellInterface 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.
 *    If you do not agree with these terms you must remove this software
 * from your computer NOW.
 *
 * Note: Something similar in implementation to how this package does
 * its thing (Java Native Interface - Win32 Registry) has been done by:
 *     http://www.trustice.com/java/jnireg/index.shtml
 */

import java.util.*;
import java.io.IOException;

public class CygwinShellPathXl8or {
  static CygwinTestFor cygwin_exists = new CygwinTestFor();
  public String GetCDP()
  {
    if(cygwin_exists.test())
    {
        CygDrivePrefix cdp = new CygDrivePrefix();
        String cygdpre;
        cygdpre = cdp.GetCygDrivePrefix();
        return cygdpre;
    }
    else
    {
      return "";
    }
  }
} /* end of public class */


// We're "friendly" -- no access from outside package
class CygwinTestFor {
  private native boolean cygwinExists();
  public static boolean test()
  {
    CygwinTestFor yepnope = new CygwinTestFor();
    boolean cygwin_on_system = false;
    try {
       cygwin_on_system = yepnope.cygwinExists();
    } catch(Exception e) {
      System.out.println("A program error is happening, here is Java's esoteric-oo message:");
      System.out.println( e.getMessage());
    }
    return cygwin_on_system;
  }
  static {
    try {
      System.loadLibrary("Java_CygwinInterface");
    } catch(UnsatisfiedLinkError e) {
      System.out.println("A JVM exception was caught:");
      System.out.println( e.getMessage());
    }
  }
} /* class is over */


class CygDrivePrefix {
  private native int getCygdrivePathPrefix();
  private static StringBuffer cygDrvprefix = new StringBuffer(32);

  static {
    try {
      System.loadLibrary("Java_CygwinInterface");
    } catch(UnsatisfiedLinkError e) {
      System.out.println("A JVM exception was caught:");
      System.out.println( e.getMessage());
    }
  }

/* For package testing purposes */
  public static void main(String args[])
  {
    CygwinTestFor test_if_cygwin = new CygwinTestFor();
    if(test_if_cygwin.test())
    {
      System.out.println("A Cygwin installation has been detected on this system, " +
                         " getting prefix string...");
      CygDrivePrefix cdp = new CygDrivePrefix();
      int retnlength = 0;
      try
      {
        retnlength = cdp.getCygdrivePathPrefix();
      } catch(Exception e) {
        System.out.println("A program error is happening, here is Java's " +
                           "esoteric-oo message:");
        System.out.println( e.getMessage() );
      }
      if(retnlength > 0)
      {
        System.out.println("Cygwin shell path prefix successfully retrieved: \"" +
            cygDrvprefix + "\"");
      }
    }
    else
    {
      System.out.println("No Cygwin installation detected on this system.");
    }
  }


  protected String GetCygDrivePrefix()
  {

    CygDrivePrefix cdp = new CygDrivePrefix();
    int retnlength = 0;
      try
      {
        retnlength = cdp.getCygdrivePathPrefix();
      } catch(Exception e) {
        System.out.println("A program error is happening, here is Java's " +
                           "esoteric-oo message:");
        System.out.println( e.getMessage() );
      }
      if(retnlength > 0)
      {
        return "" + cygDrvprefix;
      }
      else
      {
        return ""; /* hopefully never happens */
      }
  } /* method's over */
} /* class is done */

code syntax highlighting by GVIM, using the "nightshimmer" theme.