#!/bin/bash -eE
                  #// Content-Type: text/plain; charset=utf-8
                  #// -eE option is stop, when error was raised.
                  #// -x option is display commands one by one.

#// bashlib-prompt is provided under 3-clause BSD license.
#// Copyright (C) 2011 Sofrware Design Gallery "Sage Plaisir 21" All Rights Reserved.

function  Main_func()
{
  local  AppKey="$2"
  local  obj="g_InputCommandOpt"

  #//=== call InputCommand_func
  SetAttr_func  $obj  Class  "InputCommandOpt"
  SetAttr_func  $obj  Lead  "bashlib menu - shorthand prompt"

  SetAttr_as_AssociativeArrayName_func  $obj  CommandReplace  \
    "1" "Help" \
    "2" "SearchFile" \
    "3" "NewSh" \
    "4" "NewMenu" \
    "5" "Extract" \
    "6" "chmod_x" \
    "7" "Test" \
  \
    "Help"  "Help_sth_func" \
    "SearchFile"  "SearchFile_sth_func" \
    "NewSh" "NewSh_sth_func" \
    "NewMenu" "NewMenu_sth_func" \
    "Extract" "Extract_sth_func" \
    "chmod_x" "chmod_x_sth_func" \
    "Test" "Test_sth_func" \

  SetAttr_as_AssociativeArrayName_func  $obj  MenuCaption  \
    "1" "Open Japanese help (for HTML5+SVG browser, Chrome, Safari) [Help]" \
    "2" "Search file [SearchFile]" \
    "3" "New - Shell script file for bashlib [NewSh]" \
    "4" "New - Menu with shorthand prompt [NewMenu]" \
    "5" "Extract compressed file. List up file names [Extract]" \
    "6" "Set or Unset execute attribute by file extension [chmod_x]" \
    "7" "Run Automated Testing of bashlib [Test]" \

  InputCommand_func  $obj  ""  "$1"  "$AppKey"
}


if [ "${BASH_VERSINFO[0]}" -ge "4" ];then
  declare_AssociativeArrayClass="declare -A"
else
  declare_AssociativeArrayClass="declare"
fi

export  g_AssociativeArrayMaxLength=100
$declare_AssociativeArrayClass  g_InputCommandOpt


 
#//*********************************************************************
#// <<< [Help_sth_func] >>> 
#//*********************************************************************
function  Help_sth_func()
{
  local  abs_path ; GetAbsPath_func  "../document/bashlib.html" ; abs_path="$g_Ret"
  local  commands
  local  apps

  apps=( "Google Chrome"  "Safari" )
  GetUsableApplicationsForMac_func  commands  "${apps[@]}"  #//[out] commands
  if [ "${commands[0]}" != "" ];then
    echo  ${commands[0]}  "file://$abs_path"
    eval  ${commands[0]}  "file://$abs_path" &
    sleep  1
    return  0
  fi

  apps=( "google-chrome"  "firefox" )
  GetUsableCommands_func  commands  "${apps[@]}"  #//[out] commands
  if [ "${commands[0]}" != "" ];then
    echo  ${commands[0]}  "file://$abs_path"
    eval  ${commands[0]}  "file://$abs_path" &
    sleep  1
  else
    echo  "$abs_path"
  fi
}


 
#//*********************************************************************
#// <<< [SearchFile_sth_func] >>> 
#//*********************************************************************
function  SearchFile_sth_func()
{
  local  search_path
  local  keyword
  local  filter

  echo  ""
  echo  "Enter only: $g_StartInPath"
  InputPath_func  "Search target folder path >"  \
     --ChkFolderExists  --AllowEnterOnly ; search_path="$g_Ret"
  if [ "$search_path" == "" ];then  search_path="$g_StartInPath"  ;fi
  StringClass.cutLastOf_method  "$search_path"  "/" ; search_path="$g_Ret"

  echo  ""
  echo  "Enter only: All files"
  Input_func  "filter for file name *.txt >" ; filter="$g_Ret"

  echo  ""
  echo  "Enter only: regardless of contents"
  Input_func  "keyword >" ; keyword="$g_Ret"

  echo_line_func
  echo  "Search $keyword in $search_path/$filter"

  if [ "$keyword" == "" ];then
    if [ "$filter" == "" ];then
      find  "$search_path" -print
    else
      find  "$search_path" -name "$filter" -print
    fi
  else
    StringEscapeUtilsClass.escapeGrep_method  "$keyword" ; keyword="$g_Ret"
    if [ "$filter" == "" ];then
      grep -rni "$keyword" "$search_path"  || :  #// : ignores exit status
    else
      grep -rni --include="$filter" "$keyword" "$search_path"  || :  #// : ignores exit status
    fi
  fi
}


 
#//*********************************************************************
#// <<< [NewSh_sth_func] >>> 
#//*********************************************************************
function  NewSh_sth_func()
{
  NewShSub_sth_func  "sample/Sample.sh"  "$@"
}


function  NewShSub_sth_func()
{
  local  TemplatePath="$1"
  local  AppKey="$3"
  local  path
  local  parent_path
  local  name
  local  default_name="a.sh"

  while true; do
    echo  "Enteronly : ./$default_name"
    InputPath_func  "New file path >"  --AllowEnterOnly ; path="$g_Ret"
    if [ "$path" == "" ];then  path="$default_name"  ;fi
    GetAbsPath_func  "$path"  "$g_StartInPath" ; path="$g_Ret"
    if [ ! -e "$path" ];then  break  ;fi
    if [ -d "$path" ];then  path="$path/$default_name" ; break  ;fi
    echo  "<ERROR msg=\"Already file exists\"/>" >&2
  done ; done_func $?

  GetParentAbsPath_func  "$path"
  parent_path="$g_Ret"
  AppKeyClass.newWritable_method  "$AppKey"  "$parent_path"

  #// make <Sample.sh>
  mkdir_func  "$parent_path"
  cp  "$TemplatePath"  "$path"

  echo  "Created $path."

  #// search scriptlib folder
  while true ;do
    if [ -e "$parent_path/scriptlib" ];then  break  ;fi
    if [ "$parent_path" == "/" ];then  parent_path="" ; break ;fi
    parent_path=`dirname "$parent_path"`
  done ; done_func $?

  #// make scriptlib folder
  if [ "$parent_path" == "" ];then
    GetParentAbsPath_func  "$path"
    parent_path="$g_Ret"
    cp -Rap  "scriptlib"  "$parent_path"
    echo  "Created $parent_path/scriptlib folder."
  fi

  #// guide
  name=`basename  "$path"`
  echo  "You can start by input \"./$name\" in shell"
}


 
#//*********************************************************************
#// <<< [NewMenu_sth_func] >>> 
#//*********************************************************************
function  NewMenu_sth_func()
{
  NewShSub_sth_func  "sample/Menu.sh"  "$@"
}


 
#//*********************************************************************
#// <<< [Extract_sth_func] >>> 
#//*********************************************************************
function  Extract_sth_func()
{
  local  AppKey="$2"
  local  package_path
  local  target_path

  InputPath_func  "compressed file path >"  --ChkFileExists ; package_path="$g_Ret"

  while true ;do

    echo  "Enteronly : List up file names"
    echo  "[Ctrl]+[C] : Stop"
    InputPath_func  "Extracted folder path >"  --AllowEnterOnly ; target_path="$g_Ret"

    if [ "$target_path" == "" ];then
      echo  "$package_path | less"
      sleep  1
      ListUpIn_func  "$package_path" | less
    else
      AppKeyClass.newWritable_method  "$AppKey"  "$target_path"
      Extract_func  "$package_path"  "$target_path"
      break
    fi
  done ; done_func $?
}


 
#//*********************************************************************
#// <<< [chmod_x_sth_func] >>> 
#//*********************************************************************
function  chmod_x_sth_func()
{
  local  work_folder
  local  set_paths
  local  unset_paths
  local  defaults

  defaults="$g_StartInPath"
  echo  ""
  echo  "Enteronly : $defaults"
  InputPath_func  "Target folder path >"  --AllowEnterOnly  --ChkFolderExists
  work_folder="$g_Ret"
  if [ "$work_folder" == "" ];then  work_folder="$defaults"  ;fi

  defaults="*.sh"
  echo  ""
  echo  "Enteronly : $defaults"
  Input_func  "file to set execute attribute (\".\"=no file, CSV format)>" ; set_paths="$g_Ret"
  if [ "$set_paths" == "" ];then  set_paths="$defaults"  ;fi

  defaults="*.txt, *.html, *.svg"
  echo  ""
  echo  "Enteronly : $defaults"
  Input_func  "file to unset execute attribute (\".\"=no file, CSV format)>" ; unset_paths="$g_Ret"
  if [ "$unset_paths" == "" ];then  unset_paths="$defaults"  ;fi

  chmod_x_func  "$work_folder"  "$set_paths"  "$unset_paths"
}


 
#//*********************************************************************
#// <<< [Test_sth_func] >>> 
#//*********************************************************************
function  Test_sth_func()
{
  if [ -e "test_patch" ];then
    cp -Rap  "../test"  "."
    cp -Rap  test_patch/*  "test"
    rm -r  "test_patch"
  fi

  test/Test.sh
}


 
#//*********************************************************************
#// <<< [T_Error_sth_func] >>> 
#//*********************************************************************
function  T_Error_sth_func()
{
  Error_func  "Test of error message"
}


 






#//--- start of bashlib include ------------------------------------------------------ 

#// <<< set up bashlib and call Main_func >>> 

#// bashlib is provided under 3-clause BSD license.
#// Copyright (C) 2011 Sofrware Design Gallery "Sage Plaisir 21" All Rights Reserved.

g_BashLibPath="scriptlib/bashlib_inc.sh"; g_Ver="1.0"
g_StartInPath=`pwd`; cd "`dirname "$BASH_SOURCE"`"; g_Arguments=( "$BASH_SOURCE" "$@" )
for (( i = 0; i < 20; i ++ ));do
  if [ -e "$g_BashLibPath" ];then break ;else  g_BashLibPath="../$g_BashLibPath" ;fi ;done
if [ "$i" == "20" ];then  echo "${g_BashLibPath##*../} is not found.
Download bashlib $g_Ver and copy scriptlib folder"; exit 1 ;fi
source  "$g_BashLibPath"  #// include
CallMain_func
#//--- end of bashlib include --------------------------------------------------------
 
