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