07cf8011b071e556994d1fab6c02d04e6fc43508
[sface.git] / macos / build-dmg.sh
1 #!/bin/sh
2
3 # PURPOSE
4 # package a combination of sfa+sface into a dmg install disk
5 # supports leopard and snow leopard
6
7 # REQUIREMENTS
8 # sface-skel-snow-leopard<arch>.dmg is the skeleton that has Qt and all the other third-party software
9 #   it is expected to be found in the local directory
10 #   otherwise, or if -f is specified, it gets fetched 
11 #     from http://mirror.onelab.eu/third-party/ 
12 # the script expects the taglevels for both sfa and sface, 
13 #   in order to retrieve the corresponding code and to label the resulting package properly
14 #   alternatively, a build-dir and tags file can be used instead
15 # Should have the folowing tools installed
16 #   git: to retrieve code
17 #   rpm: to retrieve the version numbers in specfiles
18
19 # NOTES
20 # we initially leveraged the first similar packaging initially made by Baris Metin
21 # as of sface-0.1-9 we have bootstrapped our own skeleton images, 
22 #   i.e. you can use vn as a skeleton for vn+1
23 # about that, the way the background image gets (or not) found 
24 #   on the final system is as follows 
25 #   before compressing the image, you should open the image through the finder
26 #   and tweak it, as described in http://el-tramo.be/guides/fancy-dmg
27 #   that's the raison d'ĂȘtre for the -i option
28 # basically: open two finder windows, one on the .backgrounf dir (use cmd-shift G)
29 #   and one on the image itself; then using Finder->view->show view options
30 #   drag the background image as appropriate
31 # the important point being that the filename for the background must 
32 #   exist - and so be the same - on the target system.
33 # that's why the image 'volname' is constant across versions
34
35 # the place to search for the  skeleton
36 SKEL_REPO=http://mirror.onelab.eu/third-party
37
38 # default is snow-leopard
39 DEFAULT_ARCH=snow-leopard
40 ALL_ARCHS="leopard snow-leopard"
41
42 # does not affect final dmg size as it's compressed
43 DISK_SIZE=100M
44
45 ########################################
46 COMMAND=$(basename $0)
47
48 # extract the tag part of a GITPATH
49 function git_url () { echo $1 | cut -d @ -f 1 ; }
50 function git_tag () { echo $1 | cut -d @ -f 2 ; }
51
52 # go in $dest and retrieve $path part of codebase based on gitpath, e.g.
53 # git_retrieve \
54 #     git://git.onelab.eu/sface.git@sface-0.1-5 
55 #     sface 
56 #     /Volumes/sface-skel-sn/sface.app/Contents/Resources/
57 # or, with the -f (-file) option:
58 # git_retrieve -f \
59 #     git://git.onelab.eu/sface.git@sface-0.1-5 
60 #     macos/appIcon.icns 
61 #     /Volumes/sface-skel-sn/sface.app/appIcon.icns 
62 function git_retrieve () {
63     filemode=""
64     case "$1" in -f|--file) filemode=true; shift;; esac
65     gitpath=$1; shift
66     path=$1; shift
67     dest=$1; shift
68
69     giturl=$(git_url $gitpath)
70     gittag=$(git_tag $gitpath)
71     if [ -z "$filemode" ] ; then
72         [ -d "$dest" ] || mkdir -p "$dest"
73         pushd "$dest" >& /dev/null
74         git archive --remote=${giturl} ${gittag} | tar -xf - ${path}
75         popd >& /dev/null
76     else
77         destdir=$(dirname "$dest")
78         [ -d "$destdir" ] || mkdir -p "$destdir"
79         git archive --remote=${giturl} ${gittag} | tar -xOf - ${path} > "${dest}"
80     fi
81 }
82
83 function die () {
84     echo "$@" "- exiting" 
85     exit 1
86 }
87
88 function package () {
89     
90     sface_GITPATH=$1; shift
91     sfa_GITPATH=$1; shift
92     arch=$1; shift
93
94     skel_name=sface-skel-${arch}
95     skel_dmg=./${skel_name}.dmg
96     skel_mnt="/Volumes/${skel_name}"
97
98     sfa_release=sfa-$(git_tag $sfa_GITPATH | sed -e s,sfa-,,)
99     sface_release=sface-$(git_tag $sface_GITPATH | sed -e s,sface-,,)
100     release=$(git_tag $sfa_GITPATH)-$(git_tag $sface_GITPATH)
101
102     img_name=${sface_release}-${sfa_release}-${arch}
103     img_dmg=./${img_name}.dmg
104     img_raw_dmg=./${img_name}.raw.dmg
105     img_mnt_name="sface ($arch)"
106     img_mnt="/Volumes/${img_mnt_name}"
107
108     # check we're clear
109     [ -f $img_dmg ] && die "output dmg $img_dmg already exists"
110     [ -f $img_raw_dmg ] && die "please clean up tmp file $img_raw_dmg"
111
112     if [ -n "$FORCE_DOWNLOAD" ] ; then
113         [ -d "$skel_mnt" ] && hdiutil unmount "$skel_mnt"
114         [ -f "$skel_dmg" ] && rm -f "$skel_dmg"
115     fi
116
117     # mount the skeleton if not yet avail.
118     if [ ! -d "$skel_mnt" ] ; then
119         # locate skel dmg
120         if [ ! -f $skel_dmg ] ; then
121             echo "Retrieving $skel_dmg from $SKEL_REPO"
122             curl -O $SKEL_REPO/$skel_dmg
123         fi
124         # needless to check for existence as curl will have created on anyway
125         echo "Mounting skeleton"
126         if ! hdiutil mount -quiet -mountpoint $skel_mnt $skel_dmg ; then
127             die "Could not mount $skel_dmg
128 Please check it is published at $SKEL_REPO
129 Also make sure to trash the current one before retrying"
130         fi
131     fi
132     [ -d "$skel_mnt" ] || die "Could not mount skeleton $skel_dmg"
133
134     # if image already mounted, it's an old run
135     [ -d "$img_mnt" ] && die "Image already mounted on $img_mnt"
136
137     echo "Initializing image ..."
138     hdiutil create -quiet -size $DISK_SIZE -fs HFS -volname "${img_mnt_name}" $img_raw_dmg 
139     hdiutil attach -readwrite -mount required "$img_raw_dmg"
140     
141     # copy skeleton as-is
142     echo "Populating from  skeleton.."
143     ( cd "$skel_mnt" ; find . 2> /dev/null | cpio --quiet -c -o) | ( cd "$img_mnt" ; cpio -diu )
144
145     # places relative to mount point
146     app=sface.app
147     # xxx tmp
148     skelapp_path=$(ls -d "$skel_mnt"/*.app)
149     skelapp=$(basename "$skelapp_path")
150     if [ "$skelapp" != "$app" ] ; then
151         mv "$img_mnt/$skelapp" "$img_mnt/$app"
152     fi
153     # unmount the skeleton
154     hdiutil unmount "$skel_mnt"
155
156     # 
157     resource_path=$app/Contents/Resources
158     bin_path=$app/Contents/MacOS
159
160     # retrieve sfa/sfa and sface/sface in the image
161     rm -rf "$img_mnt"/$resource_path/{sfa,sface}
162     git_retrieve $sfa_GITPATH sfa "$img_mnt"/$resource_path/
163     # retrieve version number from specfile
164     sfa_tag=$(rpm -q --specfile "$img_mnt"/$resource_path/sfa/sfa.spec --qf '%{version}-%{taglevel}')
165     make -C "$img_mnt"/$resource_path/sfa VERSIONTAG="${sfa_tag}" SCMURL="${sfa_GITPATH}" version
166
167     git_retrieve $sface_GITPATH sface "$img_mnt"/$resource_path/
168     # retrieve version number from specfile
169     sface_tag=$(rpm -q --specfile "$img_mnt"/$resource_path/sface/sface.spec --qf '%{version}-%{taglevel}')
170     make -C "$img_mnt"/$resource_path/sface VERSIONTAG="${sface_tag}" SCMURL="${sface_GITPATH}" version
171
172     # copy binaries from sface to the bin dir
173     for bin in sface.bin sface-run; do 
174         # don't use the --file mode as this loses the executable bit
175         git_retrieve $sface_GITPATH $bin "$img_mnt"/$bin_path
176     done
177
178     ### install background and app icons
179     # clean up any 'background' dir if exists
180     rm -rf "$img_mnt"/{,.}background 
181     git_retrieve --file $sface_GITPATH macos/graphic-install-background.png "$img_mnt"/.background/background.png
182     git_retrieve --file $sface_GITPATH macos/graphic-sfa.icns "$img_mnt"/$resource_path/appIcon.icns
183     # the volume icons won't work - who cares
184 #    git_retrieve --file $sface_GITPATH macos/graphic-vol-sface.icns "$img_mnt"/.background/volumeIcon.icns
185 #    git_retrieve --file $sface_GITPATH macos/graphic-vol-sface.png "$img_mnt"/.background/volumeIcon.png
186
187     # instantiate version
188     sed -e "s,@VERSIONTAG@,$sfa_release,g" -e "s,@SCMURL@,${sfa_GITPATH},g" \
189         "$img_mnt"/$resource_path/sfa/util/version.py.in > "$img_mnt"/$resource_path/sfa/util/version.py
190
191     # clean up just in case
192     find "$img_mnt" -name '*pyc' | xargs rm -f
193     find "$img_mnt" -name '*~' | xargs rm -f
194
195     if [ -n "$INTERACTIVE" ] ; then
196         echo "Please tweak icons layout in $img_mnt under Finder, and press enter when done ..."
197         read _
198     fi
199
200     hdiutil detach "$img_mnt"
201     rm -f $img_dmg
202     echo "Compressing..."
203     hdiutil convert -quiet -format UDZO -imagekey zlib-level=9 -o $img_dmg $img_raw_dmg 
204     # clean up the raw image
205     rm -f $img_raw_dmg
206
207     echo "=================================================="
208     echo "Install image ready in $img_dmg"
209     echo "You may publish it by running e.g."
210     echo "rsync -av $img_dmg root@build.onelab.eu:/build/sface"
211     echo "=================================================="
212
213 }
214
215 ########################################
216 # either provide a build dir and tags file
217 # or sface-GITPATH sfa-GITPATH 
218 function usage () {
219     echo "$COMMAND -b <build-dir> -t <tags-file>"
220     echo " or"
221     echo "$COMMAND -c <sface-GITPATH> -s <sfa-GITPATH>"
222     echo ""
223     echo "Common options"
224     echo " -i : interactive : let you align icons properly in the image before it gets wrapped"
225     echo " -f : force download of the skeleton package"
226     echo " -a leopard|snow-leopard"
227     echo " -A : run on all known archs"
228     echo " -n : dry-run"
229     echo " -v : verbose"
230     echo " -h : this help"
231 }
232
233 DEFAULT_BUILD_DIR=$HOME/git/build
234 DEFAULT_TAGS_FILE=onelab-k32-tags.mk
235
236 function main () {
237     while getopts "b:t:c:s:ifa:Anvh" opt ; do
238         case $opt in 
239             b) BUILD_DIR=$OPTARG;;
240             t) TAGS_FILE=$OPTARG;;
241             c) sface_GITPATH=$OPTARG;;
242             s) sfa_GITPATH=$OPTARG;;
243             i) INTERACTIVE=true;;
244             f) FORCE_DOWNLOAD=true;;
245             a) ARCHS="$ARCHS $OPTARG";;
246             A) ARCHS="leopard snow-leopard";;
247             n) dry_run=true;;
248             v) set -x;;
249             h) usage; exit 1 ;;
250         esac
251     done
252     shift $(($OPTIND - 1))
253
254     [ -z "$BUILD_DIR" ] && BUILD_DIR=$DEFAULT_BUILD_DIR
255     [ -z "$TAGS_FILE" ] && TAGS_FILE=$DEFAULT_TAGS_FILE
256     [ -z "$ARCHS" ] && ARCHS=$DEFAULT_ARCH
257     [[ -n "$@" ]] && { usage ; exit 1; }
258     if [ -z "$sface_GITPATH" ] ; then
259         sface_GITPATH=$(make -C $BUILD_DIR stage1=true PLDISTROTAGS=$TAGS_FILE +sface-GITPATH 2> /dev/null)
260         echo "Retrieved from $BUILD_DIR/$TAGS_FILE.."
261         echo " sface_GITPATH=$sface_GITPATH"
262     fi
263     if [ -z "$sfa_GITPATH" ] ; then
264         sfa_GITPATH=$(make -C $BUILD_DIR stage1=true PLDISTROTAGS=$TAGS_FILE +sfa-GITPATH 2> /dev/null)
265         echo "Retrieved from $BUILD_DIR/$TAGS_FILE.."
266         echo " sfa_GITPATH=$sfa_GITPATH"
267     fi
268     for ARCH in $ARCHS; do 
269         if [ -z "$dry_run" ] ; then
270             package $sface_GITPATH $sfa_GITPATH $ARCH
271         else
272             echo "Would run package $sface_GITPATH $sfa_GITPATH $ARCH"
273         fi
274     done
275         
276 }
277
278 main "$@"