Skip to content

Commit

Permalink
Merge pull request #35 from gerlero/scripts
Browse files Browse the repository at this point in the history
Add volume script and bashrc file
  • Loading branch information
gerlero authored May 15, 2022
2 parents f6c872b + a6dea60 commit 7abf8e7
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 26 deletions.
10 changes: 10 additions & 0 deletions Contents/MacOS/bashrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SCRIPT_DIR="$(\cd $(dirname ${BASH_SOURCE:-${ZSH_NAME:+$0}}) && \pwd -L)"

"$SCRIPT_DIR/volume" -quiet mount

VOLUME=`"$SCRIPT_DIR/volume" -show-prefix`

# Keep the volume directory open in this process (prevents accidental ejection)
exec {fd}<"$VOLUME"

. "$VOLUME/etc/bashrc"
19 changes: 2 additions & 17 deletions Contents/MacOS/openfoam
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#!/bin/zsh -e

VOLUME_ID="{{VOLUME_ID}}"

APP_BUNDLE="${0:A:h:h:h}"
APP_NAME="${APP_BUNDLE:t:r}"
DMG_FILE="$APP_BUNDLE/Contents/Resources/$APP_NAME.dmg"
VOLUME="/Volumes/$APP_NAME"
VOLUME_ID_FILE="$VOLUME/.vol_id"

echo "---------------------------------------------------------------------------"
echo " | "
Expand All @@ -16,19 +11,9 @@ echo " ( ) | Precompiled OpenFOAM for macOS "
echo " | {{APP_HOMEPAGE}} "
echo "---------------------------------------------------------------------------"

if [ ! -d "$VOLUME" ]
then
echo "Mounting the $APP_NAME volume..."
hdiutil attach -quiet "$DMG_FILE"
echo "You can safely eject the volume from the Finder after use."
fi
"$APP_BUNDLE/Contents/MacOS/volume" mount

if [ ! -f "$VOLUME_ID_FILE" ] || [ "$VOLUME_ID" != "$(< "$VOLUME_ID_FILE")" ]
then
echo "ERROR: Volume mounted at $VOLUME does not match this version." 1>&2
echo "Eject the volume and try again?" 1>&2
exit 1
fi
VOLUME=$("$APP_BUNDLE/Contents/MacOS/volume" -show-prefix)

# Keep the volume directory open in this process (prevents accidental ejection)
exec {fd}<"$VOLUME"
Expand Down
134 changes: 134 additions & 0 deletions Contents/MacOS/volume
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#!/bin/zsh -e

VOLUME_ID="{{VOLUME_ID}}"

APP_BUNDLE="${0:A:h:h:h}"
APP_NAME="${APP_BUNDLE:t:r}"
DMG_FILE="$APP_BUNDLE/Contents/Resources/$APP_NAME.dmg"
VOLUME="/Volumes/$APP_NAME"
VOLUME_ID_FILE="$VOLUME/.vol_id"

printHelp() {
cat<<HELP_USAGE
Usage: ${0##*/} [OPTION] <command>
commands:
mount Mount the $APP_NAME volume. Does nothing if the volume is
already mounted
eject Eject the $APP_NAME volume
options:
-quiet Do not print status messages to stdout
-force Eject or replace the mounted volume even if it is in use
and/or it does not belong to this app
-show-prefix Print the volume mount point and exit
-help Print this message and exit
Manage the $APP_NAME mountable volume (virtual disk) that contains the OpenFOAM
installation
HELP_USAGE
}

QUIET=false
FORCE=false

while [ "$#" -gt 0 ]
do
case "$1" in
-quiet)
QUIET=true
shift
;;
-force)
FORCE=true
shift
;;
-show-prefix)
echo "$VOLUME"
exit 0
;;
-h | -help)
printHelp
exit 0
;;
-*)
echo "Invalid option '$1'" 2>&1
exit 1
;;
*)
break
;;
esac
done

if [ "$#" -ne 1 ]
then
echo "A command is required" 2>&1
exit 1
fi

case "$1" in
mount)
if [ -d "$VOLUME" ]; then
if [ ! -f "$VOLUME_ID_FILE" ] || [ "$VOLUME_ID" != "$(< "$VOLUME_ID_FILE")" ]; then
if $FORCE; then
$QUIET || echo "Ejecting different volume already mounted at $VOLUME..."
if ! hdiutil detach -quiet "$VOLUME" -force; then
echo "ERROR: Failed to force eject a different volume at $VOLUME" 1>&2
exit 1
fi
else
echo "ERROR: A different volume is already mounted at $VOLUME" 1>&2
echo "Eject the volume and try again?" 1>&2
exit 1
fi
fi
$QUIET || echo "The $APP_NAME volume is already mounted."
else
$QUIET || echo "Mounting the $APP_NAME volume..."
if ! hdiutil attach -quiet "$DMG_FILE"; then
echo "ERROR: Failed to mount the $APP_NAME volume" 1>&2
exit 1
fi
if [ ! -f "$VOLUME_ID_FILE" ] || [ "$VOLUME_ID" != "$(< "$VOLUME_ID_FILE")" ]; then
echo "ERROR: A different volume is mounted at $VOLUME" 1>&2
echo "Eject the volume and try again?" 1>&2
exit 1
fi
fi
$QUIET || echo "You can safely eject the volume from the Finder after use."
exit 0
;;
eject)
if [ ! -d "$VOLUME" ]; then
$QUIET || echo "The $APP_NAME volume is not mounted."
exit 0
fi
if $FORCE; then
$QUIET || echo "Ejecting the volume at $VOLUME..."
if ! hdiutil detach -quiet "$VOLUME" -force; then
echo "ERROR: Failed to force eject the volume at $VOLUME" 1>&2
exit 1
fi
else
if [ ! -f "$VOLUME_ID_FILE" ] || [ "$VOLUME_ID" != "$(< "$VOLUME_ID_FILE")" ]; then
$QUIET || echo "A different volume is mounted at $VOLUME (use -force to eject it anyway)."
exit 0
fi
$QUIET || echo "Ejecting the $APP_NAME volume..."
if ! hdiutil detach -quiet "$VOLUME"; then
echo "ERROR: Failed to eject the $APP_NAME volume" 1>&2
echo "The volume is probably in use (use -force to override)" 1>&2
exit 1
fi
fi
$QUIET || echo "Done."
exit 0
;;
*)
"Invalid command '$1'" 2>&1
exit 1
;;
esac
28 changes: 19 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ install: $(INSTALL_DIR)/$(APP_NAME).app
VOLUME = /Volumes/$(APP_NAME)
VOLUME_ID_FILE = $(VOLUME)/.vol_id

APP_CONTENTS = build/$(APP_NAME).app/Contents/Info.plist \
build/$(APP_NAME).app/Contents/MacOS/launch \
build/$(APP_NAME).app/Contents/MacOS/openfoam \
build/$(APP_NAME).app/Contents/Resources/LICENSE \
build/$(APP_NAME).app/Contents/Resources/icon.icns \
build/$(APP_NAME).app/Contents/Resources/$(APP_NAME).dmg
APP_CONTENTS = \
build/$(APP_NAME).app/Contents/Info.plist \
build/$(APP_NAME).app/Contents/MacOS/openfoam \
build/$(APP_NAME).app/Contents/MacOS/volume \
build/$(APP_NAME).app/Contents/MacOS/launch \
build/$(APP_NAME).app/Contents/MacOS/bashrc \
build/$(APP_NAME).app/Contents/Resources/LICENSE \
build/$(APP_NAME).app/Contents/Resources/icon.icns \
build/$(APP_NAME).app/Contents/Resources/$(APP_NAME).dmg


$(INSTALL_DIR)/$(APP_NAME).app: build/$(APP_NAME).app
Expand All @@ -50,20 +53,27 @@ build/$(APP_NAME).app/Contents/Info.plist: Contents/Info.plist | build/$(APP_NAM
cp Contents/Info.plist build/$(APP_NAME).app/Contents/
sed -i '' "s|{{APP_VERSION}}|$(APP_VERSION)|g" build/$(APP_NAME).app/Contents/Info.plist

build/$(APP_NAME).app/Contents/MacOS/openfoam: build/$(APP_NAME).app/Contents/Resources/$(APP_NAME).dmg Contents/MacOS/openfoam
build/$(APP_NAME).app/Contents/MacOS/openfoam: Contents/MacOS/openfoam | build/$(APP_NAME).app/Contents/MacOS/volume
mkdir -p build/$(APP_NAME).app/Contents/MacOS/
cp Contents/MacOS/openfoam build/$(APP_NAME).app/Contents/MacOS/
sed -i '' "s|{{APP_HOMEPAGE}}|$(APP_HOMEPAGE)|g" build/$(APP_NAME).app/Contents/MacOS/openfoam
[ ! -d $(VOLUME) ] || hdiutil detach $(VOLUME)

build/$(APP_NAME).app/Contents/MacOS/volume: build/$(APP_NAME).app/Contents/Resources/$(APP_NAME).dmg Contents/MacOS/volume
mkdir -p build/$(APP_NAME).app/Contents/MacOS/
cp Contents/MacOS/volume build/$(APP_NAME).app/Contents/MacOS/
hdiutil attach build/$(APP_NAME).app/Contents/Resources/$(APP_NAME).dmg
cat $(VOLUME_ID_FILE)
sed -i '' "s|{{VOLUME_ID}}|$$(cat $(VOLUME_ID_FILE))|g" build/$(APP_NAME).app/Contents/MacOS/openfoam
sed -i '' "s|{{VOLUME_ID}}|$$(cat $(VOLUME_ID_FILE))|g" build/$(APP_NAME).app/Contents/MacOS/volume
hdiutil detach $(VOLUME)

build/$(APP_NAME).app/Contents/MacOS/launch: Contents/MacOS/launch | build/$(APP_NAME).app/Contents/MacOS/openfoam
mkdir -p build/$(APP_NAME).app/Contents/MacOS
cp Contents/MacOS/launch build/$(APP_NAME).app/Contents/MacOS/

build/$(APP_NAME).app/Contents/MacOS/bashrc: Contents/MacOS/bashrc | build/$(APP_NAME).app/Contents/MacOS/volume
mkdir -p build/$(APP_NAME).app/Contents/MacOS
cp Contents/MacOS/bashrc build/$(APP_NAME).app/Contents/MacOS/

build/$(APP_NAME).app/Contents/Resources/LICENSE: LICENSE
mkdir -p build/$(APP_NAME).app/Contents/Resources
cp LICENSE build/$(APP_NAME).app/Contents/Resources/
Expand Down

0 comments on commit 7abf8e7

Please sign in to comment.