src: Yearly copyright bump
[quassel.git] / src / core / SQL / updateSQLResource.sh
1 #!/bin/bash
2 # Copyright (C) 2005-2019 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 many 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 'README.md' 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, use C locale to sort consistently regardless of
33 # language, change to SQL directory so find output stays consistent,
34 # then...
35 # Find all files with a .sql ending...
36 # | ...sort the results by version
37 # | ...modify the beginning to change '.[...]' into '    <file>./SQL[...]' (add the directory path back)
38 #   ...and modify the end to add '[...]</file>'
39 # | ...append to the resource file.
40 (export LC_ALL="C" ; cd "$SCRIPT_DIR" ; find . -name "*.sql" | sort --field-separator=/ --key=2,2 --key=4n | sed -e "s/^./    <file>.\/$SQL_DIR_NAME/g" -e "s/$/<\/file>/g" >> "$SQL_RES_FILE" )
41 # Newer versions of sort support a "--version-sort" flag to naturally sort.
42 # Unfortunately, some supported platforms don't yet have that version.
43 # For now, "--field-separator=/ --key=2,2 --key=4n" does -almost- the same
44 # thing (there's a difference when sorting periods versus underscores).
45 #
46 # Further explanation for sort command:
47 # --field-separator=/
48 #   > Separate fields by "/"
49 # --key=2,2
50 #   > Sort by the second field, i.e. "SQLite" or "Postgres"
51 # --key=4n
52 #   > Sort by the fourth field using numeric comparison
53 #     (e.g. the "##" in "SQL/[database]/versions/##")
54
55 # Add the Qt resource footer
56 cat >> "$SQL_RES_FILE" <<EOF
57 </qresource>
58 </RCC>
59 EOF