JAL-3393 improved flexibility of script and updated doc
[jalview.git] / utils / install4j / mk_ds_store.sh
1 #!/usr/bin/env bash
2
3 set -e
4
5 if [[ $(uname) != "Darwin" ]]; then
6   echo "Must be run on a Mac"
7   exit 1
8 fi
9
10 function usage() {
11   cat <<EOH
12 Usage: $(basename "$0") --name "Your Application Name" [options]
13
14 Options:
15   --name <name>
16       The application name (without '.app' extension) (mandatory)
17   --bgimg <pic.png>
18       Path to your background image (defaults to background.png)
19   --output <outputfile-dsstore>
20       The filename of the resullting .DS_Store (defaults to Output-DS_Store)
21   -h, --help
22       This help text
23
24 EOH
25 exit 0
26 }
27
28 # defaults
29 BACKGROUNDIMG="background.png"
30 OUTPUTNAME="Output-DS_Store"
31
32 while [[ "${1:0:1}" = "-" ]]; do
33         case $1 in
34                 --name)
35                         NAME="$2"
36                         shift; shift;;
37     --bgimg)
38       BACKGROUNDIMG="$2"
39       shift; shift;;
40     --output)
41       OUTPUTNAME="$2"
42       shift; shift;;
43     -h | --help)
44                         usage;;
45   esac
46 done
47
48 if [[ -z "$NAME" ]]; then
49   echo "Must supply a --name Name argument"
50   exit 1
51 fi
52
53 WDIR=$(pwd)
54 # make a relative path absolute as we'll be cd-ing to /tmp
55 if [[ "${BACKGROUNDIMG#/}" = "${BACKGROUNDIMG}" ]]; then
56   BACKGROUNDIMG="${WDIR}/${BACKGROUNDIMG}"
57 fi
58
59 # Note that VOLNAME is only for making the DS_Store file in. The final DMG can have a different Volume name
60 # This temporary VOLNAME can be seen if you do `strings /Volume/Your DMG Name/.DS_Store` on the final .DS_Store
61 # so we set this to something relatable to the application name.
62 VOLNAME="${NAME// /_}_Installer"
63
64 APPNAME="${NAME}.app"
65
66 DMG="${VOLNAME}.dmg"
67 # We set the Applications folder link to " " in the DMG
68 APPLICATIONSFOLDERLINK=" "
69
70 VOLDIR="/Volumes/${VOLNAME}"
71 # The directory and filename this temporary volume is mounted from can be seen in `strings .DS_Store` so we move out
72 # of userspace for generic and security reasons.
73 cd /tmp
74 mkdir -p "$VOLNAME"
75 cd "$VOLNAME"
76 mkdir "$APPNAME"
77 ln -s /Applications " "
78 mkdir .background
79 mkdir .fseventsd
80 cp "$BACKGROUNDIMG" .background/background.png
81 cd /tmp
82 [ -e "$DMG" ] && rm "$DMG"
83 hdiutil create -volname "$VOLNAME" -srcfolder "$VOLNAME" -size 1m -fs HFS+ -fsargs "-c c=64,a=16,e=16" -format UDRW "$DMG"
84 open "$DMG"
85
86 sleep 1
87
88 # WINX and WINY set the position on the display that the Finder window appears
89 WINX=34
90 WINY=55
91 WINWIDTH=480
92 WINHEIGHT=300
93 # APPX and APPY are pixel position of the _centre_ of the app icon
94 APPX=133
95 APPY=124
96 # APPLICATIONSX and APPLICATIONSY are pixel position of the _centre_ of the applications folder icon
97 APPLICATIONSX=336
98 APPLICATIONSY=124
99 TEXTSIZE=14
100 ICONSIZE=76
101
102 osascript - "$VOLNAME" "$APPNAME" <<EOF
103 on run {volumeName, appName}
104         tell application "Finder"
105                 tell disk (volumeName as string)
106                         open
107                         
108                         set theXOrigin to $WINX
109                         set theYOrigin to $WINY
110                         set theWidth to $WINWIDTH
111                         set theHeight to $WINHEIGHT
112                         
113                         set theBottomRightX to (theXOrigin + theWidth)
114                         set theBottomRightY to (theYOrigin + theHeight)
115                         
116                         tell container window
117                                 set current view to icon view
118                                 set toolbar visible to false
119                                 set statusbar visible to false
120                                 set pathbar visible to false
121                                 set bounds to {theXOrigin, theYOrigin, theBottomRightX, theBottomRightY + 220}
122                                 set statusbar visible to false
123                                 set position of every item to {100, theBottomRightY + 150}
124                         end tell
125                         
126                         set opts to the icon view options of container window
127                         tell opts
128                                 set icon size to $ICONSIZE
129                                 set text size to $TEXTSIZE
130                                 set label position to bottom    
131                                 set arrangement to not arranged
132                         end tell
133                         
134                         set background picture of opts to file ".background:background.png"
135                         
136                         set position of item (appName as string) to {$APPX, $APPY}
137                         set position of item "$APPLICATIONSFOLDERLINK" to {$APPLICATIONSX, $APPLICATIONSY}
138
139                         tell container window
140                                 set bounds to {theXOrigin, theYOrigin, theBottomRightX, theBottomRightY}
141                         end tell
142                         
143                         close
144                         open
145
146                         update every item
147                 end tell
148         end tell
149 end run
150 EOF
151
152 # this 'sleep 3' isn't necessary but it gives you a chance to look at your handiwork!
153 sleep 3
154
155 DSSTORE="${VOLDIR}/.DS_Store"
156 until [ -e "$DSSTORE" ] && grep background.png "$DSSTORE"; do
157   sleep 1
158   echo "Waiting for \"$DSSTORE\" to update"
159 done
160
161 cp "$DSSTORE" "${WDIR}/${OUTPUTNAME}"
162 echo "Created new .DS_Store as \"$OUTPUTNAME\""
163
164 umount "$VOLDIR"
165 cd /tmp
166 rm "$DMG"
167 rm -r "/tmp/${VOLNAME}"
168
169 cd "$WDIR"
170