summaryrefslogtreecommitdiffstats
path: root/lib/gajim/settings-clean
blob: c906ff6ccd9a293a683b161a173b02cc88ff6ee7 (plain)
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
#!/bin/sh

set -eu

input_to_db()
{
	local input=
	local db=

	input="$(mktemp)" || return 1
	cat - 1>"${input}" || { rm -f "${input}"; return 1; }
	if file "${input}" | grep -Fq SQLite; then
		# Input still a database: use it directly.
		db="${input}"
	else
		# Input already dumped to SQL: enter it into temporary database.
		db="$(mktemp)" || { rm -f "${input}"; return 1; }
		sqlite3 "${db}" 0<"${input}" || { rm -f "${input}"; return 1; }
		rm -f "${input}" || { rm -f "${db}"; return 1; }
	fi

	printf '%s' "${db}"
	return 0
}

clean()
{
	local db="${1}"
	shift 1

	sqlite3 "${db}" "UPDATE account_settings SET $(: \
		)settings = edit(settings, \"${0}-sub-account-settings\");" || \
		return 1

	return 0
}

db_to_sql()
{
	local db="${1}"
	shift 1

	sqlite3 "${db}" '.dump' || return 1

	return 0
}

usage()
{
	printf 'Usage: %s [<table>.<column>=<value> ...]\n' "${0}"

	return 0
}

main()
{
	local arg=
	local db=

	for arg in "${@}"; do
		case "${arg}" in
			?*.?*=?*);;
			*) usage 1>&2; return 1;;
		esac
	done

	db="$(input_to_db)" ||                  return 1
	clean "${db}"       || { rm -f "${db}"; return 1; }
	db_to_sql "${db}"   || { rm -f "${db}"; return 1; }
	rm -f "${db}"       ||                  return 1

	return 0
}

main "${@}"