package de.saumya.mojo.rails3;

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

import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.FileUtils;

/**
 * goal to run rails command with the given arguments. either to generate a
 * fresh rails application or to run the rails script from within a rails
 * application.
 * 
 * @goal rails
 * @execute phase="initialize"
 */
public class RailsMojo extends AbstractRailsMojo {

    /**
     * arguments for the rails command
     * 
     * @parameter default-value="${rails.args}"
     */
    protected String railsArgs = null;

    /**
     * the path to the application to be generated
     * 
     * @parameter default-value="${app_path}"
     */
    protected String appPath   = null;

    @Override
    public void executeWithGems() throws MojoExecutionException {
        String commandString;
        if (railsScriptFile().exists() && this.appPath == null) {
            commandString = railsScript("");
        }
        else {
            commandString = binScript("rails");
            if (this.appPath != null) {
                commandString += " " + this.appPath;
            }
        }
        if (this.railsArgs != null) {
            commandString += " " + this.railsArgs;
        }
        if (this.args != null) {
            commandString += " " + this.args;
        }
        execute(commandString, false);
        if (this.appPath != null) {
            final File app = new File(this.project.getBasedir(), this.appPath);
            // rectify the prolog of the script to use ruby instead of jruby
            final File script = new File(new File(app, "script"), "rails");
            try {
                FileUtils.fileWrite(script.getAbsolutePath(),
                                    FileUtils.fileRead(script).replace("jruby",
                                                                       "ruby"));
            }
            catch (final IOException e) {
                throw new MojoExecutionException("failed to filter " + script,
                        e);
            }
            // rectify the Gemfile to allow both ruby + jruby to work
            // TODO make this to work NOT ONLY with sqlite3
            final File gemfile = new File(app, "Gemfile");
            try {
                FileUtils.fileWrite(gemfile.getAbsolutePath(),
                                    FileUtils.fileRead(gemfile)
                                            .replace("\ngem \"sqlite3-ruby\", :require => \"sqlite3\"\n",
                                                     "\ngem \"sqlite3-ruby\", :require => \"sqlite3\" unless defined?(JRUBY_VERSION)\n"
                                                             + "gem \"activerecord-jdbc-adapter\", :require =>'jdbc_adapter' if defined?(JRUBY_VERSION)\n"
                                                             + "gem \"jdbc-sqlite3\", :require => 'jdbc/sqlite3' if defined?(JRUBY_VERSION)\n"));
            }
            catch (final IOException e) {
                throw new MojoExecutionException("failed to filter " + script,
                        e);
            }
            try {
                // TODO obey force, skip and pretend flags !!
                // TODO some screen logging maybe !?
                FileUtils.copyURLToFile(Thread.currentThread()
                        .getContextClassLoader()
                        .getResource("pom.xml"), new File(app, "pom.xml"));
            }
            catch (final IOException e) {
                throw new MojoExecutionException("error copying pom.xml", e);
            }
        }
    }
}

@@ -73,16 +73,80 @@
 ception;
+%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0Aimport
@@ -2142,32 +2142,381 @@
 );%0A            %7D
+%0A%0A            final String database;%0A            final Pattern pattern = Pattern.compile(%22.*-d%5C%5Cs+(%5Ba-z0-9%5D+).*%22);%0A            final Matcher matcher = pattern.matcher(commandString);%0A            if (matcher.matches()) %7B%0A                database = matcher.group(1);%0A            %7D%0A            else %7B%0A                database = %22sqlite3%22;%0A            %7D
 %0A            // 
@@ -2574,68 +2574,8 @@
 ork%0A
-            // TODO make this to work NOT ONLY with sqlite3%0A
     
@@ -2830,16 +2830,21 @@
 lace
+First
 (%22%5Cngem 
 %5C%22sq
@@ -2843,54 +2843,40 @@
 gem 
+(
 %5C%22
-sqlite3-ruby%5C%22, :require =%3E %5C%22sqlite3%5C%22%5Cn%22,%0A
+%5B%5Er%5D%5Ba-z0-9-%5D+%5C%22.*)%5Cn%22,%0A     
     
@@ -2935,49 +2935,10 @@
 gem 
-%5C%22sqlite3-ruby%5C%22, :require =%3E %5C%22sqlite3%5C%22
+$1
  unl
@@ -2960,32 +2960,37 @@
 UBY_VERSION)%5Cn%22%0A
+     
                 
@@ -3181,32 +3181,37 @@
                 
+     
 + %22gem %5C%22jdbc-sq
@@ -3212,44 +3212,326 @@
 dbc-
-sqlite3%5C%22, :require =%3E 'jdbc/sqlite3
+%22%0A                                                                  + database%0A                                                                  + %22%5C%22, :require =%3E 'jdbc/%22%0A                                                                  + database%0A                                                                  + %22
 ' if

allow other databases then sqlite3 to work