-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcodific_JBs.sh
executable file
·96 lines (84 loc) · 3.48 KB
/
codific_JBs.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
#!/bin/bash
#
# Autor= João Batista Ribeiro
# Bugs, Agradecimentos, Críticas "construtivas"
# me envie um e-mail. Ficarei Grato!
# e-mail: [email protected]
#
# Este programa é um software livre; você pode redistribui-lo e/ou
# modifica-lo dentro dos termos da Licença Pública Geral GNU como
# publicada pela Fundação do Software Livre (FSF); na versão 2 da
# Licença, ou (na sua opinião) qualquer versão.
#
# Este programa é distribuído na esperança que possa ser útil,
# mas SEM NENHUMA GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a
# qualquer MERCADO ou APLICAÇÃO EM PARTICULAR.
#
# Veja a Licença Pública Geral GNU para mais detalhes.
# Você deve ter recebido uma cópia da Licença Pública Geral GNU
# junto com este programa, se não, escreva para a Fundação do Software
#
# Livre(FSF) Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Script: Convert text UTF-8 to ISO-8859-1 (ISO Latin 1) and others
#
# Last update: 19/06/2023
#
help() {
echo -e "Use the file name (with extension) that want to convert"
echo -e "Example: $(basename "$0") file.srt\n"
exit 0
}
if [ "$#" -lt 1 ]; then # Check if has passed the file name
echo -e "\n$(basename "$0"): Error - need the pass file name\n"
help
fi
case $1 in # case to call help
'--help' | '-h')
help
esac
fileName=$1
if [ ! -e "$fileName" ]; then
echo -e "\nThe file \"$fileName\" don't exist\nTry \"$(basename "$0") -h\" or with another file\n"
exit 1
fi
fileName2Tmp=$(echo "$fileName" | rev | cut -d '.' -f2- | rev) # File name without extension
extension=$(echo "$fileName" | rev | cut -d '.' -f1 | rev) # Extension of the file
codification=$(file "$fileName") # Get the codification of the file
if echo "$codification" | grep -q "ISO-8859"; then # Check if codification is iso-8859
codStart="iso-8859"
codEnd="utf-8"
fileName2="${fileName2Tmp}_${codEnd}.$extension"
iconv -f iso-8859-1 -t utf-8//TRANSLIT "$fileName" > "$fileName2" # Convert file codification to utf-8 and save in another file
elif echo "$codification" | grep -q "UTF-8"; then # Check if codification is utf8
codStart="utf-8"
codEnd="iso-8859"
fileName2="${fileName2Tmp}_${codEnd}.$extension"
iconv -f utf-8 -t iso-8859-1//TRANSLIT "$fileName" > "$fileName2" # Convert file codification to iso-8859 and save in another file
elif echo "$codification" | grep -q "Non-ISO extended-ASCII text"; then # Check if codification is "Non-ISO extended-ASCII text"
codStart="Non-ISO extended-ASCII text - maybe iso-8859-1"
codEnd="utf-8"
fileName2="${fileName2Tmp}_${codEnd}.$extension"
iconv -f iso-8859-1 -t utf-8//TRANSLIT "$fileName" > "$fileName2" # Convert file codification to utf-8 and save in another file
else # In last case, if not one of the cod bellow, the script ends with error
echo -e "\n codification unknown\n The file was not converted\n"
exit 1
fi
if [ "$?" -eq 1 ]; then
echo -e "Error in the run of iconv\nTry $(basename "$0") -h"
exit 1
else
echo -e "\n## File has been converted with success ##\n\"$fileName\" from $codStart to $codEnd"
echo "\"$fileName\" -> \"$fileName2\""
echo -en "\nOverwrite the original file?\n(y)es, (n)o (hit enter to no): "
answerOver=$2
if [ "$answerOver" == '' ]; then
read -r answerOver
fi
if [ "$answerOver" == 'y' ]; then
mv -v "$fileName2" "$fileName"
echo -e "\nThe file has been overwrite\n"
else
echo -e "\nThe file has not been overwrite\n"
fi
fi