PageRenderTime 1394ms CodeModel.GetById 24ms RepoModel.GetById 9ms app.codeStats 0ms

/brlcad/tags/rel-7-16-2/sh/make_dmg.sh

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git
Shell | 457 lines | 354 code | 44 blank | 59 comment | 62 complexity | b74e34856d3107484c136b0e7a590911 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, Apache-2.0, AGPL-3.0, LGPL-3.0, GPL-3.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, 0BSD, BSD-3-Clause
  1. #!/bin/sh
  2. # M A K E _ D M G . S H
  3. # BRL-CAD
  4. #
  5. # Copyright (c) 2005-2009 United States Government as represented by
  6. # the U.S. Army Research Laboratory.
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions
  10. # are met:
  11. #
  12. # 1. Redistributions of source code must retain the above copyright
  13. # notice, this list of conditions and the following disclaimer.
  14. #
  15. # 2. Redistributions in binary form must reproduce the above
  16. # copyright notice, this list of conditions and the following
  17. # disclaimer in the documentation and/or other materials provided
  18. # with the distribution.
  19. #
  20. # 3. The name of the author may not be used to endorse or promote
  21. # products derived from this software without specific prior written
  22. # permission.
  23. #
  24. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  25. # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  28. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  30. # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  32. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. #
  36. ###
  37. #
  38. # Script for generating a Mac OS X disk mounting image (.dmg) from a
  39. # clean installation. The script should generate a .dmg that works on
  40. # 10.2+ but may require a recent version of Mac OS X to successfully
  41. # generate the disk mounting image.
  42. #
  43. # Author: Christopher Sean Morrison
  44. #
  45. ######################################################################
  46. NAME="$1"
  47. VERSION="$2"
  48. if [ "x$NAME" = "x" ] ; then
  49. echo "Usage: $0 title version [background] [contents ...]"
  50. echo "ERROR: must specify a title for the package name"
  51. exit 1
  52. fi
  53. if [ "x$VERSION" = "x" ] ; then
  54. echo "Usage: $0 title version [background] [contents ...]"
  55. echo "ERROR: must specify a version"
  56. exit 1
  57. fi
  58. shift 2
  59. OPENUP=`dirname $0`/../misc/macosx/openUp
  60. PATH=/bin:/usr/bin:/usr/sbin
  61. LC_ALL=C
  62. umask 002
  63. # TMPDIR=/tmp
  64. if [ "x$TMPDIR" = "x" ] || [ ! -w $TMPDIR ] ; then
  65. if [ -w /usr/tmp ] ; then
  66. TMPDIR=/usr/tmp
  67. elif [ -w /var/tmp ] ; then
  68. TMPDIR=/var/tmp
  69. elif [ -w /tmp ] ; then
  70. TMPDIR=/tmp
  71. else
  72. TMPDIR=.
  73. fi
  74. fi
  75. DMG_NAME="${NAME}_${VERSION}"
  76. DMG="${DMG_NAME}.dmg"
  77. if [ -d "$DMG" ] ; then
  78. echo "ERROR: there is a directory of same name in the way of creating $DMG"
  79. exit 1
  80. fi
  81. if [ -f "$DMG" ] ; then
  82. remove=""
  83. while [ "x$remove" = "x" ] ; do
  84. echo "WARNING: the disk mounting image ($DMG) already exists, remove it?"
  85. echo -n "yes/no? "
  86. read remove
  87. case x$remove in
  88. x[yY][eE][sS])
  89. remove=yes
  90. ;;
  91. x[yY])
  92. remove=yes
  93. ;;
  94. x[nN][oO])
  95. remove=no
  96. ;;
  97. x[nN])
  98. remove=no
  99. ;;
  100. *)
  101. remove=""
  102. ;;
  103. esac
  104. done
  105. if [ "x$remove" = "xyes" ] ; then
  106. rm -f "$DMG"
  107. fi
  108. if [ -f "$DMG" ] ; then
  109. echo "ERROR: cannot continue with $DMG in the way"
  110. exit 1
  111. fi
  112. fi
  113. if [ -f "${DMG}.sparseimage" ] ; then
  114. rm -f "${DMG}.sparseimage"
  115. if [ ! "x$?" = "x0" ] ; then
  116. echo "ERROR: unable to successfully remove the previous ${DMG}.sparseimage"
  117. exit 1
  118. fi
  119. if [ -f "${DMG}.sparseimage" ] ; then
  120. echo "ERROR: ${DMG}.sparseimage is in the way"
  121. exit 1
  122. fi
  123. fi
  124. PKG="${DMG_NAME}.pkg"
  125. if [ -d "$PKG" ] ; then
  126. :
  127. elif [ -d "/tmp/$PKG" ] ; then
  128. PKG="/tmp/${DMG_NAME}.pkg"
  129. fi
  130. # calculate capacity required
  131. DMG_CAPACITY=50
  132. ARGS=""
  133. while [ ! "x$*" = "x" ] ; do
  134. ARG="$1"
  135. shift
  136. size="`du -ks $ARG | awk '{print $1}'`"
  137. size="`echo \"$size 1024 / p\" | dc`"
  138. DMG_CAPACITY="`expr $DMG_CAPACITY + $size`"
  139. ARGS="$ARGS $ARG"
  140. done
  141. pkg_size="`du -ks $PKG | awk '{print $1}'`"
  142. DMG_CAPACITY="`expr $DMG_CAPACITY + $pkg_size`"
  143. hdiutil create "$DMG" -megabytes $DMG_CAPACITY -layout NONE -type SPARSE -volname $DMG_NAME
  144. if [ ! "x$?" = "x0" ] ; then
  145. echo "ERROR: hdiutil failed to complete successfully"
  146. exit 1
  147. fi
  148. if [ ! -f "${DMG}.sparseimage" ] ; then
  149. echo "ERROR: hdiutil failed to create ${DMG}.sparseimage"
  150. exit 1
  151. fi
  152. hdidDisk=`hdid -nomount "${DMG}.sparseimage" | head -n 1 | grep '/dev/disk[0-9]*' | awk '{print $1}'`
  153. if [ ! "x$?" = "x0" ] ; then
  154. echo "ERROR: unable to successfully get the hdid device name"
  155. exit 1
  156. fi
  157. if [ "x$hdidDisk" = "x" ] ; then
  158. echo "ERROR: unable to get the hdid device name"
  159. exit 1
  160. fi
  161. /sbin/newfs_hfs -v ${DMG_NAME} -b 4096 $hdidDisk
  162. if [ ! "x$?" = "x0" ] ; then
  163. echo "ERROR: unable to successfully create a new hfs filesystem on $hdidDisk"
  164. exit 1
  165. fi
  166. hdiutil eject $hdidDisk
  167. if [ ! "x$?" = "x0" ] ; then
  168. echo "ERROR: unable to successfully eject $hdidDisk"
  169. exit 1
  170. fi
  171. VOL_DIR="/Volumes/${DMG_NAME}"
  172. if [ -d "$VOL_DIR" ] ; then
  173. echo "ERROR: there is already a disk mounted at $VOL_DIR"
  174. exit 1
  175. fi
  176. hdidMountedDisk=`hdid ${DMG}.sparseimage | head -n 1 | grep '/dev/disk[0-9]*' | awk '{print $1}'`
  177. if [ ! "x$?" = "x0" ] ; then
  178. echo "ERROR: unable to successfully get the mounted hdid device name"
  179. exit 1
  180. fi
  181. if [ "x$hdidMountedDisk" = "x" ] ; then
  182. echo "ERROR: unable to get the mounted hdid device name"
  183. exit 1
  184. fi
  185. timeout=20
  186. while [ $timeout -gt 0 ] ; do
  187. if [ -d "$VOL_DIR" ] ; then
  188. timeout=1
  189. fi
  190. timeout="`expr $timeout - 1`"
  191. sleep 1
  192. done
  193. if [ ! -d "$VOL_DIR" ] ; then
  194. echo "ERROR: timed out waiting for $DMG to mount"
  195. hdiutil eject $hdidMountedDisk
  196. exit 1
  197. fi
  198. if [ -d "$PKG" ] ; then
  199. if [ ! -r "$PKG" ] ; then
  200. echo "ERROR: unable to read the installer package"
  201. hdiutil eject $hdidMountedDisk
  202. exit 1
  203. fi
  204. echo "Copying $PKG to $VOL_DIR"
  205. cp -pR "$PKG" "${VOL_DIR}/."
  206. if [ ! "x$?" = "x0" ] ; then
  207. echo "ERROR: unable to successfully copy $PKG to $VOL_DIR"
  208. hdiutil eject $hdidMountedDisk
  209. exit 1
  210. fi
  211. fi
  212. found_background=no
  213. for ARG in $ARGS ; do
  214. echo "Processing $ARG"
  215. if [ ! -r "$ARG" ] ; then
  216. if [ ! -f "$ARG" ] ; then
  217. echo "ERROR: specified content ($ARG) does not exist"
  218. hdiutil eject $hdidMountedDisk
  219. exit 1
  220. fi
  221. echo "ERROR: specified content ($ARG) is not readable"
  222. hdiutil eject $hdidMountedDisk
  223. exit 1
  224. fi
  225. argname="`basename $ARG`"
  226. if [ "x$argname" = "x" ] ; then
  227. echo "ERROR: unable to determine the $ARG base name"
  228. hdiutil eject $hdidMountedDisk
  229. exit 1
  230. fi
  231. if [ -d "$ARG" ] ; then
  232. echo "Recursively copying $ARG to $VOL_DIR"
  233. cp -pR "$ARG" "${VOL_DIR}/."
  234. if [ ! "x$?" = "x0" ] ; then
  235. echo "ERROR: unable to successfully copy $ARG to $VOL_DIR"
  236. hdiutil eject $hdidMountedDisk
  237. exit 1
  238. fi
  239. if [ ! -d "${VOL_DIR}/${argname}" ] ; then
  240. echo "ERROR: $argname failed to copy to the disk image"
  241. hdiutil eject $hdidMountedDisk
  242. exit 1
  243. fi
  244. elif [ -f "$ARG" ] ; then
  245. echo "Copying $ARG to $VOL_DIR"
  246. cp -p "$ARG" "${VOL_DIR}/."
  247. if [ ! "x$?" = "x0" ] ; then
  248. echo "ERROR: unable to successfully copy $ARG to $VOL_DIR"
  249. hdiutil eject $hdidMountedDisk
  250. exit 1
  251. fi
  252. if [ ! -f "${VOL_DIR}/${argname}" ] ; then
  253. echo "ERROR: $argname failed to copy to the disk image"
  254. hdiutil eject $hdidMountedDisk
  255. exit 1
  256. fi
  257. for dstore in DS_Store .DS_Store ; do
  258. if [ "x$argname" = "x$dstore" ] ; then
  259. if [ ! -f "${VOL_DIR}/.DS_Store" ] ; then
  260. mv "${VOL_DIR}/$dstore" "${VOL_DIR}/.DS_Store"
  261. if [ ! "x$?" = "x0" ] ; then
  262. echo "ERROR: unable to successfully move $dstore to .DS_Store"
  263. hdiutil eject $hdidMountedDisk
  264. exit 1
  265. fi
  266. if [ -f "${VOL_DIR}/DS_Store" ] ; then
  267. echo "ERROR: unable to move $dstore to .DS_Store"
  268. hdiutil eject $hdidMountedDisk
  269. exit 1
  270. fi
  271. fi
  272. fi
  273. done
  274. if [ "x$found_background" = "xno" ] ; then
  275. background="`echo $argname | sed 's/^[bB][aA][cC][kK][gG][rR][oO][uU][nN][dD]\.[a-zA-Z][a-zA-Z][a-zA-Z]*$/__bg__/'`"
  276. if [ "x$background" = "x__bg__" ] ; then
  277. found_background="$argname"
  278. fi
  279. fi
  280. else
  281. echo "ERROR: $ARG does not exist as a file or directory (sanity check)"
  282. hdiutil eject $hdidMountedDisk
  283. exit 1
  284. fi
  285. done
  286. echo "Directory listing of $VOL_DIR is:"
  287. ls -la $VOL_DIR/.
  288. if [ ! "x$found_background" = "xno" ] ; then
  289. echo "You now have 60 seconds to set the background on the disk image."
  290. else
  291. echo "There is currently an assumption that there is a background file in the Applescript."
  292. echo "Aborting."
  293. hdiutil eject $hdidMountedDisk
  294. exit 1
  295. fi
  296. osascript <<EOF
  297. set oldApp to (path to frontmost application as string)
  298. tell application "Finder"
  299. activate
  300. make new Finder window
  301. set target of Finder window 1 to disk "$DMG_NAME"
  302. set imageFile to file "$found_background" of disk "$DMG_NAME"
  303. tell Finder window 1
  304. set toolbar visible to false
  305. set zoomed to false
  306. set the bounds to {0, 0, 512, 320}
  307. set the position to {10, 54}
  308. set current view to icon view
  309. tell its icon view options
  310. set icon size to 96
  311. set arrangement to arranged by kind
  312. try
  313. set background picture to imageFile
  314. end try
  315. end tell
  316. end tell
  317. tell application "System Events"
  318. keystroke "j" using command down
  319. end tell
  320. say "You now have one minute to set a background on the disk image."
  321. delay 60
  322. tell application "System Events"
  323. keystroke "j" using command down
  324. end tell
  325. update disk "$DMG_NAME"
  326. close window 1
  327. end tell
  328. tell application oldApp
  329. activate
  330. end tell
  331. EOF
  332. if [ ! "x$?" = "x0" ] ; then
  333. echo "ERROR: unable to run osascript successfully to set initial window properties"
  334. hdiutil eject $hdidMountedDisk
  335. exit 1
  336. fi
  337. if [ ! "x$found_background" = "xno" ] ; then
  338. if [ -x /Developer/Tools/SetFile ] ; then
  339. if [ -f "${VOL_DIR}/${found_background}" ] ; then
  340. /Developer/Tools/SetFile -a V "${VOL_DIR}/${found_background}"
  341. if [ ! "x$?" = "x0" ] ; then
  342. echo "ERROR: unable to successfully set $found_background invisible"
  343. hdiutil eject $hdidMountedDisk
  344. exit 1
  345. fi
  346. fi
  347. fi
  348. fi
  349. if [ -x "$OPENUP" ] ; then
  350. $OPENUP "${VOL_DIR}/."
  351. if [ ! "x$?" = "x0" ] ; then
  352. echo "ERROR: sanity check failure -- unexpected error returned from $OPENUP ($?)"
  353. hdiutil eject $hdidMountedDisk
  354. exit 1
  355. fi
  356. fi
  357. # "recreate" a new Finder window via the open command and set the
  358. # parameters again before detaching as a last-ditch effort to make the
  359. # parameters stick.
  360. open "/Volumes/$DMG_NAME"
  361. osascript <<EOF
  362. set oldApp to (path to frontmost application as string)
  363. tell application "Finder"
  364. activate
  365. tell Finder window 1
  366. set toolbar visible to false
  367. set zoomed to false
  368. set the bounds to {0, 0, 512, 320}
  369. set position to {10, 54}
  370. set current view to icon view
  371. end tell
  372. update disk "$DMG_NAME"
  373. delay 2
  374. close window 1
  375. end tell
  376. EOF
  377. if [ ! "x$?" = "x0" ] ; then
  378. echo "ERROR: unable to run osascript successfully (again) to set window properties"
  379. hdiutil eject $hdidMountedDisk
  380. exit 1
  381. fi
  382. hdiutil eject $hdidMountedDisk
  383. if [ ! "x$?" = "x0" ] ; then
  384. echo "ERROR: unable to successfully eject $hdidMountedDisk"
  385. exit 1
  386. fi
  387. # UDCO for pre 10.2
  388. hdiutil convert "${DMG}.sparseimage" -o "$DMG" -format UDZO -imagekey zlib-level=9
  389. if [ ! "x$?" = "x0" ] ; then
  390. echo "ERROR: unable to successfully compress $DMG via hdiutil convert"
  391. exit 1
  392. fi
  393. if [ ! -f "$DMG" ] ; then
  394. echo "ERROR: hdiutil failed to compress ${DMG}.sparseimage to $DMG"
  395. exit 1
  396. fi
  397. rm "${DMG}.sparseimage"
  398. if [ ! "x$?" = "x0" ] ; then
  399. echo "ERROR: unable to successfully remove the ${DMG}.sparseimage temporary"
  400. exit 1
  401. fi
  402. if [ -f "${DMG}.sparseimage" ] ; then
  403. echo "ERROR: the ${DMG}.sparseimage temporary still exists"
  404. exit 1
  405. fi
  406. # Local Variables:
  407. # mode: sh
  408. # tab-width: 8
  409. # sh-indentation: 4
  410. # sh-basic-offset: 4
  411. # indent-tabs-mode: t
  412. # End:
  413. # ex: shiftwidth=4 tabstop=8