Migration script in postinst to use a shared openstreetmap tile cache.
[marble] / packaging / debian / marble.postinst
1 #!/bin/sh
2
3 set -e
4
5 if [ "$1" != "configure" ]
6 then
7   exit 0
8 fi
9
10 ### Change old default values to new default values
11
12 CONFIG_FILE="/home/user/.config/kde.org/Marble Desktop Globe.conf"
13 if test -e "${CONFIG_FILE}"
14 then
15   sed -i \
16   -e 's/^sideBar=true$/sideBar=false/' \
17   -e 's/^volatileTileCacheLimit=30$/volatileTileCacheLimit=6/' \
18   -e 's/^projection=0$/projection=2/' \
19   -e '/^distanceUnit=/d' \
20   -e 's/^persistentTileCacheLimit=300$/persistentTileCacheLimit=0/' \
21   "${CONFIG_FILE}"
22   chown user:users "${CONFIG_FILE}"
23 fi
24
25 ### Migrate cached openstreetmap tiles to shared directory
26
27 # The OSM tile download location in Marble 1.0.x and earlier
28 OSM_DIR_10="${HOME}/MyDocs/.local/share/marble/maps/earth/openstreetmap"
29
30 # The OSM tile download location in Marble 1.1 and later
31 OSM_DIR_11="${HOME}/MyDocs/.maps/OpenStreetMap I"
32
33 # The OSM theme .dgml file
34 OSM_DGML="/opt/marble/share/marble/data/maps/earth/openstreetmap/openstreetmap.dgml"
35
36 # A file used as a flag to indicate a previous migration
37 MIGRATED="${OSM_DIR_10}/.migrated"
38
39 # Verify we have the .dgml file and can write to it (required)
40 test -w "${OSM_DGML}" || { echo "DGML file ${OSM_DGML} missing."; exit 1; }
41
42 # Check whether the tiles were previously migrated. In that case there's nothing to do
43 test -e "${MIGRATED}" && exit 0
44
45 # Tile migration
46 if test -d "${OSM_DIR_10}"
47 then
48     mkdir -p "${OSM_DIR_11}"
49     # Cached tiles from an old Marble installation exist and must be migrated
50     for x in ${OSM_DIR_10}/[0-9]*
51     do
52         test -e "${x}" || continue # nullglob would be shorter, but is a bashism
53         dx="$(basename ${x})"
54         if test -e "${OSM_DIR_11}/${dx}"
55         then
56             # Tiles /x/ were downloaded both in Marble and another application
57             for y in ${x}/[0-9]*
58             do
59                 test -e "${y}" || continue
60                 dy="$(basename ${y})"
61                 target="${OSM_DIR_11}/${dx}/${dy}/"
62                 if test -e "${target}"
63                 then
64                     test -e "${z}" || continue
65                     # Tiles /x/y/ were downloaded both in Marble and another application. Check each /x/y/z
66                     for z in ${y}/[0-9]*.png
67                     do
68                         fz="$(basename ${z})"
69                         echo -n "."
70                         # Overwrite the shared tile only if the Marble one is newer
71                         mv -u "${z}" "${target}"
72                         # If the shared tile was not overwritten, delete the Marble one now
73                         test -e "${z}" && rm "${z}"
74                     done
75                 else
76                     # Target dir does not exist yet, so we can move it over (much quicker)
77                     mv "${y}" "${OSM_DIR_11}/${dx}"
78                 fi
79             done
80         else
81             # Target does not exist yet, so we can move it over (much quicker)
82             mv "${x}" "${OSM_DIR_11}/"
83         fi
84     done
85 fi
86
87 # Delete now empty directories
88 test -d "${OSM_DIR_10}/" && find "${OSM_DIR_10}/" -depth -type d -empty -delete
89
90 # If files are left in the old directory, leave a flag to avoid running 
91 # the migration again. This only happens if the user created custom files in
92 # the OSM cache directory
93 test -d "${OSM_DIR_10}/" && touch "${MIGRATED}"
94
95 # Finally, change the download location in the .dgml file. Also needed for new installations
96 sed -i "s@<sourcedir format=\"PNG\"> earth/openstreetmap </sourcedir>@<sourcedir format=\"PNG\"> ${OSM_DIR_11} </sourcedir>@" "${OSM_DGML}"
97
98 exit 0