package com.horstmann.labrat;

import java.io.File;

public class Javac extends org.apache.tools.ant.taskdefs.Javac
{

   @Override
   protected String getSystemJavac()
   {
      
      String superJavac = super.getSystemJavac();
      if (superJavac == null || superJavac.equals("javac.exe"))
      {
         String javac = null;
         File javaHome = new File(System.getProperty("java.home"));
         while (javac == null && javaHome != null)
         {
            File javacNoExt = new File(new File(javaHome, "bin"), "javac");
            if (javacNoExt.exists()) javac = javacNoExt.getAbsolutePath();
            else
            {
               File javacExe = new File(new File(javaHome, "bin"), "javac.exe");
               if (javacExe.exists()) javac = javacExe.getAbsolutePath();
            }
            if (javac == null && javaHome.getName().contains("jre"))
            {
               File jdkHome = new File(javaHome.getParentFile(), javaHome.getName().replace("jre", "jdk"));
  
               javacNoExt = new File(new File(jdkHome, "bin"), "javac");
               if (javacNoExt.exists()) javac = javacNoExt.getAbsolutePath();
               else
               {
                  File javacExe = new File(new File(jdkHome, "bin"), "javac.exe");
                  if (javacExe.exists()) javac = javacExe.getAbsolutePath();
               }
            }
            javaHome = javaHome.getParentFile();
         }
         if (javac != null) return javac;
      }
      
      return superJavac;
   }
  
}
