#!/bin/bash
set -e -u
function tree_MapFN_Depth1Group_Cnt(){
[[ $# -lt 1 ]] && return 61
rootD=$1
local flagD="/tmp"
local dbFP=$flagD/sqlite3db_file.db
local rootDId=$(echo "$rootD" | sed 's/\//--/g')
local cmdF="$flagD/cmdF$rootDId"
local aggRootD="$flagD/aggRootD$rootDId"
local source_cmd="set -e -u; \n source /app/bash-simplify/dir_tree_group/tree_MapFN_Depth1Group_Cnt.sh"
cd $rootD
cat <(echo -e "$source_cmd" ) <(find . -type d | xargs -I@ echo "dire_list_file_mime $dbFP $rootDId $aggRootD $rootD @") | tee "$cmdF" 1>/dev/null
rm -f $dbFP
rm -fr $aggRootD
bash $cmdF
tree -I mime_ls.csv $aggRootD
echo "SELECT count(*) sqlite表中文件记录总数 FROM tab_file;" | sqlite3 --line "$dbFP"
}
function sqlite3_create_tab_import_fileNameLs() {
[[ $# -lt 4 ]] && return 63
local dbFP=$1
local mimeLsCsvFP=$2
local parentD=$3
local dirName=$4
which sqlite3 1>/dev/null || sudo apt install -y sqlite3 sqlitebrowser
sqlite3 "$dbFP" < <(echo -e """
CREATE TABLE IF NOT EXISTS tab_file (outParentD varchar(512) , parentD varchar(512), dire varchar(128), fileName varchar(128), mimeType varchar(64));
.exit
""")
sqlite3 "$dbFP" <<EOF
.mode csv
.separator ;
.headers off
.import $mimeLsCsvFP tab_file
-- SELECT count(*) FROM tab_file;
.exit
EOF
}
function dire_list_file_mime() {
[[ $# -lt 5 ]] && return 62
local dbFP=$1
local rootDId=$2
local aggRootD=$3
local rootD=$4
local dire=$5
local agg_dire="$aggRootD/$dire"
mkdir -p $agg_dire
local parentD=$(dirname $dire)
local dirName=$(basename $dire)
local outParentD=$aggRootD/$parentD
local mimeLsCsvFP="$outParentD/mime_ls.csv"
local mimeAggOutFP="$outParentD/mime_agg.txt"
local directFileCnt=\
$( cd $rootD && cd $dire && find . -maxdepth 1 -type f |wc -l ;)
[[ $directFileCnt -eq 0 ]] && return 0
( cd $rootD && cd $dire && find . -maxdepth 1 -type f | while read -r _FN ; do ( FN="${_FN/.\//}" && mimeTyp=$(FN="$FN" bash /tmp/BuszCmd_MapFileNameToX) && echo "$outParentD;$parentD;$dire;$FN;$mimeTyp" ;) ; done | tee "$mimeLsCsvFP" 1>/dev/null;)
sqlite3_create_tab_import_fileNameLs $dbFP $mimeLsCsvFP $parentD $dirName
local sql="select mimeType,count(*) from tab_file where parentD='$parentD' and dire='$dire' group by mimeType ;"
local mimeTyp_Cnt__Ls="$(sqlite3 "$dbFP" < <( echo "$sql" ) )"
mimeTyp_Cnt__Ls="$(echo "$mimeTyp_Cnt__Ls" | sed 's/\//--/g')"
mimeTyp_Cnt__Ls="$(echo "$mimeTyp_Cnt__Ls" | sed 's/|/=/g')"
echo "$mimeTyp_Cnt__Ls" | while read -r mimeTyp_Cnt ; do ( touch "$agg_dire/${mimeTyp_Cnt}" ;) ; done
}