open Unix
open Universal

(******************************** Utils Module ********************************)
(******************************************************************************)

  returns true if the path contains an initialized  repo , 
  * otherwise returns false
 * otherwise returns false *)
let cml_initialized (path : string) : bool =
  Sys.file_exists (path^".cml")

  returns true if the current directory ( or parent ) is an initialized  repo , 
  * otherwise returns false
 * otherwise returns false *)
let cml_initialized_r (path : string) : bool =
  try match Filesystem.cml_path path with
    | Some _ -> true
    | None -> false
  with
  | Fatal _ -> false

(* returns whether the given argument is a flag (if arg is of the form
 * dash [-] followed by any number of characters > 0) *)
let arg_is_flag (arg : string) : bool =
  let r = Str.regexp "^-.*" in
  Str.string_match r arg 0

  precondition : [ arg ] is a flag . 
  * postcondition : returns the list of flags from the argument . 
  * example : [ get_flags_from_arg "  " ~ [ " h " ; " i " ] ] 
  * example : [ get_flags_from_arg " --hi " ~ [ " hi " ] ]
 * postcondition: returns the list of flags from the argument.
 * example: [get_flags_from_arg "-hi" ~ ["h"; "i"]]
 * example: [get_flags_from_arg "--hi" ~ ["hi"]] *)
let get_flags_from_arg (arg : string) : string list =
  let r_double_dash = Str.regexp "^--" in
  let r_single_dash = Str.regexp "^-" in
  if Str.string_match r_double_dash arg 0 then
    [Str.replace_first r_double_dash "" arg]
  else
    Str.(replace_first r_single_dash "" arg |> split (Str.regexp ""))

(* fetch the user info (username) *)
let get_user_info () : string =
  try
    let ic = open_in ".cml/config" in
    let raw = input_line ic in
    let split = (String.index raw ' ') + 1 in
    close_in ic; String.sub raw split (String.length raw - split)
  with
  | Sys_error _ -> raise (Fatal "username not set, set with [cml --user <name>]")

(* set the user info (username) *)
let set_user_info (name : string) : unit =
  let oc = open_out ".cml/config" in
  output_string oc ("user: "^name^"\n"); close_out oc

******************************* Utils Module *******************************
****************************************************************************
 returns whether the given argument is a flag (if arg is of the form
 * dash [-] followed by any number of characters > 0) 
 fetch the user info (username) 
 set the user info (username) 
open Unix
open Universal


  returns true if the path contains an initialized  repo , 
  * otherwise returns false
 * otherwise returns false *)
let cml_initialized (path : string) : bool =
  Sys.file_exists (path^".cml")

  returns true if the current directory ( or parent ) is an initialized  repo , 
  * otherwise returns false
 * otherwise returns false *)
let cml_initialized_r (path : string) : bool =
  try match Filesystem.cml_path path with
    | Some _ -> true
    | None -> false
  with
  | Fatal _ -> false

let arg_is_flag (arg : string) : bool =
  let r = Str.regexp "^-.*" in
  Str.string_match r arg 0

  precondition : [ arg ] is a flag . 
  * postcondition : returns the list of flags from the argument . 
  * example : [ get_flags_from_arg "  " ~ [ " h " ; " i " ] ] 
  * example : [ get_flags_from_arg " --hi " ~ [ " hi " ] ]
 * postcondition: returns the list of flags from the argument.
 * example: [get_flags_from_arg "-hi" ~ ["h"; "i"]]
 * example: [get_flags_from_arg "--hi" ~ ["hi"]] *)
let get_flags_from_arg (arg : string) : string list =
  let r_double_dash = Str.regexp "^--" in
  let r_single_dash = Str.regexp "^-" in
  if Str.string_match r_double_dash arg 0 then
    [Str.replace_first r_double_dash "" arg]
  else
    Str.(replace_first r_single_dash "" arg |> split (Str.regexp ""))

let get_user_info () : string =
  try
    let ic = open_in ".cml/config" in
    let raw = input_line ic in
    let split = (String.index raw ' ') + 1 in
    close_in ic; String.sub raw split (String.length raw - split)
  with
  | Sys_error _ -> raise (Fatal "username not set, set with [cml --user <name>]")

let set_user_info (name : string) : unit =
  let oc = open_out ".cml/config" in
  output_string oc ("user: "^name^"\n"); close_out oc
