/*
 * Written by: Thomas Fors.
 *
 * This script is free!
 *
 * I ask that if you find it useful, please contact Canon and tell them you
 * use Adobe Camera Raw for your raw conversions. Ask them to start
 * cooperating with Adobe so future Canon cameras will be supported by ACR
 * much quicker after release.
 *
 * You can contact Canon here:
 *   http://consumer.usa.canon.com/ir/controller?act=ProductInfoFormAct&fcategoryid=601
 *
 * You might also consider asking them to support a standard RAW format.
 *   http://rawformat.com
 *
 * Finally, the legal stuff..
 *
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// Find the Official ACR plugin in the expected location or ask the user to help
var originalACR = File(Folder.startup + "/Plug-Ins/File Formats/Camera Raw.8bi");
if ( !originalACR.exists ) {
		if ( Folder.fs == "Windows" ) {
				originalACR = File.openDialog("Find and Select the 'Camera Raw.8bi' File", "Photoshop Plug-ins:*.8bi");
		} else if ( Folder.fs == "Macintosh" ) {
				originalACR = File.openDialog("Find and Select the 'Camera Raw.8bi' File", pluginFilter);
		}
}

// Check if hack already exists and prompt the user asking what to do
var bRemove = false;
var bProceed = false;
var newACR = File(originalACR.path + "/Camera Raw Unofficial Canon 20D.8bi");
if ( newACR.exists ) {
		bRemove = confirm("Unofficial Canon 20D support already exists.\rDo you want to remove?");
		if (bRemove) {
				newACR.remove();
				alert("Unofficial Canon 20D support has been successfully removed.\rYou should restart Photoshop.");
		}
} else {
		bProceed = confirm("Do you want to add Unofficial Canon 20D support?");
}

if ( bProceed ) {
		// Create a backup copy of the original ACR file to work with.
		originalACR.copy(newACR);

		// Read backup copy of ACR into memory
		newACR.encoding = "BINARY";
		newACR.open("r");
		var memoryACR = newACR.read();
		newACR.close();

		// Perform some sanity checks to make sure this is the ACR plug-in
		var validACR = true;
		if (!memoryACR.match(/Adobe/)) validACR = false;
		if (!memoryACR.match(/Camera Raw/)) validACR = false;
		if (!memoryACR.match(/Thomas Knoll/)) validACR = false;
		if (!memoryACR.match(/Canon EOS-1D Mark II/)) validACR = false;

		if ( validACR ) {
				// Do the hack
				var loc = memoryACR.search(/Canon EOS-1D Mark II/);
				newACR.open("e");
				newACR.seek(loc, 0);
				newACR.write("Canon EOS 20D\0");
				newACR.close();
				alert("Unofficial Canon 20D support has been added! :-)\rYou need to restart Photoshop.");
		} else {
				alert("Official ACR Plug-in not found or incorrect version found.  Cannot create Unofficial Canon 20D support. :-(");
		}
}

//===============================
// Utility function to filter
// plugin files in dialog on Mac
// platform
//===============================
function pluginFilter(f) {
		lcName = f.name;
		lcName.toLowerCase();
		if ( lcName.indexOf(".8bi") == (f.name.length - 4) ) {
				return true;
		} else if ( f instanceof Folder ) {
				return true;
		} else {
				return false;
		}
}
