File: gen-text-file.sh

package info (click to toggle)
wit 3.01a-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 9,440 kB
  • sloc: ansic: 109,416; pascal: 2,205; sh: 1,972; makefile: 749
file content (66 lines) | stat: -rwxr-xr-x 1,279 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env bash

dir="$1"
shift

if [[ ! -d $dir ]]
then
    echo "Directory not found: $dir" >&2
    exit 1
fi

function gen_text()
{
    local varname="$1"
    local src="$2"

    printf "\nconst char %s[] =\n{\n" "$varname"
    if [[ $varname =~ _cr$ ]]
    then
	grep -v '^~' "$src" \
	    | sed 's/\\/\\\\/g; s/"/\\"/g; s/^/  "/; s/#FF#/#\\f/; s/$/\\r\\n"/'
    else
	grep -v '^~' "$src" \
	    | sed 's/\\/\\\\/g; s/"/\\"/g; s/^/  "/; s/#FF#/#\\f/; s/$/\\n"/'
    fi
    printf "};\n\n"
}

for src in "$@"
do
    src="${src##*/}"
    name="${src%.*}"
    name="text_${name//[-.]/_}"
    cname="$( echo "SZS_${name}_INC" | awk '{print toupper($0)}' )"
    #echo "$name : $src -> $dest"

    {
	printf "\n#ifndef %s\n#define %s 1\n" "$cname" "$cname"

	if [[ -f "$dir/$src" ]]
	then
	    gen_text "$name" "$dir/$src"
	    echo "$src: $dir/$src" >"$src.d"
	elif [[ -d "$dir/$src" ]]
	then
	    printf '%s:' "$src" >"$src.d"
	    for inc in "$dir/$src"/*.inc
	    do
		name="${inc##*/}"
		name="${name%.*}"
		name="text_${name//[-.]/_}"
		gen_text "$name" "$inc"
		printf ' \\\n\t%s' "$inc" >>"$src.d"
	    done
	    printf '\n' >>"$src.d"
	else
	    printf '\n!! Text not found: %s\n' "$dir/$src"
	    exit 1
	fi

	printf "#endif // %s\n\n" "$cname"

    } >"$src"

done