import java.io.*;
import java.util.*;
import java.net.*;

class install_lapack {


   public static void download (String source, String destination) {

      URL u;
      InputStream is = null;
      DataInputStream dis;
      FileOutputStream fos = null;
      String s;

      try {

         //------------------------------------------------------------//
         //  Create the URL.                                   //
         //------------------------------------------------------------//

         u = new URL(source);

         //----------------------------------------------//
         //   Open an input stream from the url.  //        //----------------------------------------------//

         is = u.openStream();         // throws an IOException

         //-------------------------------------------------------------//
         // Convert the InputStream to a buffered DataInputStream.      //
         // Buffering the stream makes the reading faster; the          //
         // readLine() method of the DataInputStream makes the reading  //
         // easier.                                                     //
         //-------------------------------------------------------------//

         dis = new DataInputStream(new BufferedInputStream(is));

         //------------------------------------------------------------//
         // Now just read each record of the input stream, and print   //
         // it out.  Note that it's assumed that this problem is run   //
         // from a command-line, not from an application or applet.    //
         //------------------------------------------------------------//
        fos = new FileOutputStream( destination );
        final int BUFSIZ = 4096;
        byte inbuf[] = new byte[BUFSIZ];
        int n;

        while ((n = dis.read(inbuf, 0, BUFSIZ)) != -1)
          fos.write(inbuf, 0, n);



      } catch (MalformedURLException mue) {

         System.out.println(" a MalformedURLException happened.");
         mue.printStackTrace();
         System.exit(1);

      } catch (IOException ioe) {

         System.out.println(" IOException happened.");
         ioe.printStackTrace();
         System.exit(1);

      } finally {

         //---------------------------------//
         // Step 6:  Close the InputStream  //
         //---------------------------------//

         try {
            is.close();
            fos.close();
            fos = null;
         } catch (IOException ioe) {
            // just going to ignore this one
         }

      } // end of 'finally' clause
   }  // end of put



      public static void main ( String [ ] args ) {
/*
         System.out.println("os = " + System.getProperty("os.name"));
         System.out.println("arch = " + System.getProperty("os.arch"));
         System.out.println("version = " + System.getProperty("os.version"));
*/
         Properties sysProps = System.getProperties();

         boolean sse_supported = false;
         boolean sse2_supported = false;
         boolean sse3_supported = false;
         boolean amd = false;
         boolean intel = false;
         boolean sixty_four_bit = false;
         String file, install_dir,install_file;
         String cmd;

         try {

               if( args.length >= 1 ) {
                  install_dir = args[0];
               } else {
                  install_dir = "/tmp";
               }

               File dir = new File( install_dir );
               if( dir.exists() ) {
                  System.out.println( install_dir+" exists");
               } else {
                  System.out.println( "ERROR "+install_dir+" does not  exists");
               }

               if( dir.canWrite() ) {
                  System.out.println( install_dir+" is writeable");
               } else {
                  System.out.println( "ERROR "+install_dir+" is not writeable");
               }

               file = "/cpuid_linux.exe";
               install_file = install_dir + file;

      System.out.println("install_file ="+install_file);

               download( "http://dev.simtk.org/jacklm/lapack/cpuid_linux.exe",
                        install_file );

               File f = new File(install_file);


               // wait for cpuid_linux.exe to completely download 
               int i = 0;
               while( (i < 100) && (f.length() < 14607) ) {
                  try { Thread.sleep(100); }
                  catch (InterruptedException e) {}
                  i++;
               }

/*
               for( int i=0;i<=10;i++) {
                   if( f.exists() ) break;
                    System.out.println("waiting for "+install_file+" to be installed i="+i);
                   try { Thread.sleep(1000); }
                   catch (InterruptedException e) {}
               }
*/
               // make sure cpuid_linux.exe is executable
               cmd = "chmod 777 " + install_file;
               Process p = Runtime.getRuntime().exec(cmd);
               try { Thread.sleep(500); }
               catch (InterruptedException e) {}

               // run cpuid_linux.exe and catch its output 
               p = Runtime.getRuntime().exec(install_file);
               BufferedReader in = new BufferedReader(
               new InputStreamReader(p.getInputStream()));
               String line = null;

               // determine CPU manufacturer
               line = in.readLine();
               if( line != null ) {
                  if( line.endsWith("GenuineIntel") ) {
                      intel = true; 
                  } else if( line.endsWith( "AuthenticAMD" ) ) {
                      amd = true; 
                  } else {
                      System.out.println(" *** Unknown CPU manufacturer  *** \n");
                  }
               }

               // determine which instruction set extensions are supported 
               while ((line = in.readLine()) != null) {
                  if( line.endsWith("SSE") ) sse_supported = true;
                  if( line.endsWith("SSE2") ) sse2_supported = true;
                  if( line.endsWith("SSE3") ) sse3_supported = true;
               }

               
               p = Runtime.getRuntime().exec("uname -i");
               in = new BufferedReader(
               new InputStreamReader(p.getInputStream()));
            
               // determine if 32/64bit system
               line = null;
               while ((line = in.readLine()) != null) {
                  if( line.endsWith("64") ) {
                     sixty_four_bit = true; 
                  }
               }

               if ( intel ) {
                  if( sixty_four_bit ) {
                         file = "/intel64_lapack";
                  } else {
                         file = "/intel32_lapack";
                  }
               } else if (amd){
                  if( sixty_four_bit ) {
                         file = "/amd64_lapack";
                  } else {
                         file = "/amd32_lapack";
                  }

               }

      System.out.println("install_file ="+install_dir+file);

               download( "http://dev.simtk.org/jacklm/lapack"+file, 
                           install_dir + file);


             } catch (IOException e) {
               e.printStackTrace();
             }
      }






}
