package com.nincraft.modpackdownloader.manager;

import com.nincraft.modpackdownloader.container.CurseFile;
import com.nincraft.modpackdownloader.handler.CurseFileHandler;
import com.nincraft.modpackdownloader.status.DownloadStatus;
import com.nincraft.modpackdownloader.util.DownloadHelper;
import com.nincraft.modpackdownloader.util.Reference;
import lombok.extern.log4j.Log4j2;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.io.File;
import java.io.IOException;

@Log4j2
public class ModPackManager {
	public static boolean updateModPack() {
		log.trace("Updating Curse modpack");
		String modPackIdName = "";
		try {
			modPackIdName = FileUtils.readFileToString(new File("modpackid"));
			log.info(String.format("Found modpackid file with id %s", modPackIdName));
		} catch (IOException e) {
			log.error("Could not find modpackid file", e);
			return false;
		}
		String modPackId = modPackIdName.substring(0, modPackIdName.indexOf('-'));
		if(!NumberUtils.isNumber(modPackId)){
			log.error(String.format("Unable to find a valid project ID, found %s", modPackId));
			return false;
		}
		String modPackName = modPackIdName.substring(modPackIdName.indexOf('-') + 1);
		CurseFile modPack = new CurseFile(modPackId, modPackName);
		modPack.initModpack();
		Reference.mcVersion = "*";
		CurseFileHandler.updateCurseFile(modPack);
		if (!modPack.getFileName().contains(".zip")) {
			modPack.setFileName(modPack.getFileName() + ".zip");
		}
		Reference.modFolder = ".";
		if (DownloadStatus.SKIPPED.equals(DownloadHelper.downloadFile(modPack, false))) {
			log.info(String.format("No new updates found for %s", modPack.getName()));
			return false;
		}
		Reference.modFolder = "mods";
		try {
			ZipFile modPackZip = new ZipFile(modPack.getFileName());
			modPackZip.extractAll(".");
		} catch (ZipException e) {
			log.error("Could not unzip modpack", e);
			return false;
		}

		return true;
	}

	public static void handlePostDownload() {
		try {
			File overrides = new File("overrides");
			FileUtils.copyDirectory(overrides, new File("."), true);
			FileUtils.deleteDirectory(overrides);
		} catch (IOException e) {
			log.error(e);
		}
	}
}

@@ -1086,16 +1086,17 @@
 ));%0A%09%09if
+ 
 (!Number
@@ -1121,16 +1121,17 @@
 PackId))
+ 
 %7B%0A%09%09%09log
@@ -1831,199 +1831,786 @@
 ;%0A%09%09
-try %7B%0A%09%09%09ZipFile modPackZip = new ZipFile(modPack.getFileName());%0A%09%09%09modPackZip.extractAll(%22.%22);%0A%09%09%7D catch (ZipException e) %7B%0A%09%09%09log.error(%22Could not unzip modpack%22, e);%0A%09%09%09return false;%0A%09%09%7D%0A
+File modsFolder = new File(Reference.modFolder);%0A%09%09File backupModsFolder = new File(%22backupmods%22);%0A%09%09try %7B%0A%09%09%09FileUtils.moveDirectory(modsFolder, backupModsFolder);%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09log.error(%22Could not backup mod folder%22, e);%0A%09%09%09return false;%0A%09%09%7D%0A%09%09try %7B%0A%09%09%09ZipFile modPackZip = new ZipFile(modPack.getFileName());%0A%09%09%09modPackZip.extractAll(%22.%22);%0A%09%09%7D catch (ZipException e) %7B%0A%09%09%09log.error(%22Could not unzip modpack%22, e);%0A%09%09%09try %7B%0A%09%09%09%09FileUtils.moveDirectory(backupModsFolder, modsFolder);%0A%09%09%09%7D catch (IOException e1) %7B%0A%09%09%09%09log.error(%22Could not restore backup mods folder%22, e1);%0A%09%09%09%09return false;%0A%09%09%09%7D%0A%09%09%09return false;%0A%09%09%7D%0A%09%09try %7B%0A%09%09%09FileUtils.deleteDirectory(backupModsFolder);%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09log.error(%22Unable to delete backup mods folder%22, e);%0A%09%09%7D
 %0A%09%09r
@@ -2869,13 +2869,14 @@
 e);%0A%09%09%7D%0A
+%0A
 %09%7D%0A%7D%0A

Backup mods directory during update of modpack