
console.log("Copying libc++_shared.so to jniLibs folder");

let custompath = undefined;
process.argv.forEach(function (val, index, array) {
	if (index === 2) {
		custompath = val;
	}
});

if (custompath) {
	console.log("You have specified the custom path: " + custompath);	
}


try {
	const fs = require('fs');
	const path = require('path');

	const source = path.join(__dirname, "android", "app", "src", "main", "jniLibs", "arm64-v8a", "libc++_shared.so");
	let destination;

	if (custompath) {
		destination = path.join(custompath, "jniLibs");
	} else {
		destination = path.join("..", "..", "android", "app", "src", "main", "jniLibs");
	}

	if (!fs.existsSync(destination)) {
	    fs.mkdirSync(destination);
	}

	destination = path.join(destination, "arm64-v8a")

	if (!fs.existsSync(destination)) {
	    fs.mkdirSync(destination);
	}

	destination = path.join(destination, "libc++_shared.so")

	console.log(destination);
	fs.createReadStream(source).pipe(fs.createWriteStream(destination));
	console.log("Done copying libc++_shared.so to jniLibs folder");
} catch (error) {
	console.log("------------------------------------------------------------------------")
	console.log("Encountered an error trying to copy c++ shared library:", error.message);
	console.log("Your repositroy structure may be off.. either by mistake or on purpose.")
	console.log("------------------------------------------------------------------------")
	console.log("If your android app folder is not, relative node_modules/react-native-scanbot-sdk, at ../../android/app/")
	console.log("Please run this script again and specify your destination path manually, e.g:")
	console.log("node node_modules/react-native-scanbot-sdk/copy-shared-library.js ../workpace-2/android/app/src/main/")
	console.log("------------------------------------------------------------------------")
}








