package org.smblott.intentradio;

import android.content.Context;
import android.os.Environment;
import android.os.Process;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class CopyResource extends Logger
{
   /* Install raw file resource id into location path on the SD card.
    *
    * Returns the full path on success (so the first character is '/'), or an
    * error message on failure.
    */

   public static String copy(Context context, int id, String path)
      { return copy(context, id, path, false); }

   public static String copy(Context context, int id, String path, boolean overwrite)
   {
      log("CopyResource id: ", ""+id);
      log("CopyResource path: ", path);

      boolean success = true;
      InputStream input = null;
      FileOutputStream output = null;
      File tmp = null;

      log("CopyResource SD card: ", Environment.getExternalStorageState(), ".");
      File sdcard = Environment.getExternalStorageDirectory();
      if ( sdcard == null || ! Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) )
         { return "Error:\nSD card not found or not ready."; }

      path = sdcard.getAbsolutePath() + "/" + path;
      log("CopyResource full path: ", path);

      File file = new File(path);
      File directory = new File(file.getParent());

      if ( ! directory.isDirectory() )
         { return "Error:\nParent directory does not exist...\n\n" + file.getParent(); }

      if ( file.exists() && ! overwrite )
         { return "Error:\nFile already exists, not copied...\n\n" + path; }

      try
      {
         tmp = File.createTempFile(".IntentRadio.", null, directory);
         log("CopyResource tmp path: ", tmp.toString());

         input = context.getResources().openRawResource(id);
         output = new FileOutputStream(tmp);

         byte[] buffer = new byte[1024];
         int count = 0;

         while ( 0 < (count = input.read(buffer)) )
            output.write(buffer, 0, count);
      }
      catch ( Exception e)
         { success = false; }
      finally
      {
         try
         {
            if ( input  != null ) input.close();
            if ( output != null ) output.close();
         }
         catch ( Exception e)
            { success = false; }
      }

      if ( success )
         success = tmp.renameTo(file);

      if ( tmp != null && tmp.exists() )
         if ( ! tmp.delete() )
            log("CopyResource failed to delete: ", tmp.toString());

      return success ? path : "Unknown error.";
   }
}

@@ -2015,86 +2015,140 @@
 t);%0A
+%0A
       
-%7D%0A      catch ( Exception e)%0A         %7B success = false; %7D%0A      finally
+   input.close();%0A         output.close();%0A%0A         input = null;%0A         output = null;%0A      %7D%0A      catch (Exception e1)
 %0A   
@@ -2157,27 +2157,40 @@
  %7B%0A         
-try
+success = false;
 %0A         %7B%0A
@@ -2183,24 +2183,28 @@
 e;%0A         
+try 
 %7B%0A          
@@ -2306,25 +2306,16 @@
    %7D
-%0A        
  catch (
  Exc
@@ -2310,17 +2310,16 @@
  catch (
- 
 Exceptio
@@ -2325,41 +2325,12 @@
 on e
-)%0A            %7B success = false; 
+2) %7B
 %7D%0A  

Reorganize exceptions.