iteration on the dmg packager
[sface.git] / macos / build-dmg.sh
1 #!/bin/sh
2
3 # PURPOSE
4 # package a combination of sfa+sface into a snow leopard dmg install disk
5
6 # NOTES
7 # we leverage the first similar packaging initially made by Baris Metin
8
9 # REQUIREMENTS
10 # sface-skel.dmg is the skeleton that has Qt and all the other third-party software
11 #   it is expected to be found in the local directory
12 #   should be made available on http://mirror.onelab.eu/third-party/ as well
13 # the script expects the taglevels for both sfa and sface, 
14 #   in order to retrieve the corresponding code and to label the resulting package properly
15 #
16
17 # the place to search for the  skeleton
18 SKEL_REPO=http://mirror.onelab.eu/third-party
19
20 sfa_GITPATH=git://git.onelab.eu/sfa.git@sfa-1.0-20
21 sface_GITPATH=git://git.onelab.eu/sface.git@sface-0.1-5
22 sface_GITPATH=git://git.onelab.eu/sface.git@master
23
24 # hard-wired for snow-leopard for now
25 arch=snow-leopard
26
27 # does not affect final dmg size as it's compressed
28 DISK_SIZE=100M
29
30 ########################################
31 COMMAND=$(basename $0)
32
33 # extract the tag part of a GITPATH
34 function git_url () { echo $1 | cut -d @ -f 1 ; }
35 function git_tag () { echo $1 | cut -d @ -f 2 ; }
36
37 # go in $dest and retrieve $path part of codebase based on gitpath, e.g.
38 # git_retrieve \
39 #     git://git.onelab.eu/sface.git@sface-0.1-5 
40 #     sface 
41 #     /Volumes/sface-skel-sn/sface.app/Contents/Resources/
42 # or, with the -f (-file) option:
43 # git_retrieve -f \
44 #     git://git.onelab.eu/sface.git@sface-0.1-5 
45 #     macos/appIcon.icns 
46 #     /Volumes/sface-skel-sn/sface.app/appIcon.icns 
47 function git_retrieve () {
48     filemode=""
49     case "$1" in -f|--file) filemode=true; shift;; esac
50     gitpath=$1; shift
51     path=$1; shift
52     dest=$1; shift
53
54     giturl=$(git_url $gitpath)
55     gittag=$(git_tag $gitpath)
56     if [ -z "$filemode"] ; then
57         [ -d $dest ] || mkdir -p $dest
58         pushd $dest >& /dev/null
59         git archive --remote=${giturl} ${gittag} | tar -xf - ${path}
60         popd
61     else
62         git archive --remote=${giturl} ${gittag} | tar -xOf - ${path} > ${dest}
63     fi
64 }
65
66 function die () {
67     echo "$@" "- exiting" 
68     exit 1
69 }
70
71 function main () {
72     skel_name=sface-skel-${arch}
73     skel_dmg=./${skel_name}.dmg
74     skel_mnt=/Volumes/${skel_name}
75
76     sfa_release=sfa-$(git_tag $sfa_GITPATH | sed -e s,sfa-,,)
77     sface_release=sface-$(git_tag $sface_GITPATH | sed -e s,sface-,,)
78     release=$(git_tag $sfa_GITPATH)-$(git_tag $sface_GITPATH)
79
80     img_name=${sface_release}-${sfa_release}-${arch}
81     img_dmg=./${img_name}.dmg
82     img_raw_dmg=./${img_name}.raw.dmg
83     img_mnt=/Volumes/${img_name}
84
85     # check we're clear
86     [ -f $img_dmg ] && die "output dmg $img_dmg already exists"
87     [ -f $img_raw_dmg ] && die "please clean up tmp file $img_raw_dmg"
88
89     # mount the skeleton if not yet avail.
90     if [ ! -d $skel_mnt ] ; then
91         # locate skel dmg
92         if [ ! -f $skel_dmg ] ; then
93             echo "Retrieving $skel_dmg from $SKEL_REPO"
94             curl -O $SKEL_REPO/$skel_dmg
95         fi
96         # needless to check for existence as curl will have created on anyway
97         if ! hdiutil mount $skel_dmg ; then
98             die "Could not mount $skel_dmg
99 Please check it is published at $SKEL_REPO
100 Also make sure to trash the current one before retrying"
101         fi
102     fi
103     [ -d $skel_mnt ] || die "Could not mount skeleton $skel_dmg"
104
105     # if image already mounted, it's an old run
106     [ -d $img_mnt ] && die "Image already mounted on $img_mount"
107
108     hdiutil create -size $DISK_SIZE -fs HFS+J -volname ${img_name} $img_raw_dmg
109     hdiutil attach -readwrite -mount required $img_raw_dmg 
110
111     # copy skeleton as-is
112     tar -C $skel_mnt -cf - . | tar -C $img_mnt -xf -
113     # places relative to mount point
114     app=sface.app
115     resource_path=$app/Contents/Resources
116     bin_path=$app/Contents/MacOS
117     icon_path=$app/Contents/Resources/
118     background_path=background
119     # retrieve sfa/sfa and sface/sface in the image
120     git_retrieve $sfa_GITPATH sfa $img_mnt/$resource_path/
121     git_retrieve $sface_GITPATH sface $img_mnt/$resource_path/
122     # copy binaries from sface to the bin dir
123     for bin in sface.bin sface-run; do 
124         git_retrieve --file $sface_GITPATH $bin $img_mnt/$bin_path/$bin
125     done
126     # install background and app icons
127     git_retrieve --file $sface_GITPATH macos/background.png $img_mnt/$background_path/background.png
128     git_retrieve --file $sface_GITPATH macos/appIcon.icns $img_mnt/$icon_path/appIcon.icns
129     # xxx ?
130     git_retrieve --file $sface_GITPATH macos/appIcon.icns $img_mnt/.VolumeIcon.icns
131
132     # this apparently requires xcode
133     /Developer/Tools/SetFile -a V $img_mnt/$background_path
134     # clean up just in case
135     find $img_mnt -name '*pyc' | xargs rm -f
136     find $img_mnt -name '*~' | xargs rm -f
137
138     echo "Please open $img_mnt under Finder ..."
139     read _
140
141     hdiutil detach $img_mnt
142     rm -f $img_dmg
143     hdiutil convert -format UDZO -imagekey zlib-level=9 -o $img_dmg $img_raw_dmg
144     # clean up the raw image
145     rm -f $img_raw_dmg
146     # unmount the skeleton
147     hdiutil unmount $skel_mnt
148 }
149
150 main "$@"