package com.renard.ocr.help;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;

import android.app.DownloadManager;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.ParcelFileDescriptor;

import com.renard.util.Util;

public class OCRLanguageInstallService extends IntentService {

	protected static final String ACTION_INSTALL_COMPLETED = "com.renard.ocr.ACTION_OCR_LANGUAGE_INSTALLED";
	protected static final String ACTION_INSTALL_FAILED = "com.renard.ocr.ACTION_INSTALL_FAILED";
	protected static final String EXTRA_OCR_LANGUAGE = "ocr_language";
	protected static final String EXTRA_OCR_LANGUAGE_DISPLAY = "ocr_language_display";
	public static final String EXTRA_STATUS = "status";

	
	public OCRLanguageInstallService(){
		this(OCRLanguageInstallService.class.getSimpleName());
	}

	public OCRLanguageInstallService(String name) {
		super(name);
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		if (!intent.hasExtra(DownloadManager.EXTRA_DOWNLOAD_ID)) {
			return;
		}
		final long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
		if (downloadId != 0) {

			DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
			ParcelFileDescriptor file;
			try {
				file = dm.openDownloadedFile(downloadId);
				FileInputStream fin = new FileInputStream(file.getFileDescriptor());
				BufferedInputStream in = new BufferedInputStream(fin);
				FileOutputStream out = openFileOutput("tess-lang.tmp", Context.MODE_PRIVATE);
				GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
				final byte[] buffer = new byte[2048];
				int n = 0;
				while (-1 != (n = gzIn.read(buffer))) {
					out.write(buffer, 0, n);
				}
				out.close();
				gzIn.close();
				FileInputStream fileIn = openFileInput("tess-lang.tmp");
				TarArchiveInputStream tarIn = new TarArchiveInputStream(fileIn);

				TarArchiveEntry entry = tarIn.getNextTarEntry();
				if (entry != null) {
					byte[] content = new byte[(int) entry.getSize()];
					int bytesRead = 0;
					while (bytesRead < entry.getSize()) {
						bytesRead += tarIn.read(content, bytesRead, content.length - bytesRead);
					}
					File extDir = Environment.getExternalStorageDirectory();
					File tessDir = new File(extDir, Util.OCR_DATA_DIRECTORY);
					final String langName = entry.getName().substring("tesseract-ocr/tessdata/".length());
					File trainedData = new File(tessDir, langName);
					FileOutputStream fout = new FileOutputStream(trainedData);
					fout.write(content);
					fout.close();
					String lang = langName.substring(0, langName.length() - ".traineddata".length());
					notifyReceivers(lang);
				}
				tarIn.close();

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

		}
	}

	private void notifyReceivers(String lang) {
		//notify language activity
		Intent resultIntent = new Intent(ACTION_INSTALL_COMPLETED);
		resultIntent.putExtra(EXTRA_OCR_LANGUAGE, lang);
		resultIntent.putExtra(EXTRA_STATUS, DownloadManager.STATUS_SUCCESSFUL);
		sendBroadcast(resultIntent);
	}
}

@@ -2359,24 +2359,194 @@
 TarEntry();%0A
+%09%09%09%09%0A%09%09%09%09while (entry!=null && !(entry.getName().endsWith(%22.traineddata%22) && !entry.getName().endsWith(%22_old.traineddata%22)))%7B%0A%09%09%09%09%09entry = tarIn.getNextTarEntry();%0A%09%09%09%09%7D%0A
 %09%09%09%09if (entr

fix install language service for archives that contain several files