package ahlers.phantom.embedded;

import de.flapdoodle.embed.process.config.store.DownloadConfigBuilder;
import de.flapdoodle.embed.process.config.store.DownloadPath;
import de.flapdoodle.embed.process.extract.UUIDTempNaming;
import de.flapdoodle.embed.process.io.directories.UserHome;
import de.flapdoodle.embed.process.io.progress.StandardConsoleProgressListener;

/**
 * @author [[mailto:michael@ahlers.consulting Michael Ahlers]]
 */
public class PhantomDownloadConfigBuilder
        extends DownloadConfigBuilder {

    public PhantomDownloadConfigBuilder() {
        fileNaming().setDefault(new UUIDTempNaming());

        /* Official download site appears problematic for programmatic user agents. See michaelahlers/specter#1 (and ariya/phantomjs#13951). */

        // downloadPath().setDefault(new DownloadPath("https://bitbucket.org/ariya/phantomjs/downloads/"));
        // downloadPath().setDefault(new DownloadPath("https://cnpmjs.org/downloads/"));
        downloadPath().setDefault(new DownloadPath("https://github.com/paladox/phantomjs/releases/download/2.1.7/"));

        progressListener().setDefault(new StandardConsoleProgressListener());
        packageResolver().setDefault(PhantomPackageResolver.getInstance());
        artifactStorePath().setDefault(new UserHome(".embedded-phantom"));
        downloadPrefix().setDefault(new DownloadPrefix("phantom-download"));
        userAgent().setDefault(new UserAgent("Mozilla/5.0 (compatible Embedded Phantom +https://github.com/michaelahlers/embedded-phantom)"));
    }

}

@@ -360,16 +360,45 @@
 tener;%0A%0A
+import java.nio.file.Paths;%0A%0A
 /**%0A * @
@@ -582,16 +582,25 @@
 gBuilder
+ defaults
 () %7B%0A   
@@ -608,51 +608,175 @@
     
-fileNaming().setDefault(new UUIDTempNaming(
+/* Names temporary, in-progress downloads. */%0A        fileNaming().setDefault(new UUIDTempNaming());%0A        downloadPrefix().setDefault(new DownloadPrefix(%22downloads%22
 ));%0A
@@ -1239,87 +1239,8 @@
 %22));
-%0A%0A        progressListener().setDefault(new StandardConsoleProgressListener());
 %0A   
@@ -1307,24 +1307,89 @@
 Instance());
+%0A%0A        /* Defines where completed download files will land. */
 %0A        art
@@ -1429,16 +1429,26 @@
 serHome(
+Paths.get(
 %22.embedd
@@ -1458,28 +1458,54 @@
 phantom%22
+, %22downloads%22).toString()
 ));%0A
+%0A
         
 download
@@ -1492,38 +1492,40 @@
 );%0A%0A        
-downloadPrefix
+progressListener
 ().setDefaul
@@ -1534,41 +1534,40 @@
 new 
-DownloadPrefix(%22phantom-download%22
+StandardConsoleProgressListener(
 ));%0A
@@ -1704,22 +1704,44 @@
 phantom)%22));
+%0A%0A        return this;
 %0A    %7D%0A%0A%7D%0A

Refine temp. naming