diff --git a/Makefile b/Makefile index c33f354..3a4171f 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,14 @@ -DEST ?= /usr/bin +PREFIX?= /usr +BINDIR= ${PREFIX}/bin all: @echo Run \'make install\' to install fontpreview on your device install: - @cp fontpreview $(DEST)/fontpreview - @chmod 755 $(DEST)/fontpreview + @cp fontpreview $(DESTDIR)$(BINDIR)/fontpreview + @chmod 755 $(DESTDIR)$(BINDIR)/fontpreview @echo fontpreview has been installed on your device uninstall: - @rm -rf $(DEST)/fontpreview + @rm -rf $(DESTDIR)$(BINDIR)/fontpreview @echo fontpreview has been removed from your device diff --git a/fontpreview b/fontpreview index 039327a..608f8b8 100755 --- a/fontpreview +++ b/fontpreview @@ -4,12 +4,6 @@ # # Dependencies: sxiv, imagemagick, xdotool, fzf -# Use mktemp to create temporary files that won't -# collide with any other application's tmp files. -FONTPREVIEW_DIR="$(mktemp -d --tmpdir fontpreview_dir_XXXXXXXX)" -PIDFILE="$(mktemp --tmpdir="$FONTPREVIEW_DIR" fontpreview_XXXXXXXX.pid)" -FONT_PREVIEW="$(mktemp --tmpdir="$FONTPREVIEW_DIR" fontpreview_XXXXXXXX.png)" -TERMWIN_IDFILE="$(mktemp --tmpdir="$FONTPREVIEW_DIR" fontpreview_XXXXXXXX.termpid)" VERSION=1.0.4 # Default values @@ -110,7 +104,7 @@ main(){ generate_preview "$font" "$FONT_PREVIEW" - if [ $FIRST_RUN == true ]; then + if [[ $FIRST_RUN == true ]]; then FIRST_RUN=false # Display the font preview using sxiv @@ -126,19 +120,18 @@ main(){ # sxiv when the user exits the script echo $! >"$PIDFILE" - # Check for crashes of sxiv - elif [ -f $PIDFILE ] ; then - PID=$(cat $PIDFILE) - if [ ! -e /proc/$PID ] ; then + # Check for crashes of sxiv + elif [[ -f $PIDFILE ]] ; then + if ! pgrep -F "$PIDFILE" >/dev/null 2>&1; then echo "Restart sxiv - You maybe using a obsolete version. " >&2 # Display the font preview using sxiv - sxiv -g "$SIZE$POSITION" "$FONT_PREVIEW" -N "fontpreview" -b & - + sxiv -g "$SIZE$POSITION" -N "fontpreview" -b "$FONT_PREVIEW" & + # Change focus from sxiv, back to the terminal window # so that user can continue to search for fonts without # having to manually change focus back to the terminal window xdotool windowfocus "$(cat "$TERMWIN_IDFILE")" - + # Save the process ID so that we can kill # sxiv when the user exits the script echo $! >"$PIDFILE" @@ -155,6 +148,16 @@ trap "" SIGTSTP trap pre_exit EXIT +# Use mktemp to create a temporary directory that won't +# collide with temporary files of other application. +FONTPREVIEW_DIR="$(mktemp -d "${TMPDIR:-/tmp}/fontpreview_dir.XXXXXXXX")" || exit +PIDFILE="$FONTPREVIEW_DIR/fontpreview.pid" +touch "$PIDFILE" || exit +FONT_PREVIEW="$FONTPREVIEW_DIR/fontpreview.png" +touch "$FONT_PREVIEW" || exit +TERMWIN_IDFILE="$FONTPREVIEW_DIR/fontpreview.termpid" +touch "$TERMWIN_IDFILE" || exit + # Parse the arguments options=$(getopt -o hi:o: --long position:,size:,version,search-prompt:,font-size:,bg-color:,fg-color:,preview-text:,input:,output:,help -- "$@") eval set -- "$options" @@ -162,11 +165,11 @@ eval set -- "$options" while true; do case "$1" in --size) - shift; + shift FONTPREVIEW_SIZE=$2 ;; --position) - shift; + shift FONTPREVIEW_POSITION=$2 ;; -h|--help)