-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcase-t.sh
117 lines (113 loc) · 4.24 KB
/
case-t.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/bash
#------------------------------------------------------------------------------#
# Programmed By Liz #
#------------------------------------------------------------------------------#
# filename title case
#
# 2023-11-01 rewrote title case
# originally read into array but... there's ' -._'...
# scan the string instead of using array
# regex =~ is finicky, best to define a variable and use that
# added FAT function
# 2023-11-02 fixed extension detection
# 2023-11-04 added single file command line processing
# 2023-11-05 added misc spelling corrections: case-t.dat
# roman numerals up to 20
# tag detection 'TAG:'
#---------------------------------------------------------------- initialization
hdg="case-t"
cat="$HOME/.icons/EMOJI Fav/Cat.png"
err="$HOME/.icons/EMOJI Fav/Skull And Crossbones.png"
# notify-send -i "$cat" "caja" "title case"
#----------------------------------------------------------- title case function
function fx_case-t ()
{
fst=$(findmnt -no "fstype" --target "$src") # get file system
dir=${src%/*} # directory
des="$dir/" # destination
fil=${src##*/} # filename
ext=${fil##*.} # extension
nam=${fil%.*} # name
siz=${#nam} # name length
reg="[^0-9a-zA-Z']" # regex white space detect
f=1 # ucase flag
tag=${nam%%:*} # tag detection
pos=${#tag}
if ((pos==siz))
then # no tag
pos=0
else # tag
des="$des$tag"
fi
if [[ "$nam" == "$ext" ]] # no extension
then
ext=""
else
ext=${ext,,} # -> lcase
fi
for ((i=pos;i<siz;i++)) # scan name
do
chr=${nam:$i:1} # get character
if ((f)) # case ?
then # ucase
des="$des${chr^}" # tcase
f=0
else # lcase
des="$des${chr,}"
fi
if [[ $chr =~ $reg ]] # white space detect
then
f=1
fi
done
while read lin # misc substitutions
do
rpl=${lin^^} # ucase substitution
rgxb="^$lin " # regex's
rgxe=" $lin$"
if [[ "$des" =~ $rgxb ]]
then # begin
des=${des/$lin /$rpl }
elif [[ "$des" =~ $rgxe ]]
then # end
des=${des/ $lin/ $rpl}
else # middle
des=${des// $lin / $rpl }
fi
done < /home/caltrop/.config/caja/scripts/case-t.dat
if [[ "$ext" != "" ]] # extension ?
then
des="$des.$ext"
fi
fst=$(findmnt -no "fstype" --target "$src") # fat detection
if [[ $fst == vfat ]]
then
tmp="$dir/temp"
mv "$src" "$tmp"
src="$tmp"
fi
mv -n "$src" "$des" # move/rename
}
#------------------------------------------------------------------ main program
if [[ $CAJA_SCRIPT_SELECTED_FILE_PATHS == "" ]]
then # single file
if [[ $1 == "" ]]
then
notify-send -i "$err" "ERROR!" "nothing to process"
exit 1
fi
# notify-send -i "$HOME/.icons/EMOJI Fav/Cat.png" "$hdg" "single file"
src="$1"
fx_$hdg
echo "$des"
else # scan files
# notify-send -i "$HOME/.icons/EMOJI Fav/Cat.png" "$hdg" "caja"
while read src
do
if [[ "$src" != "" ]] # ignore empty lines
then
fx_$hdg
fi
done <<< $CAJA_SCRIPT_SELECTED_FILE_PATHS
fi
#-------------------------------------------------------------------------------