Sort sql.qrc when generating, emphasize script
[quassel.git] / src / core / SQL / updateSQLResource.sh
1 #!/bin/bash
2 # Copyright (C) 2005-2016 by the Quassel Project - devel@quassel-irc.org
3 # Licensed under GNU General Public License version 2, or (at your option)
4 # version 3.
5 #
6 # Call this script whenever you move or rename the SQL files inside this
7 # folder.  It'll regenerate 'sql.qrc' for you.  If it fails, you can manually
8 # edit the file, too; just follow the pattern already in there.
9 #
10 # NOTE: In most cases you must upgrade the database schema version to make a
11 # change, which includes adding upgrade scripts and modifying existing setup
12 # queries.  See 'upgradeSchema.sh' for details.
13
14 set -u # Bash - fail on undefined variable
15
16 # Path to the directory containing this script, resolving any symlinks, etc
17 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
18
19 # Name of the current directory
20 SQL_DIR_NAME="${SCRIPT_DIR##*/}"
21 # See https://stackoverflow.com/questions/1371261/get-current-directory-name-without-full-path-in-bash-script/1371283#1371283
22
23 # SQL resource file (one directory up from this script)
24 SQL_RES_FILE="$SCRIPT_DIR/../sql.qrc"
25
26 # Add the Qt resource header, overwriting the existing file
27 cat > "$SQL_RES_FILE" <<EOF
28 <!DOCTYPE RCC><RCC version="1.0">
29 <qresource>
30 EOF
31
32 # In a subshell, change to the SQL directory so find output remains consistent,
33 # then...
34 # Find all files with a .sql ending...
35 # | ...sort the results by version
36 # | ...modify the beginning to change '.[...]' into '    <file>./SQL[...]' (add the directory path back)
37 #   ...and modify the end to add '[...]</file>'
38 # | ...append to the resource file.
39 (cd "$SCRIPT_DIR" ; find . -name "*.sql" | sort -t/ -k2,2 -k3n | sed -e "s/^./    <file>.\/$SQL_DIR_NAME/g" -e "s/$/<\/file>/g" >> "$SQL_RES_FILE" )
40 # Newer versions of sort support a "--version-sort" flag to naturally sort.
41 # Unfortunately, some supported platforms don't yet have that version.
42 # For now, "-t/ -k2,2 -k3n" does -almost- the same thing (there's a difference
43 # when sorting periods versus underscores).
44
45 # Add the Qt resource footer
46 cat >> "$SQL_RES_FILE" <<EOF
47 </qresource>
48 </RCC>
49 EOF