pdf2book/install.sh

122 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
## INSTALL pdf2book in Linux or macOS
### Dependencies:
# - bash: check with `bash --version`
# - python3: check with `python3 --version`
#- venv: check with `python3 -m venv --help`
#
# check dependencies
set -e
bash --version > /dev/null
python3 --version > /dev/null
python3 -m venv --help > /dev/null
set +e
#
PREFIX="$HOME/.local"
HELP="""
$0: install pdf2book with its virtual environment
usage:
-h|--help show this help message and exti
-d|--delete remove pdf2book
-f|--force force overwriting of PREFIX/lib/pdf2book.git
can be used for upgrading
-p|--prefix PREFIX install into PREFIX/lib and PREFIX/bin
-v|--verbose print more info (set -x)
-y|--yes don't ask for confimation of prefix
"""
# OPTIONS
# Iterate through each argument
while [[ $# -gt 0 ]]; do
case "$1" in
--delete|-d)
DELETE=1
;;
--force|-f)
FORCE=1
;;
--help|-h)
echo "$HELP"
# Add help instructions here
exit 0
;;
--prefix|-p)
PSET=1
PREFIX=$2
shift 1
;;
--verbose|-v)
set -x
;;
--yes|-y)
PSET=1
;;
*)
echo "Unknown option: $1"
echo "HELP"
exit 1
;;
esac
shift 1 # Move to the next argument
done
if [[ $PSET != 1 ]]
then
echo -n """Where to do you want to install 'pdf2book'?
PREFIX [$PREFIX] :
"""
read userprefix
if [[ -n $userprefix ]]
then
PREFIX=$userprefix
fi
fi
LIBDIR=$PREFIX/lib
BINDIR=$PREFIX/bin
SRCDIR=$LIBDIR/pdf2book.git
if [[ -d $SRCDIR ]]
then
if [[ -z $FORCE ]]
then
echo -n """Directory '$SRCDIR' already exist.
Do you want to overwrite? [y/N] """
read confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || echo "... exiting ..." && exit 1
fi
rm -rf $SRCDIR
fi
mkdir -p $LIBDIR $BINDIR
git clone https://git.wlankabel.at/onipa/pdf2book.git $SRCDIR
python3 -m venv $SRCDIR/.venv
. $SRCDIR/.venv/bin/activate
pip3 install pypdf
echo -n '''#!/bin/bash
SRCDIR='$SRCDIR'
VENVDIR=$SRCDIR/.venv
. $VENVDIR/bin/activate
python3 $SRCDIR/pdf2book.py $@
deactivate
'''> $BINDIR/pdf2book
chmod +x $BINDIR/pdf2book
echo "pdf2book is installed into '$(realpath $BINDIR)'"
#check if $BINDIR is in $PATH
INPATH=$(echo $PATH | tr : '\n' | grep $(realpath $BINDIR) | wc -l )
if [[ $INPATH -lt 1 ]]
then
if [[ $(uname) == "Darwin" ]]
then
RCFILE='.zshrc'
else
RCFILE=".bashrc"
fi
echo "WARNING! '$BINDIR' is not in \$PATH"
echo -n """Do you want to add '$BINDIR' to \$PATH (via ~/$RCFILE)? [Y/n] """
read confirm
if [[ $confirm == [yY] || $confirm == [yY][eE][sS] || -z $confirm ]]
then
echo 'export PATH=$PATH:'$(realpath $BINDIR) >> $HOME/$RCFILE
fi
fi
#echo inpath: $INPATH
echo "you might need to restart your shell"
#echo prefix: $PREFIX