Ticket #447: migrate-aliases.sh

File migrate-aliases.sh, 2.1 KB (added by valerio, 3 years ago)

Script to migrate aliases, we could base ours on that one! ( Original post)

Line 
1    #!/bin/sh
2
3    # extract from the kmessrc file (the ${2}) the contact and the associated nicks
4    #       from the user "${1}"
5    # example use: get_names_nick zeds_back@hotmail.com ~/.kde/share/config/kmessrc
6    # return: a "$contact -> $nick" stream
7    get_names_nick() {
8        grep "\[Contact_${1}/" "${2}" -A 1 | sed '{
9            N
10            s|[^/]*/\([^]]*\)].*alternativeName=\(.*\)|\1 -> \2|g
11            N
12        }' | grep -v -- '--'
13    }
14
15    # overwrite for the contact ${1} the nick ${2}, reading on the contactlist ${3}
16    # example use: set_new_nick vito_pasculli@libero.it "Vito Pasculli"
17    #       ~/.kde4/share/apps/kmess/zeds_back\@hotmail.com/contactlist
18    # return: a stream containg the contactlist, with the nick "Vito Pasculli"
19    #       associated to the contact vito_pasculli@libero.it
20    set_new_nick() {
21        sed "/\[Contacts]\[${1}]\[Extension]/ {
22            N
23            s/alternativeName=.*/alternativeName=${2}/
24            N
25            N
26            N
27            N
28            N
29            N
30            N
31            N
32            N
33            s/useAlternativeName=.*/useAlternativeName=true/
34        }" "${3}"
35    }
36
37    # main script :)
38    # fondamentally: for each "$contact" -> "$nick" extracted from the kmessrc, set
39    #       the nick into the contactlist
40    # ${1} is the kmessrc file
41    # ${2} is the contactlist file
42    # ${3} is the user nick
43    # example use: main ~/.kde/share/config/kmessrc
44    #       ~/.kde4/share/apps/kmess/zeds_back\@hotmail.com/contactlist
45    #       zeds_back@hotmail.com
46    # return: a stream representing the new contactlist
47    main() {
48        tmpfile="$(mktemp)"
49        cp "${2}" "${tmpfile}"
50        get_names_nick "${3}" "${1}" | while read row; do
51            contact="${row/\ ->\ */}"
52            nick="${row/*\ ->\ /}"
53            cp "${tmpfile}" "${tmpfile}~"
54            set_new_nick "${contact}" "${nick}" "${tmpfile}~" > "${tmpfile}"
55        done
56        cat "${tmpfile}"
57        rm "${tmpfile}"
58    }
59
60    # the only 2 executed lines :P
61    [ -z "${3}" ] && echo "USAGE: ${0} kmessrcfile contactlistfile user_nick" && exit -1
62    main "${1}" "${2}" "${3}"
63
64