Skip to content

Commit

Permalink
Automate the file association (in the new in-place install).
Browse files Browse the repository at this point in the history
  • Loading branch information
Poikilos committed Dec 9, 2022
1 parent 5286e84 commit 9478b86
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 7 deletions.
125 changes: 125 additions & 0 deletions install-in-place.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/bin/bash
rel_exe="scripts/blnk"
if [ ! -f "$rel_exe" ]; then
>&2 echo "$rel_exe is missing. This must run from the blnk repo."
exit 1
fi
real_exe="`realpath $rel_exe`"
if [ "x$PREFIX" = "x" ]; then
PREFIX=~/.local
fi
THIS_XDG_APP_ID=org.poikilos-blnk
OLD_NAME="org.poikilos.blnk.desktop"
SC_NAME="$THIS_XDG_APP_ID.desktop"
BIN_NAME="blnk"
DISPLAY_NAME="BLnk"
MIMETYPE="application/x-blnk"
APPLICATIONS=$PREFIX/share/applications
mkdir -p $APPLICATIONS

BIN_DIR=$PREFIX/local/bin
mkdir -p "$BIN_DIR"

BIN_FILE="$BIN_DIR/$BIN_NAME"
if [ -f "$BIN_FILE" ]; then
>&2 printf "* removing old \"$BIN_FILE\"..."
rm "$BIN_FILE"
if [ $? -ne 0 ]; then
>&2 echo "FAILED"
exit 1
else
>&2 echo "OK"
fi
fi

>&2 printf "* 'ln -s \"$real_exe\" \"$BIN_FILE\"'..."
ln -s "$real_exe" "$BIN_FILE"
if [ $? -ne 0 ]; then
>&2 echo "FAILED"
exit 1
else
>&2 echo "OK"
fi

OLD_SC_FILE="$APPLICATIONS/$OLD_NAME"
SC_FILE="$APPLICATIONS/$SC_NAME"

if [ -f "$OLD_SC_FILE" ]; then
>&2 echo "* removing deprecated \"$OLD_SC_FILE\""
fi

>&2 printf "* writing \"$SC_FILE\"..."
cat > "$SC_FILE" <<END
[Desktop Entry]
Exec=$BIN_FILE
MimeType=$MIMETYPE;
NoDisplay=false
Name=$DISPLAY_NAME
Type=Application
END
if [ $? -ne 0 ]; then
>&2 echo "FAILED"
exit 1
else
>&2 echo "OK"
fi

./install-mimetype.sh

PREV_PREFIX="$PREFIX"

# mimetype.rc will use the environment's prefix if any, so blank it for now.
# FIXME: Set prefix to the one that install-mimetype used.
PREFIX=
source mimetype.rc
if [ $? -ne 0 ]; then exit 1; fi
if [ ! -f "$DONE_DEST" ]; then
echo "Error: The mimetype file (DONE_DEST='$DONE_DEST') was not installed."
exit 1
fi

PREFIX="$PREV_PREFIX"

THIS_XDG_DATA_DIR=$PREFIX/share
# MIMEAPPS_LIST=$THIS_XDG_DATA_DIR/applications/mimeapps.list
# ^ didn't work (doesn't show up in Open With menu--see
# <https://forums.linuxmint.com/viewtopic.php?t=272852>:
MIMEAPPS_LIST=~/.config/mimeapps.list

>&2 printf "Adding $MIMETYPE=$SC_NAME to $MIMEAPPS_LIST..."
if [ ! -f $MIMEAPPS_LIST ]; then
mkdir -p $PREFIX/share/applications
echo "$MIMETYPE=$SC_NAME" > $MIMEAPPS_LIST
if [ $? -ne 0 ]; then
>&2 echo "FAILED"
exit 1
else
>&2 echo "OK"
fi

else
if grep -Fxq "$MIMETYPE=$SC_NAME" $MIMEAPPS_LIST
then
>&2 echo "OK ($MIMETYPE=$SC_NAME was already in $MIMEAPPS_LIST)"
else
echo "$MIMETYPE=$SC_NAME" >> $MIMEAPPS_LIST
fi
if [ $? -ne 0 ]; then
>&2 echo "FAILED"
exit 1
else
>&2 echo "OK"
fi
fi

# /dev/null since it doesn't seem to be necessary anymore (After switching to ~/.config/mimeapps.list above).
cat >/dev/null <<END
The following manual step may be necessary if $THIS_XDG_DATA_DIR is not in
(XDG_DATA_DIRS=$XDG_DATA_DIRS):
- Associate a .blnk file with blnk:
Double click a blnk file, click "Choose a Program" button, choose blnk, check "Always use..."
(If a different program was already set, right-click a .blnk file, Open With, choose blnk, check "Always use...").
END
35 changes: 28 additions & 7 deletions install-mimetype.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,27 @@
$0
---------------------
END
NOEXIT=false

if [ "x$PREFIX" = "x" ]; then
PREFIX="/usr/local"
if [ "x$1" = "x--no-exit" ]; then
NOEXIT=true
fi


exit_if(){
# This is useless since it doesn't stop the script itself.
code=$1
if [ "x$code" = "x" ]; then
code=1
fi
if [ "x$NOEXIT" = "xfalse" ]; then
exit $code
else
return $code
fi
}

. ./mimetype.rc
if [ "x$RUNAS_CMD" = "x" ]; then
RUNAS_CMD="sudo"
fi
Expand All @@ -19,8 +36,9 @@ if [ ! -f "`command -v $RUNAS_CMD`" ]; then
echo "* $RUNAS_CMD doesn't exist, so it will not be used."
RUNAS_CMD=""
fi
in_file="org.poikilos-blnk.xml"

if [ ! -f "$in_file" ]; then
# defined by mimetype.rc
>&2 echo "Error: '$in_file' is missing. This script must run from the cloned blnk repo."
exit 1
fi
Expand All @@ -41,22 +59,25 @@ END
exit 0
fi

unexpected_db="/usr/share/mime"
expected_db="$PREFIX/share/mime"
unexpected_dest="$unexpected_db/packages/$in_file"
expected_dest="$expected_db/packages/$in_file"
. ./mimetype.rc
if [ $? -ne 0 ]; then exit 1; fi

if [ -f "$unexpected_dest" ]; then
>&2 echo "Error: 'There is already a packaged version of $in_file at $unexpected_dest, so nothing will be done. If you are sure the package is removed, delete the file and this script will attempt to install it at the unpackaged location $expected_dest."
exit 2
fi

echo "[$0] expected_db=$expected_db"
echo "[$0] unexpected_db=$unexpected_db"
if [ -f "$expected_dest" ]; then
DONE_DEST="$expected_dest"
>&2 echo "* trying to update existing $expected_dest..."
>&2 echo " uninstalling..."
$RUNAS_CMD xdg-mime uninstall --mode system $expected_dest
code=$?
if [ $code -eq 0 ]; then
>&2 echo " Success"
DONE_DEST="$expected_dest"
else
>&2 echo " Error: '$RUNAS_CMD xdg-mime uninstall --mode system $expected_dest' failed with error code $code."
exit $code
Expand Down
25 changes: 25 additions & 0 deletions mimetype.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
if [ "x$PREFIX" = "x" ]; then
PREFIX="/usr/local"
fi
if [ "x$THIS_XDG_APP_ID" = "x" ]; then
THIS_XDG_APP_ID="org.poikilos-blnk"
fi
in_file="$THIS_XDG_APP_ID.xml"
echo "mimetypes PREFIX=$PREFIX"

expected_db="/usr/share/mime"
unexpected_db="$PREFIX/share/mime"
# ^ previously these two values were switched, but on Linux Mint 21,
# 'sudo xdg-mime install --mode system $in_file' uses the /usr prefix
# not /usr/local.

unexpected_dest="$unexpected_db/packages/$in_file"
expected_dest="$expected_db/packages/$in_file"
DONE_DEST=
if [ -f "$unexpected_dest" ]; then
DONE_DEST="$unexpected_dest"
elif [ -f "$expected_dest" ]; then
DONE_DEST="$expected_dest"
else
echo "There is no $unexpected_dest nor $expected_dest"
fi

0 comments on commit 9478b86

Please sign in to comment.