1#!/bin/bash
2
3#
4# Copyright (C) 2011 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#      http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18
19
20
21# This script queries a media provider database, and generates a script to
22# approximately recreate the same file system structure on another device,
23# using dummy files.
24
25EXTERNAL=$2
26if [ "$EXTERNAL" == "" ]
27then
28    EXTERNAL="/storage"
29fi
30
31
32if [ "$1" == "" ]
33then
34    echo "Usage: $0 <file.db> [external storage root]"
35    exit 2
36fi
37
38if [ ! -f "$1" ]
39then
40    echo "Couldn't find file $1"
41    exit 3
42fi
43
44# generate script to generate directory structure and content
45sqlite3 $1 "select format, media_type, mime_type, case when substr(_data,-1) is '\' then substr(_data,1,length(_data)-1) else _data end from files where _data like '"$EXTERNAL"/%';" | {
46
47MKDIRS=/tmp/mkdirs$$
48CPFILES=/tmp/cpfiles$$
49
50echo "# create directories" > $MKDIRS
51echo "# copy files" > $CPFILES
52
53IFS="|"
54while read format mediatype mimetype data;
55do
56    if [ "$format" == "14337" ]
57    then
58        # jpeg
59        echo "cat /storage/sdcard0/proto.jpg > \"$data\"" >> $CPFILES
60    elif [ "$format" == "14347" ]
61    then
62        # png
63        echo "cat /storage/sdcard0/proto.png > \"$data\"" >> $CPFILES
64    elif [ "$format" == "14343" -a "$mediatype" == "0" ]
65    then
66        # gif
67        echo "cat /storage/sdcard0/proto.gif > \"$data\"" >> $CPFILES
68    elif [ "$format" == "12292" -a "$mediatype" == "0" ]
69    then
70        # txt
71        echo "cat /storage/sdcard0/proto.txt > \"$data\"" >> $CPFILES
72    elif [ "$format" == "12293" -a "$mediatype" == "0" ]
73    then
74        # html
75        echo "cat /storage/sdcard0/proto.html > \"$data\"" >> $CPFILES
76    elif [ "$format" == "12297" ]
77    then
78        # mp3
79        echo "cat /storage/sdcard0/proto.mp3 > \"$data\"" >> $CPFILES
80    elif [ "$format" == "12296" ]
81    then
82        # wav
83        echo "cat /storage/sdcard0/proto.wav > \"$data\"" >> $CPFILES
84    elif [ "$format" == "12299" -a "$mediatype" == "0" ]
85    then
86        # m4v
87        echo "cat /storage/sdcard0/proto.m4v > \"$data\"" >> $CPFILES
88    elif [ "$format" == "12299" -a "$mediatype" == "3" ]
89    then
90        # mp4
91        echo "cat /storage/sdcard0/proto.m4v > \"$data\"" >> $CPFILES
92    elif [ "$format" == "12299" -a "$mediatype" == "2" ]
93    then
94        # m4a
95        echo "cat /storage/sdcard0/proto.m4a > \"$data\"" >> $CPFILES
96    elif [ "$format" == "47492" ]
97    then
98        # 3gp
99        echo "cat /storage/sdcard0/proto.3gp > \"$data\"" >> $CPFILES
100    elif [ "$format" == "47362" ]
101    then
102        # ogg
103        echo "cat /storage/sdcard0/proto.ogg > \"$data\"" >> $CPFILES
104    elif [ "$format" == "47747" ]
105    then
106        # doc
107        echo "cat /storage/sdcard0/proto.doc > \"$data\"" >> $CPFILES
108    elif [ "$format" == "12288" -a "$mediatype" == "0" ]
109    then
110        # unknown type
111        echo "cat /storage/sdcard0/proto.dat > \"$data\"" >> $CPFILES
112    elif [ "$format" == "12289" ]
113    then
114        # directory, ignore
115        true
116    elif [ "$format" == "12288" -a "$mediatype" == "4" ]
117    then
118        # playlist, ignore
119        true
120    else
121        echo ignored: $format '|' $mediatype '|' $mimetype '|' $data
122    fi
123    echo mkdir -p \"$(dirname "$data")\" >> $MKDIRS
124done
125
126sort -u $MKDIRS > mkfiles.sh
127cat $CPFILES >> mkfiles.sh
128rm -rf $MKDIRS $CPFILES
129
130}
131
132# generate playlist files
133sqlite3 $1 "select audio_playlists._data, audio._data from audio_playlists left outer join audio_playlists_map on audio_playlists._id=audio_playlists_map.playlist_id left outer join audio on audio_playlists_map.audio_id=audio._id order by audio_playlists_map.playlist_id,audio_playlists_map.play_order;" | {
134
135IFS="|"
136while read plist entry
137do
138    echo "echo \"$(basename $entry)\" >> \"$plist\"" >> mkfiles.sh
139done
140}
141
142echo mkfiles.sh generated. Now run:
143grep sdcard0\/proto mkfiles.sh |sed 's/cat \/storage\/sdcard0\//adb push protos\//' | sed 's/ > .*/ \/storage\/sdcard0\//'|sort -u
144echo adb push mkfiles.sh /storage/sdcard0
145echo adb shell sh /storage/sdcard0/mkfiles.sh
146
147