/*package build_tools;*/

/*

* No - this has to be rewrite. It is too convoluted.
* I also no longer want class Colours in here.
*
*
*/

import java.io.*; import java.io.File; import java.nio.file.Files; import java.util.stream.Stream; import java.nio.file.Paths;

class Colours {

public static final String ANSI_RESET  = "\u001B[0m";
public static final String ANSI_BLACK  = "\u001B[30m";
public static final String ANSI_RED    = "\u001B[31m";
public static final String ANSI_GREEN  = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE   = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN   = "\u001B[36m";
public static final String ANSI_WHITE  = "\u001B[37m";

public static final String ANSI_BLACK_BACKGROUND  = "\u001B[40m";
public static final String ANSI_RED_BACKGROUND    = "\u001B[41m";
public static final String ANSI_GREEN_BACKGROUND  = "\u001B[42m";
public static final String ANSI_YELLOW_BACKGROUND = "\u001B[43m";
public static final String ANSI_BLUE_BACKGROUND   = "\u001B[44m";
public static final String ANSI_PURPLE_BACKGROUND = "\u001B[45m";
public static final String ANSI_CYAN_BACKGROUND   = "\u001B[46m";
public static final String ANSI_WHITE_BACKGROUND  = "\u001B[47m";

/* Bold Colour variants */
public static final String COLOUR_BOLD_BLACK  = "\u001b[1;30m"; // BOLD BLACK
public static final String COLOUR_BOLD_RED    = "\u001b[1;31m"; // BOLD RED
public static final String COLOUR_BOLD_GREEN  = "\u001b[1;32m"; // BOLD GREEN
public static final String COLOUR_BOLD_YELLOW = "\u001b[1;33m"; // BOLD YELLOW
public static final String COLOUR_BOLD_BLUE   = "\u001b[1;34m"; // BOLD BLUE
public static final String COLOUR_BOLD_PURPLE = "\u001b[1;35m"; // BOLD PURPLE
public static final String COLOUR_BOLD_CYAN   = "\u001b[1;36m"; // BOLD CYAN
public static final String COLOUR_BOLD_WHITE  = "\u001b[1;37m"; // BOLD WHITE
public static final String TRUE_RESET         = "\u001b[0;37m";

/*
 * Colours.rev()
 */
public static String rev() {
  return TRUE_RESET;
}

/*
 * blue()
 */
public static String blue(String i) {
  return ANSI_BLUE+i+ANSI_WHITE;
}

/*
 * lightblue()
 */
public static String lightblue(String i) {
  return COLOUR_BOLD_BLUE+i+TRUE_RESET;
}

/*
 * red()
 */
public static String red(String i) {
  return ANSI_RED+i+ANSI_RESET;
}

/*
 * Main constructor (def initialize)
 */
public Colours() {
}

/*
 * run()
 */
void run() {
}

public static void e(String i) {
  System.out.print(i);
}

public static void en(String i) {
  System.out.println(i);
}

public static void main(String[] args) {
  Colours x = new Colours();
}

}

/*

* class BuildTools
*
* This is mostly test-code, to see whether we could re-write the
# RBT tools in Java.
*
* Todo: grab the info from the file generated via
*       RBT.create_simple_version_file
*/

public class BuildTools {

/*
 * TARGET_FILE
 *
 * Specify a default file to work with.
 */
public static final String TARGET_FILE =
  "/home/x/src/htop/htop-3.2.2.tar.xz";

String[] commandline_arguments;

/*
 * BASE_DIR
 *
 * This should be determined from a .yml file really.
 * At the least in a conditional manner.
 */
private String BASE_DIR = "/home/x/src/";

/*
 * Main constructor (def initialize)
 */
public BuildTools(String[] args) {
  commandline_arguments = args;

  for(int i=0; i < commandline_arguments.length; i += i){
    /*
     * handle the commandline arguments next
     */
    if (commandline_arguments[i].matches("help|--help")) {
      show_help();
      System.exit(0);
    }
    else if (commandline_arguments[i].isEmpty() || commandline_arguments[i] == null) {
      en("Please provide the name of the program that is");
      en("to be compiled.");
    }
    else {
      handle_this_program(commandline_arguments[i]);
      System.exit(0);
    }
  }
  /*run();*/
}

/*
 * return_files_from
 */
File[] return_files_from(String i) {
  return new File(i).listFiles();
}

/*
 * handle_this_program()
 */
void handle_this_program(String i) {
  /*
   * First, check whether we need to determine the real path.
   *
   * This is currently hardcoded.
   */
  boolean does_the_input_include_a_slash = i.contains("/");
  if (does_the_input_include_a_slash) {
    /* In this case we will not do anything. */
  }
  else {
    /* Else prepend BASE_DIR */
    String target_directory = BASE_DIR+i+'/';

    try {
      File[] possible_files = return_files_from(target_directory);

      if (possible_files != null) {

        /* Determine the target file here. */
        String target_file = String.valueOf(possible_files[0]);
        en(
          Colours.rev()+
          "Now using the file at "+
          Colours.red(target_file)
        );
      }
      else {
        en("Nothing was found.\n");
      }
    }
    catch (Exception error) {
      System.err.println(error.getMessage());
    }

  }
  /* Store a reference to the file we are working with next. */
  File file = new File(target_file);
  String cd_into_this_directory = file.getName();
  cd(
    BASE_DIR+cd_into_this_directory+"/"
  );

  if(file.exists() && !file.isDirectory()) {
    e(
      Colours.rev()+"The file at `"+Colours.lightblue(i)+"` exists."
    );
    /* Extract this file next */
    try {
      report_pwd();
      e("It will be extracted to the current working directory next.");
      String command1 = "tar -xvf "+TARGET_FILE;
      // using the Runtime exec method, as a sys-cmd:
      en("");
      en(command1);
      en("");
      Process p1 = Runtime.getRuntime().exec(command1);
      /*
       * Next we may use configure or meson or cmake.
       */
      String command2 = "tar -xvf "+TARGET_FILE+"configure";
      // using the Runtime exec method, as a sys-cmd:
      Process p2 = Runtime.getRuntime().exec(command2);
      /*cd(cd_into_this_directory);*/
    }
    catch (IOException e) {
      en("exception happened - here's what I know: ");
      e.printStackTrace();
      System.exit(-1);
    }
  }
  else {
    e("No such file exists at `"+Colours.red(i)+"`.");
  }

}

/*
 * cd tag
 */
public static String cd(String i) {
  /*String target = new File(i).getAbsolutePath();*/
  System.setProperty("user.dir", new File(i).getAbsolutePath());
  return System.getProperty("user.dir");
}

/*
 * report_pwd()
 */
public static void report_pwd() {
  e(
    "The current working directory is: "+return_pwd()
  );
}
/*
 * return_pwd()
 */
public static String return_pwd() {
  String dir = new File(
    System.getProperty("user.dir")
  ).getAbsolutePath()+"/";
  return dir;
}

/*
 * show_help()                                                     (help tag)
 */
public static void show_help() {
  en("The available help options are:");
  en("");
  en("");
}

/*
 * run()
 */
void run() {
  en("And this is called from run().");
}

public static void en() { /* newline */
  System.out.print("\n");
}

/*
 * General echo-output.
 */
public static void e(String i) {
  System.out.println(i);
}

public static void en(String i) {
  System.out.println(i);
}

public static void main(String[] args) {
  BuildTools x = new BuildTools(args);
}

} /*

java BuildTools expat */