package dk.statsbiblioteket.doms.transformers.fileenricher;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.ParseException;
import java.util.List;

import javax.xml.bind.JAXBException;

import dk.statsbiblioteket.doms.central.CentralWebservice;
import dk.statsbiblioteket.doms.transformers.common.DomsWebserviceFactory;
import dk.statsbiblioteket.doms.transformers.common.FileRecordingObjectListHandler;
import dk.statsbiblioteket.doms.transformers.common.ObjectHandler;
import dk.statsbiblioteket.doms.transformers.common.ObjectListHandler;
import dk.statsbiblioteket.doms.transformers.common.TrivialUuidFileReader;
import dk.statsbiblioteket.doms.transformers.common.UuidFileReader;
import dk.statsbiblioteket.doms.transformers.common.checksums.ChecksumParser;

/**
 * Tool for enriching Radio/TV file metadata.
 * Takes as input a file with program uuids (one per line). For each file, enriches metadata.
 */
public class FileEnricher {
    public static void main(String[] args) throws IOException, JAXBException, URISyntaxException, ParseException {
        //TODO: Setup apache CLI
        File uuidfile = new File(Thread.currentThread().getContextClassLoader().getResource(args[0]).toURI());
        File configfile = new File(Thread.currentThread().getContextClassLoader().getResource(args[1]).toURI());

        UuidFileReader uuidFileReader = new TrivialUuidFileReader();
        FileEnricherConfig config = new FFProbeLocationPropertyBasedDomsConfig(configfile);
        CentralWebservice webservice = new DomsWebserviceFactory(config).getWebservice();

        ChecksumParser checksums = new ChecksumParser(Thread.currentThread().getContextClassLoader().getResourceAsStream("md5s.zip"));

        ObjectHandler delegate = new DomsFFProbeFileEnricherObjectHandler(config, webservice);
        ObjectHandler objectHandler = new DomsFileEnricherObjectHandler(config, webservice,checksums.getNameChecksumsMap(), delegate);


        ObjectListHandler objectListHandler = new FileRecordingObjectListHandler(config, objectHandler);

        List<String> uuids = uuidFileReader.readUuids(uuidfile);
        objectListHandler.transform(uuids);
    }

}

@@ -1203,32 +1203,117 @@
 file = new File(
+args%5B0%5D);%0A%0A        File configFile = null;%0A%0A        try %7B%0A            URL resource = 
 Thread.currentTh
@@ -1359,24 +1359,74 @@
 rce(
-args%5B0%5D)
+%22fileenricher.properties%22);%0A            URI uri = resource
 .toURI()
 );%0A 
@@ -1421,17 +1421,16 @@
 .toURI()
-)
 ;%0A      
@@ -1435,20 +1435,19 @@
     
+ 
-File
+  
  config
-f
+F
 ile 
@@ -1461,85 +1461,179 @@
 ile(
-Thread.currentThread().getContextClassLoader().getResource(args%5B1%5D).toURI());
+uri);%0A        %7D catch (Exception e) %7B%0A            System.err.println(%22fileenricher.properties not found, try putting it in 'conf'.%22);%0A            System.exit(1);%0A        %7D
 %0A%0A  
@@ -1784,17 +1784,17 @@
 g(config
-f
+F
 ile);%0A  

Load uuid-file directly, instead of looking on the classpath.