#!/bin/bash -eE
                  #// Content-Type: text/plain; charset=utf-8
                  #// -eE オプションは、エラーが起きたら中断します
                  #// -x オプションは、１行ずつコマンドを表示します

#// 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" "ヘルプを開く (HTML5+SVG 対応ブラウザ Chrome, Safari で見えます) [Help]" \
    "2" "ファイルを検索する [SearchFile]" \
    "3" "新規作成する - bashlib が使えるシェル・スクリプト・ファイル [NewSh]" \
    "4" "新規作成する - ショートハンド・プロンプトのメニュー [NewMenu]" \
    "5" "圧縮ファイルを解凍する。ファイル名を一覧する [Extract]" \
    "6" "拡張子から実行属性を設定/解除する [chmod_x]" \
    "7" "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 のみ: $g_StartInPath"
  InputPath_func  "検索するフォルダーのパス >"  \
     --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 のみ: すべてのファイル"
  Input_func  "ファイル名のフィルター 例 *.txt >" ; filter="$g_Ret"

  echo  ""
  echo  "Enter のみ: 内容は問わない"
  Input_func  "キーワード >" ; 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  "Enterのみ : カレントの $default_name"
    InputPath_func  "新しいファイルのパス >"  --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=\"すでにファイルが存在します。\"/>" >&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  "$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  "$parent_path/scriptlib フォルダーを作成しました。"
  fi

  #// guide
  name=`basename  "$path"`
  echo  "シェルから、『./$name』と入力すると起動できます。"
}


 
#//*********************************************************************
#// <<< [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  "圧縮ファイルのパス >"  --ChkFileExists ; package_path="$g_Ret"

  while true ;do

    echo  "Enterのみ : ファイル名を一覧する"
    echo  "[Ctrl]+[C] : 中断"
    InputPath_func  "解凍先フォルダーのパス >"  --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  "Enterのみ : $defaults"
  InputPath_func  "処理を行うフォルダーのパス >"  --AllowEnterOnly  --ChkFolderExists
  work_folder="$g_Ret"
  if [ "$work_folder" == "" ];then  work_folder="$defaults"  ;fi

  defaults="*.sh"
  echo  ""
  echo  "Enterのみ : $defaults"
  Input_func  "実行属性を設定するファイル（\".\"=設定しない, CSV形式）>" ; set_paths="$g_Ret"
  if [ "$set_paths" == "" ];then  set_paths="$defaults"  ;fi

  defaults="*.txt, *.html, *.svg"
  echo  ""
  echo  "Enterのみ : $defaults"
  Input_func  "実行属性を解除するファイル（\".\"=解除しない, CSV形式）>" ; 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  "エラーメッセージのテスト"
}


 






#//--- 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##*../} が見つかりません。
bashlib $g_Ver をダウンロードして scriptlib フォルダーをコピーしてください。"; exit 1 ;fi
source  "$g_BashLibPath"  #// include
CallMain_func
#//--- end of bashlib include --------------------------------------------------------
 
