Skip to content

Commit

Permalink
feat: Automate HPLIP plugin installation
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxxMD committed Apr 29, 2024
1 parent 44bf983 commit 91899d4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 51 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ FROM scanservjs-core
# default - you will need to specifically target it.
# ==============================================================================
FROM scanservjs-core AS scanservjs-hplip
COPY hplip.sh /hplip.sh
RUN apt-get update \
&& apt-get install -yq libsane-hpaio \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& echo hpaio >> /etc/sane.d/dll.conf
&& rm -rf /var/lib/apt/lists/*

# brscan4 image
#
Expand Down
50 changes: 1 addition & 49 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,54 +41,6 @@ fi
unset IFS
set +f

if [ $(dpkg-query -W -f='${Status}' libsane-hpaio 2>/dev/null | grep -c "ok installed") -eq 1 ];
then
# assume user does not want to install plugin
HP_PLUGIN="${HP_PLUGIN:=false}"
if [ "$PLUGIN" != "false" ]; then
echo "Checking HP_PLUGIN status..."
# determine installed HPLIP version
RE="HP Linux Imaging and Printing System \(ver\. (.+?)\)"
cmd_output=$(hp-plugin --help 2>&1)
#https://stackoverflow.com/a/2778096
RAW_VERSION="$(echo "$cmd_output" | sed -rn "s/$RE/\1/p")"
# Remove ansi coloring so its just a raw string
#https://stackoverflow.com/a/51141872
HPLIP_VERSION=$(echo "$RAW_VERSION" | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g')
printf 'HPLIP Version: %s\n' "$HPLIP_VERSION"

# if variable is true then try to determine plugin version automatically
if [ "$HP_PLUGIN" = "true" ]; then

# check if plugin is already installed
# files installed to these locations https://wiki.gentoo.org/wiki/HPLIP#Binary_plugins
if [ -d /usr/share/hplip/data/firmware ]; then
printf 'A plugin is already installed. To force (re)install specify version in ENV like HP_PLUGIN=%s\n' "$HPLIP_VERSION"
else
INSTALL_PLUGIN_VERSION=$HPLIP_VERSION
fi
else
INSTALL_PLUGIN_VERSION=$HPLIP_VERSION
fi
else
echo 'HP Driver installed but HP_PLUGIN ENV not invoked'
fi

if [ ! -z "$INSTALL_PLUGIN_VERSION" ]; then
printf 'Attempting to install plugin version %s\n' "$INSTALL_PLUGIN_VERSION"
PLUGIN_FILE="/tmp/hplip-$INSTALL_PLUGIN_VERSION-plugin.run"

if [ ! -f "$PLUGIN_FILE" ]; then
echo 'Plugin does not already existing, downloading...'
wget --backups 0 -P /tmp "https://developers.hp.com/sites/default/files/hplip-$INSTALL_PLUGIN_VERSION-plugin.run"
fi
chmod +x "$PLUGIN_FILE"
yes | "$PLUGIN_FILE" --noprogress --accept --nox11 -- -i
echo "Plugin installed!"
fi
else
echo 'HP driver not installed, no plugin check required.'
fi

/hplip.sh || true

node ./server/server.js
54 changes: 54 additions & 0 deletions hplip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/sh
set -e

if [ $(dpkg-query -W -f='${Status}' libsane-hpaio 2>/dev/null | grep -c "ok installed") -eq 1 ];
then
# assume user does not want to install plugin
HP_PLUGIN="${HP_PLUGIN:=false}"
if [ "$PLUGIN" != "false" ]; then
echo "Checking HP_PLUGIN status..."
# determine installed HPLIP version
RE="HP Linux Imaging and Printing System \(ver\. (.+?)\)"
cmd_output=$(hp-plugin --help 2>&1)
#https://stackoverflow.com/a/2778096
RAW_VERSION="$(echo "$cmd_output" | sed -rn "s/$RE/\1/p")"
# Remove ansi coloring so its just a raw string
#https://stackoverflow.com/a/51141872
HPLIP_VERSION=$(echo "$RAW_VERSION" | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g')
printf 'HPLIP Version: %s\n' "$HPLIP_VERSION"

# if variable is true then try to determine plugin version automatically
if [ "$HP_PLUGIN" = "true" ]; then

# check if plugin is already installed
# files installed to these locations https://wiki.gentoo.org/wiki/HPLIP#Binary_plugins
if [ -d /usr/share/hplip/data/firmware ]; then
printf 'A plugin is already installed. To force (re)install specify version in ENV like HP_PLUGIN=%s\n' "$HPLIP_VERSION"
else
INSTALL_PLUGIN_VERSION=$HPLIP_VERSION
fi
else
INSTALL_PLUGIN_VERSION=$HPLIP_VERSION
fi
else
echo 'HP Driver installed but HP_PLUGIN ENV not invoked'
fi

if [ ! -z "$INSTALL_PLUGIN_VERSION" ]; then
printf 'Attempting to install plugin version %s\n' "$INSTALL_PLUGIN_VERSION"
PLUGIN_FILE="/tmp/hplip-$INSTALL_PLUGIN_VERSION-plugin.run"

if [ ! -f "$PLUGIN_FILE" ]; then
echo 'Plugin does not already existing, downloading...'
wget --backups 0 -q -P /tmp "https://developers.hp.com/sites/default/files/hplip-$INSTALL_PLUGIN_VERSION-plugin.run" || true
fi
echo "Making plugin runnable..."
chmod +x "$PLUGIN_FILE"
echo "Starting plugin install..."
# has to run as root in order to prevent erroneous invisible password prompt after license accept
su root -c "yes y | $PLUGIN_FILE --noprogress --accept --nox11 -- -i"
echo "Plugin installed!"
fi
else
echo 'HP driver not installed, no plugin check required.'
fi

0 comments on commit 91899d4

Please sign in to comment.