fd.c: Introduce para_chdir().
[paraslash.git] / convert_0.2-0.3.sh
1 #!/bin/bash
2
3 #-------------------------------------------------------------------------------
4 ## Script to convert the database of paraslash 0.2.x to version 0.3.x.
5 ##
6 ## Assumptions:
7 ##      - para_server 0.2.x is running and the mysql selector is active
8 ##      - para_server 0.3.x is running on another port
9 ##      - The database of paraslash 0.3.x has been initialized (i.e.
10 ##        para_client init has successfully been executed)
11 ##      - All audio files in the mysql database of paraslash 0.2.x. have
12 ##        already been added to the 0.3.x database (execute para_client add
13 ##        /my/audio/file/dir to do that)
14 ##
15 ## The script converts the attribute table, the set attributes for each audio
16 ## file, the image table and all image ids, and finally the lastplayed and the
17 ## numplayed data.
18 ##
19 ## However, it  does not convert the paraslash stream definitions from 0.2.x to
20 ## the moods of 0.3.x. You'll have to do this by hand.
21 #-------------------------------------------------------------------------------
22
23 # Call this script without arguments to see usage info
24
25 # How to connect to para_server 0.2.x.
26 client02=/usr/local/bin/para_client
27 port02=2990
28 host02=localhost
29
30 # How to connect to para_server 0.3.x.
31 client03=./para_client
32 port03=2990
33 host03=localhost
34
35 # Unset this to deactivate messages
36 debug=1
37
38
39 client02_cmd="$client02 -p $port02 -i $host02"
40 client03_cmd="$client03 -p $port03 -i $host03"
41
42 info_log()
43 {
44         if test $debug -eq 1; then
45                 echo "$@"
46         fi
47 }
48
49 exec_client02_cmd()
50 {
51         info_log "$client02_cmd -- $@"
52         result="$($client02_cmd -- "$@")"
53 }
54
55 exec_client03_cmd()
56 {
57         info_log "$client03_cmd -- $@"
58         result="$($client03_cmd -- "$@")"
59 }
60
61 convert_attribute_table()
62 {
63         local atts
64         exec_client02_cmd laa
65         atts="$result"
66         info_log "creating attributes: $atts"
67         exec_client03_cmd addatt $atts
68 }
69
70 convert_attributes()
71 {
72         local att atts current_atts cmd query="select dir.dir, dir.name"
73         exec_client02_cmd laa name dir
74         atts="$result"
75         for att in $atts; do
76                 query="$query, data.$att"
77         done
78         query="$query from dir,data where dir.name=data.name"
79         exec_client02_cmd verb "$query"
80         echo "$result" | while read dir name current_atts; do
81                 cmd="setatt "
82                 for att in $atts; do
83                         if test "${current_atts#0}" = "$current_atts"; then
84                                 cmd="$cmd $att+"
85                                 current_atts=${current_atts#1   }
86                         else
87                                 current_atts=${current_atts#0   }
88                         fi
89                 done
90                 if test "$cmd" = "setatt "; then
91                         continue
92                 fi
93                 exec_client03_cmd $cmd "$dir/$name"
94         done
95 }
96
97 convert_lastplayed_numplayed()
98 {
99         local query="select dir.dir, dir.name, unix_timestamp(data.lastplayed), data.numplayed from dir,data where data.name=dir.name"
100         local lp np data dir name
101         exec_client02_cmd verb "$query"
102         data="$result"
103         echo "$result" | while read dir name lp np; do
104                 cmd="touch -n$np -l$lp $dir/$name"
105                 exec_client03_cmd $cmd
106         done
107 }
108
109 convert_image_table()
110 {
111         local num size name
112         exec_client02_cmd piclist;
113         echo "$result" | while read num size name; do
114                 info_log "converting $name"
115                 $client02_cmd -- pic "#$num" | $client03_cmd -- addimg "$name"
116         done
117 }
118
119 convert_image_ids()
120 {
121         local query="select dir.dir, dir.name, pics.name from dir,data,pics where data.name=dir.name and pics.id=data.pic_id"
122         local img_ids_03 dir name img id
123         exec_client03_cmd lsimg -l
124         img_ids_03="$result"
125         exec_client02_cmd verb "$query"
126         echo "$result" | while read dir name img; do
127                 id="$(echo "$img_ids_03" | grep "       $img\$" | cut -f 1)"
128                 exec_client03_cmd touch "-i$id" "$dir/$name"
129         done
130 }
131
132
133 usage()
134 {
135         grep '^##' $0 | sed -e 's/^## *//'
136         echo '
137 Usage: $0 command
138
139 command is one of the following:
140
141         attrbibute_table: create attributes
142         attrbibutes: convert attributes for each audio file
143         lastplayed_numplayed: convert numplayed and lastplayed
144                 data of each audio file
145         image_table: retrieve images from mysql and add them to the database
146                 of paraslash-0.3.x
147         image_ids: convert image id of each audio file.
148         all: Do all of the above.
149
150 Edit the top of the script to customize some options.
151 '
152 }
153
154 if test $# -ne 1; then
155         usage
156         exit 1
157 fi
158
159 case "$1" in
160 attrbibute_table)
161         convert_attribute_table
162         ;;
163 attrbibutes)
164         convert_attributes
165         ;;
166 lastplayed_numplayed)
167         convert_lastplayed_numplayed
168         ;;
169 image_table)
170         convert_image_table
171         ;;
172 image_ids)
173         convert_image_ids
174         ;;
175 all)
176         convert_attribute_table
177         convert_attributes
178         convert_lastplayed_numplayed
179         convert_image_table
180         convert_image_ids
181 *)
182         usage
183         exit 1
184         ;;
185 esac