C4 board
[xonotic/mediasource.git] / gfx / minigames / build.sh
1 #!/bin/bash
2
3 CONVERT=convert
4 SED="sed -r"
5 MKDIR="mkdir -p"
6
7 OUT_DIR=../../../data/xonotic-data.pk3dir/
8 JPEG_QUALITY=75
9 TEXTURE_SIZE=2048
10 FORMAT=tga
11
12 # Converts images into $FORMAT
13 # Synopsis: convert_image input.png output
14 # Genreates output.jpg and output_alpha.jpg
15 function convert_image()
16 {
17         local covert_flags="-layers flatten"
18         if [ "$FORMAT" = "jpg" ]
19         then
20                 covert_flags="$covert_flags -quality $JPEG_QUALITY"
21         fi
22         convert -background none "$1" $covert_flags "$2.$FORMAT"
23         if [ "$FORMAT" = "jpg" ]
24         then
25                 convert -background none "$1" $covert_flags -alpha Extract "$2_alpha.jpg"
26         fi
27 }
28
29 # Ensure that the output jpg are of the appropriate size
30 # Synopsis: scale_image output
31 # Where "output" is the same as $2 in convert_image
32 function scale_image()
33 {
34         let max_w=$TEXTURE_SIZE
35         
36         if echo -n "$1" | grep -q ".*/piece.*"
37         then
38                 let max_w/=4
39         fi
40         
41         if [ "$max_w" -gt 0 ]
42         then
43                 let img_w=$(identify -format %w "$1.$FORMAT")
44                 if [ "$img_w" -gt "$max_w" ]
45                 then
46                         local scale=$(echo "scale=10; $max_w/$img_w*100" | bc)
47                         convert "$1.$FORMAT" -resize "$scale%" "$1.$FORMAT"
48                         if [ "$FORMAT" = "jpg" ]
49                         then
50                                 convert "$1_alpha.jpg" -resize "$scale%" "$1_alpha.jpg"
51                         fi
52                 fi
53         fi
54 }
55
56 # handle_file src_file out_dir
57 function handle_file()
58 {
59         local src_file="$1"
60         local out_dir="$2"
61         local out_file="$out_dir/$src_file"
62         $MKDIR "$out_dir/$(dirname "$src_file")"
63         
64         local file_extension="$(echo "$src_file" | sed -r "s/^(.*)\.([^.]+)$/\2/")"
65         local base_file="$(echo "$out_file" | sed -r "s/^(.*)\.([^.]+)$/\1/")"
66         
67         if echo -n "$file_extension" | grep -Eq "^(png|svg|tga|xcf)$" && 
68                 [ "$file_extension" != "$FORMAT" ]
69         then
70                 out_file="$base_file.$FORMAT"
71                 if [ "$src_file" -nt "$out_file" ]
72                 then
73                         echo -e "Converting \x1b[1m$src_file\x1b[0m"
74                         convert_image "$src_file" "$base_file"
75                         scale_image "$base_file"
76                         continue
77                 fi
78         elif [ "$src_file" -nt "$out_file" ]
79         then
80                 echo -e "Copying \x1b[1m$src_file\x1b[0m"
81                 cp -T "$src_file" "$out_file"
82                 continue
83         fi
84         
85         echo -e "Skipping \x1b[1m$src_file\x1b[0m"
86 }
87
88 # Create the files for packaging
89 function generate()
90 {
91         for src_file in $(git ls-files '*/*')
92         do
93                 handle_file "$src_file" "$OUT_DIR/gfx/hud/default/minigames/"
94         done
95         
96         for src_file in $(git ls-files '../../models/sprites/minigame*')
97         do
98                 handle_file "$src_file" "$OUT_DIR/models/sprites/"
99         done
100 }
101
102 SYSTEM_NAME=$(uname)
103 if [ "$SYSTEM_NAME" = Darwin -o "$SYSTEM_NAME" = FreeBSD ]
104 then
105         SED="sed -E"
106 fi
107
108
109 $MKDIR "$OUT_DIR"
110
111
112 while [ "$1" ]
113 do
114         case $1 in
115                 help|-h|--help)
116                         echo TODO
117                         exit
118                         ;;
119                 clean)
120                         echo -e "Removing old files"
121                         [ -n "$OUT_DIR" -a -e "$OUT_DIR" ] && rm -rf "$OUT_DIR"
122                         ;;
123                 *)
124                         echo 1>&2 "Unknown option: $1"
125                         ;;
126         esac
127         shift
128 done
129
130 generate