(**************************************************************************)
(*                                                                        *)
     Copyright 2020 OCamlPro & Origin Labs                               
(*                                                                        *)
(*  All rights reserved. This file is distributed under the terms of the  *)
   GNU Lesser General Public License version 2.1 , with the special       
(*  exception on linking described in the file LICENSE.                   *)
(*                                                                        *)
(**************************************************************************)

open Ezcmd.TYPES
open Ez_file.V1
open EzFile.OP

let cmd_name = "share"

let cmd =
  let args = ref [] in
  let recursive = ref false in
  let share_dir = ref None in
  Arg.{
    cmd_name ;
    cmd_action = (fun () ->
        let args =
          if !recursive then
            let files = ref [] in
            let rec iter file =
              match Unix.lstat file with
              | exception _ -> ()
              | st ->
                match st.st_kind with
                | Unix.S_REG -> files := file :: !files
                | S_DIR ->
                  begin
                    (* Since opam-bin share will be used on .opam and switches,
                       we should hardcore some directory exclusion... *)
                    match Filename.basename file with
                    | ".opam-switch"
                    | "repo"
                    | "plugins"
                    | "download-cache"
                    | "log"
                    | "var" -> ()
                    | _ ->
                      EzFile.iter_dir ~f:(fun path ->
                          iter ( file // path )
                        ) file
                  end
                | _ -> ()
            in
            List.iter iter ( List.rev !args );
            !files
          else !args
        in
        let share_dir = !share_dir in
        Share.files ?share_dir args) ;
    cmd_args = [
      [ "r" ; "rec" ], Arg.Set recursive,
      Ezcmd.info "Iter on directories";

      [ "share-dir" ], Arg.String (fun dir -> share_dir := Some dir),
      Ezcmd.info "Global directory of shared files" ;

      [], Anons (fun list -> args := list),
      Ezcmd.info "args"
    ];
    cmd_man = [];
    cmd_doc = "Share the following files";
  }

************************************************************************
                                                                        
                                                                        
  All rights reserved. This file is distributed under the terms of the  
  exception on linking described in the file LICENSE.                   
                                                                        
************************************************************************
 Since opam-bin share will be used on .opam and switches,
                       we should hardcore some directory exclusion... 
     Copyright 2020 OCamlPro & Origin Labs                               
   GNU Lesser General Public License version 2.1 , with the special       

open Ezcmd.TYPES
open Ez_file.V1
open EzFile.OP

let cmd_name = "share"

let cmd =
  let args = ref [] in
  let recursive = ref false in
  let share_dir = ref None in
  Arg.{
    cmd_name ;
    cmd_action = (fun () ->
        let args =
          if !recursive then
            let files = ref [] in
            let rec iter file =
              match Unix.lstat file with
              | exception _ -> ()
              | st ->
                match st.st_kind with
                | Unix.S_REG -> files := file :: !files
                | S_DIR ->
                  begin
                    match Filename.basename file with
                    | ".opam-switch"
                    | "repo"
                    | "plugins"
                    | "download-cache"
                    | "log"
                    | "var" -> ()
                    | _ ->
                      EzFile.iter_dir ~f:(fun path ->
                          iter ( file // path )
                        ) file
                  end
                | _ -> ()
            in
            List.iter iter ( List.rev !args );
            !files
          else !args
        in
        let share_dir = !share_dir in
        Share.files ?share_dir args) ;
    cmd_args = [
      [ "r" ; "rec" ], Arg.Set recursive,
      Ezcmd.info "Iter on directories";

      [ "share-dir" ], Arg.String (fun dir -> share_dir := Some dir),
      Ezcmd.info "Global directory of shared files" ;

      [], Anons (fun list -> args := list),
      Ezcmd.info "args"
    ];
    cmd_man = [];
    cmd_doc = "Share the following files";
  }
