<commit_before>
use std::thread;
use std::sync::{Arc, Mutex};
use std::fs::File;
use std::path::Path;
use std::env;
use std::io::Read;
use std::io::Write;
use std::io::{Error,ErrorKind};
use std::string::*;
use std::collections::BTreeMap;
use std::fmt::format;

use std::net::UdpSocket;

use touchpage::startserver;
use touchpage as tp;
use touchpage::control_updates as cu;
extern crate touchpage;

#[macro_use]
mod tryopt;
mod stringerror;

extern crate tinyosc;
use tinyosc as osc;

extern crate serde_json;
use serde_json::Value;

fn load_string(file_name: &str) -> Result<String, Box<std::error::Error> >
{
  let path = &Path::new(&file_name);
  let mut inf = try!(File::open(path));
  let mut result = String::new();
  try!(inf.read_to_string(&mut result));
  Ok(result)
}

fn write_string(text: &str, file_name: &str) -> Result<(), Box<std::error::Error> >
{
  let path = &Path::new(&file_name);
  let mut inf = try!(File::create(path));
  match inf.write(text.as_bytes()) { 
    Ok(_) => Ok(()),
    Err(e) => Err(Box::new(e)),
    }
}
      
fn make_default_prefs(guifile: &str) -> BTreeMap<String, Value>
{
  let mut map = BTreeMap::new();
  map.insert(String::from("guifile"), Value::String(guifile.to_string()));
  map.insert(String::from("ip"), Value::String("0.0.0.0".to_string()));
  map.insert(String::from("http-port"), Value::String("3030".to_string()));
  map.insert(String::from("websockets-port"), Value::String("1234".to_string()));
  map.insert(String::from("oscrecvip"), Value::String("localhost:9000".to_string()));
  map.insert(String::from("oscsendip"), Value::String("127.0.0.1:7070".to_string()));
  
  map
}   

fn main() {
  // read in the settings json.
  let args = env::args();
  let mut iter = args.skip(1); // skip the program name
  match iter.next() {
    Some(file_name) => {
      if file_name == "-mkconfigs" 
      {
        let a = iter.next();
        let b = iter.next();
        match (a,b) { 
          (Some(prefs_filename), Some(gui_filename)) => {
            let value = Value::Object(make_default_prefs(&gui_filename[..]));
           
            if let Ok(sv) = serde_json::to_string_pretty(&value) {
              match write_string(&sv[..], &prefs_filename[..]) {
                Ok(()) => println!("wrote example server config to file: {}", prefs_filename),
                Err(e) => println!("error writing default config file: {:?}", e),
              }
              match write_string(touchpage::sample_gui_config(), &gui_filename[..]) {
                Ok(()) => println!("wrote example gui config to file: {}", gui_filename),
                Err(e) => println!("error writing default config file: {:?}", e),
              }
            }
          }<commit_msg>move sample gui to json::<commit_after>
use std::thread;
use std::sync::{Arc, Mutex};
use std::fs::File;
use std::path::Path;
use std::env;
use std::io::Read;
use std::io::Write;
use std::io::{Error,ErrorKind};
use std::string::*;
use std::collections::BTreeMap;
use std::fmt::format;

use std::net::UdpSocket;

use touchpage::startserver;
use touchpage as tp;
use touchpage::control_updates as cu;
extern crate touchpage;

#[macro_use]
mod tryopt;
mod stringerror;

extern crate tinyosc;
use tinyosc as osc;

extern crate serde_json;
use serde_json::Value;

fn load_string(file_name: &str) -> Result<String, Box<std::error::Error> >
{
  let path = &Path::new(&file_name);
  let mut inf = try!(File::open(path));
  let mut result = String::new();
  try!(inf.read_to_string(&mut result));
  Ok(result)
}

fn write_string(text: &str, file_name: &str) -> Result<(), Box<std::error::Error> >
{
  let path = &Path::new(&file_name);
  let mut inf = try!(File::create(path));
  match inf.write(text.as_bytes()) { 
    Ok(_) => Ok(()),
    Err(e) => Err(Box::new(e)),
    }
}
      
fn make_default_prefs(guifile: &str) -> BTreeMap<String, Value>
{
  let mut map = BTreeMap::new();
  map.insert(String::from("guifile"), Value::String(guifile.to_string()));
  map.insert(String::from("ip"), Value::String("0.0.0.0".to_string()));
  map.insert(String::from("http-port"), Value::String("3030".to_string()));
  map.insert(String::from("websockets-port"), Value::String("1234".to_string()));
  map.insert(String::from("oscrecvip"), Value::String("localhost:9000".to_string()));
  map.insert(String::from("oscsendip"), Value::String("127.0.0.1:7070".to_string()));
  
  map
}   

fn main() {
  // read in the settings json.
  let args = env::args();
  let mut iter = args.skip(1); // skip the program name
  match iter.next() {
    Some(file_name) => {
      if file_name == "-mkconfigs" 
      {
        let a = iter.next();
        let b = iter.next();
        match (a,b) { 
          (Some(prefs_filename), Some(gui_filename)) => {
            let value = Value::Object(make_default_prefs(&gui_filename[..]));
           
            if let Ok(sv) = serde_json::to_string_pretty(&value) {
              match write_string(&sv[..], &prefs_filename[..]) {
                Ok(()) => println!("wrote example server config to file: {}", prefs_filename),
                Err(e) => println!("error writing default config file: {:?}", e),
              }
              match write_string(touchpage::json::sample_gui_config(), &gui_filename[..]) {
                Ok(()) => println!("wrote example gui config to file: {}", gui_filename),
                Err(e) => println!("error writing default config file: {:?}", e),
              }
            }
          }
{'fraction_non_alphanumeric': 0.19173069886571534, 'fraction_numerical': 0.009879253567508232, 'mean_word_length': 3.5825649622799665, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4364797', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1855, 'n_words': 392}