Explorar el Código

v2.3.4 Refinement to install and upgrade options, remove temporary files after finishing, new option --assume-yes|--yes to avoid prompts/force installs and upgrades, new option --insecure|--no-check-certificates to use curl/wget without SSL support on upgrades

master
padre
commit
4b8e086004
Se han modificado 1 ficheros con 131 adiciones y 61 borrados
  1. +131
    -61
      pdfScale.sh

+ 131
- 61
pdfScale.sh Ver fichero

@@ -12,7 +12,7 @@
# And: https://gist.github.com/MichaelJCole/86e4968dbfc13256228a # And: https://gist.github.com/MichaelJCole/86e4968dbfc13256228a




VERSION="2.3.2"
VERSION="2.3.4"




###################### EXTERNAL PROGRAMS ####################### ###################### EXTERNAL PROGRAMS #######################
@@ -95,6 +95,9 @@ PROJECT_NAME="pdfScale"
PROJECT_URL="https://github.com/tavinus/$PROJECT_NAME" PROJECT_URL="https://github.com/tavinus/$PROJECT_NAME"
PROJECT_BRANCH='master' PROJECT_BRANCH='master'
HTTPS_INSECURE=$FALSE HTTPS_INSECURE=$FALSE
ASSUME_YES=$FALSE
RUN_SELF_INSTALL=$FALSE
TARGET_LOC="/usr/local/bin/pdfscale"


########################## EXIT FLAGS ########################## ########################## EXIT FLAGS ##########################


@@ -273,9 +276,9 @@ gsPageScale() {


# Prints GS call for scaling # Prints GS call for scaling
gsPrintPageScale() { gsPrintPageScale() {
local _call_str=""
local _call_str=""
# Print Scale page command # Print Scale page command
read -d '' _call_str<< _EOF_
read -d '' _call_str<< _EOF_
"$GSBIN" \ "$GSBIN" \
-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \ -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
-dCompatibilityLevel="1.5" -dPDFSETTINGS="$PDF_SETTINGS" \ -dCompatibilityLevel="1.5" -dPDFSETTINGS="$PDF_SETTINGS" \
@@ -289,7 +292,7 @@ gsPrintPageScale() {
-f "$INFILEPDF" -f "$INFILEPDF"
_EOF_ _EOF_


echo -ne "$_call_str"
echo -ne "$_call_str"
} }


# Runs the ghostscript paper resize script # Runs the ghostscript paper resize script
@@ -330,9 +333,9 @@ gsPageResize() {
# Prints GS call for resizing # Prints GS call for resizing
gsPrintPageResize() { gsPrintPageResize() {
# Print Resize page command # Print Resize page command
local _call_str=""
local _call_str=""
# Print Scale page command # Print Scale page command
read -d '' _call_str<< _EOF_
read -d '' _call_str<< _EOF_
"$GSBIN" \ "$GSBIN" \
-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \ -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
-dCompatibilityLevel="1.5" -dPDFSETTINGS="$PDF_SETTINGS" \ -dCompatibilityLevel="1.5" -dPDFSETTINGS="$PDF_SETTINGS" \
@@ -347,7 +350,7 @@ gsPrintPageResize() {
-f "$INFILEPDF" -f "$INFILEPDF"
_EOF_ _EOF_


echo -ne "$_call_str"
echo -ne "$_call_str"
} }


# Returns $TRUE if we should use the source paper size, $FALSE otherwise # Returns $TRUE if we should use the source paper size, $FALSE otherwise
@@ -546,13 +549,23 @@ getOptions() {
shift shift
;; ;;
--install|--self-install) --install|--self-install)
RUN_SELF_INSTALL=$TRUE
shift shift
selfInstall "$1"
shift
if [[ ${1:0:1} != "-" ]]; then
TARGET_LOC="$1"
shift
fi
;; ;;
--upgrade|--self-upgrade) --upgrade|--self-upgrade)
RUN_SELF_UPGRADE=$TRUE
shift shift
selfUpgrade
;;
--insecure|--no-check-certificate)
HTTPS_INSECURE=$TRUE
shift
;;
--yes|--assume-yes)
ASSUME_YES=$TRUE
shift shift
;; ;;
--print-gs-call|--gs-call) --print-gs-call|--gs-call)
@@ -564,6 +577,9 @@ getOptions() {
;; ;;
esac esac
done done
shouldInstall && selfInstall "$TARGET_LOC" # WILL EXIT HERE
shouldUpgrade && selfUpgrade # WILL EXIT HERE


isEmpty "${_optArgs[2]}" || initError "Seems like you passed an extra file name?"$'\n'"Invalid option: ${_optArgs[2]}" $EXIT_INVALID_OPTION isEmpty "${_optArgs[2]}" || initError "Seems like you passed an extra file name?"$'\n'"Invalid option: ${_optArgs[2]}" $EXIT_INVALID_OPTION


@@ -598,6 +614,16 @@ getOptions() {
validateOutFile validateOutFile
} }


# Returns $TRUE if the install flag is set
shouldInstall() {
return $RUN_SELF_INSTALL
}

# Returns $TRUE if the upgrade flag is set
shouldUpgrade() {
return $RUN_SELF_UPGRADE
}

# Install pdfScale # Install pdfScale
selfInstall() { selfInstall() {
CURRENT_LOC="$(readlink -f $0)" CURRENT_LOC="$(readlink -f $0)"
@@ -618,19 +644,29 @@ selfInstall() {
local _answer="NO" local _answer="NO"
if isNotDir "$TARGET_FOLDER"; then if isNotDir "$TARGET_FOLDER"; then
echo $'\nThe target folder does not exist\n > '"$TARGET_FOLDER" echo $'\nThe target folder does not exist\n > '"$TARGET_FOLDER"
read -p $'\nCreate the target folder? Y/y to continue > ' _answer
_answer="$(lowercase $_answer)"
if assumeYes; then
echo ''
_answer="y"
else
read -p $'\nCreate the target folder? Y/y to continue > ' _answer
_answer="$(lowercase $_answer)"
fi
if [[ "$_answer" = "y" || "$_answer" = "yes" ]]; then if [[ "$_answer" = "y" || "$_answer" = "yes" ]]; then
_answer="no" _answer="no"
if mkdir -p "$TARGET_FOLDER" 2>/dev/null; then if mkdir -p "$TARGET_FOLDER" 2>/dev/null; then
echo "Folder Created!"
echo " > Folder Created!"
else else
echo $'\n'"There was an error when trying to create the folder." echo $'\n'"There was an error when trying to create the folder."
read -p $'\nDo you want to try again with sudo (as root)? Y/y to continue > ' _answer
_answer="$(lowercase $_answer)"
if assumeYes; then
echo $'\nTrying again with sudo, enter password if needed > '
_answer="y"
else
read -p $'\nDo you want to try again with sudo (as root)? Y/y to continue > ' _answer
_answer="$(lowercase $_answer)"
fi
if [[ "$_answer" = "y" || "$_answer" = "yes" ]]; then if [[ "$_answer" = "y" || "$_answer" = "yes" ]]; then
NEED_SUDO=$TRUE NEED_SUDO=$TRUE
if mkdir -p "$TARGET_FOLDER" 2>/dev/null; then
if sudo mkdir -p "$TARGET_FOLDER" 2>/dev/null; then
echo "Folder Created!" echo "Folder Created!"
else else
echo "There was an error when trying to create the folder." echo "There was an error when trying to create the folder."
@@ -642,19 +678,23 @@ selfInstall() {
fi fi
fi fi
else else
echo "Exiting..."
echo "Exiting... (cancelled by user)"
exit $EXIT_ERROR exit $EXIT_ERROR
fi fi
fi fi
_answer="no" _answer="no"
if isFile "$TARGET_LOC"; then if isFile "$TARGET_LOC"; then
echo $'\n'"The target file already exists: $TARGET_LOC" echo $'\n'"The target file already exists: $TARGET_LOC"
read -p "Y/y to overwrite, anything else to cancel > " _answer
_answer="$(lowercase $_answer)"
if assumeYes; then
_answer="y"
else
read -p "Y/y to overwrite, anything else to cancel > " _answer
_answer="$(lowercase $_answer)"
fi
if [[ "$_answer" = "y" || "$_answer" = "yes" ]]; then if [[ "$_answer" = "y" || "$_answer" = "yes" ]]; then
echo "Target will be replaced!" echo "Target will be replaced!"
else else
echo "Exiting..."
echo "Exiting... (cancelled by user)"
exit $EXIT_ERROR exit $EXIT_ERROR
fi fi
fi fi
@@ -675,8 +715,13 @@ selfInstall() {
else else
_answer="no" _answer="no"
echo "There was an error when trying to install pdfScale." echo "There was an error when trying to install pdfScale."
read -p $'Do you want to try again with sudo (as root)? Y/y to continue > ' _answer
_answer="$(lowercase $_answer)"
if assumeYes; then
echo $'\nTrying again with sudo, enter password if needed > '
_answer="y"
else
read -p $'Do you want to try again with sudo (as root)? Y/y to continue > ' _answer
_answer="$(lowercase $_answer)"
fi
if [[ "$_answer" = "y" || "$_answer" = "yes" ]]; then if [[ "$_answer" = "y" || "$_answer" = "yes" ]]; then
NEED_SUDO=$TRUE NEED_SUDO=$TRUE
if sudo cp "$CURRENT_LOC" "$TARGET_LOC"; then if sudo cp "$CURRENT_LOC" "$TARGET_LOC"; then
@@ -688,15 +733,16 @@ selfInstall() {
exit $EXIT_ERROR exit $EXIT_ERROR
fi fi
else else
echo "Exiting..."
echo "Exiting... (cancelled by user)"
exit $EXIT_ERROR exit $EXIT_ERROR
fi fi
fi fi
exit $?
exit $EXIT_ERROR
} }


# Tries to download with curl or wget # Tries to download with curl or wget
getUrl() { getUrl() {
useInsecure && echo $'\nHTTPS Insecure flag is enabled!\nCertificates will be ignored by curl/wget\n'
local url="$1" local url="$1"
local target="$2" local target="$2"
local _stat="" local _stat=""
@@ -711,7 +757,7 @@ getUrl() {
if isExecutable "$WGET_BIN"; then if isExecutable "$WGET_BIN"; then
useInsecure && WGET_BIN="$WGET_BIN --no-check-certificate" useInsecure && WGET_BIN="$WGET_BIN --no-check-certificate"
echo "Downloading file with wget" echo "Downloading file with wget"
_stat="$("$WGET_BIN" -O "$target" "$url" 2>&1)"
_stat="$($WGET_BIN -O "$target" "$url" 2>&1)"
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
return $TRUE return $TRUE
else else
@@ -724,7 +770,7 @@ getUrl() {
elif isExecutable "$CURL_BIN"; then elif isExecutable "$CURL_BIN"; then
useInsecure && CURL_BIN="$CURL_BIN --insecure" useInsecure && CURL_BIN="$CURL_BIN --insecure"
echo "Downloading file with curl" echo "Downloading file with curl"
_stat="$("$CURL_BIN" -o "$target" "$url" 2>&1)"
_stat="$($CURL_BIN -o "$target" "$url" 2>&1)"
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
return $TRUE return $TRUE
else else
@@ -743,37 +789,37 @@ getUrl() {


# Tries to remove temporary files from upgrade # Tries to remove temporary files from upgrade
clearUpgrade() { clearUpgrade() {
echo $'\nCleaning up downloaded files from /tmp'
if isFile "$TMP_TARGET"; then
echo -n " Remove $TMP_TARGET > "
rm "$TMP_TARGET" 2>/dev/null && echo "Ok" || echo "Fail"
else
echo " No temporary tarball was found to remove."
fi
if isDir "$TMP_EXTRACTED"; then
echo -n " Remove $TMP_EXTRACTED > "
rm -rf "$TMP_EXTRACTED" 2>/dev/null && echo "Ok" || echo "Fail"
else
echo " No temporary master folder was found to remove."
fi
echo $'\nCleaning up downloaded files from /tmp'
if isFile "$TMP_TARGET"; then
echo -n " > $TMP_TARGET > "
rm "$TMP_TARGET" 2>/dev/null && echo "Ok" || echo "Fail"
else
echo " > no temporary tarball was found to remove"
fi
if isDir "$TMP_EXTRACTED"; then
echo -n " > $TMP_EXTRACTED > "
rm -rf "$TMP_EXTRACTED" 2>/dev/null && echo "Ok" || echo "Fail"
else
echo " > no temporary master folder was found to remove"
fi
} }


# Exit upgrade with message and status code # Exit upgrade with message and status code
# $1 Mensagem (printed if not empty) # $1 Mensagem (printed if not empty)
# $2 Status (defaults to $EXIT_ERROR) # $2 Status (defaults to $EXIT_ERROR)
exitUpgrade() { exitUpgrade() {
isDir "$_cwd" && cd "$_cwd"
isNotEmpty "$1" && echo "$1"
clearUpgrade
isNotEmpty "$2" && exit $2
exit $EXIT_ERROR
isDir "$_cwd" && cd "$_cwd"
isNotEmpty "$1" && echo "$1"
clearUpgrade
isNotEmpty "$2" && exit $2
exit $EXIT_ERROR
} }


# Downloads current version from github's MASTER branch # Downloads current version from github's MASTER branch
selfUpgrade() { selfUpgrade() {
CURRENT_LOC="$(readlink -f $0)" CURRENT_LOC="$(readlink -f $0)"
_cwd="$(pwd)" _cwd="$(pwd)"
local _cur_tstamp="$(date '+%Y%m%d-%H%M%S')"
local _cur_tstamp="$(date '+%Y%m%d-%H%M%S')"
TMP_DIR='/tmp' TMP_DIR='/tmp'
TMP_TARGET="$TMP_DIR/pdfScale_$_cur_tstamp.tar.gz" TMP_TARGET="$TMP_DIR/pdfScale_$_cur_tstamp.tar.gz"
TMP_EXTRACTED="$TMP_DIR/$PROJECT_NAME-$PROJECT_BRANCH" TMP_EXTRACTED="$TMP_DIR/$PROJECT_NAME-$PROJECT_BRANCH"
@@ -791,13 +837,13 @@ selfUpgrade() {
echo $'\n'"Extracting compressed file" echo $'\n'"Extracting compressed file"
cd "$TMP_DIR" cd "$TMP_DIR"
if ! (tar xzf "$TMP_TARGET" 2>/dev/null || gtar xzf "$TMP_TARGET" 2>/dev/null); then if ! (tar xzf "$TMP_TARGET" 2>/dev/null || gtar xzf "$TMP_TARGET" 2>/dev/null); then
exitUpgrade "Extraction error."
exitUpgrade "Extraction error."
fi fi
if ! cd "$TMP_EXTRACTED" 2>/dev/null; then if ! cd "$TMP_EXTRACTED" 2>/dev/null; then
exitUpgrade $'Error when accessing temporary folder\n > '"$TMP_EXTRACTED"
exitUpgrade $'Error when accessing temporary folder\n > '"$TMP_EXTRACTED"
fi fi
if ! chmod +x pdfScale.sh; then if ! chmod +x pdfScale.sh; then
exitUpgrade $'Error when setting new pdfScale to executable\n > '"$TMP_EXTRACTED/pdfScale.sh"
exitUpgrade $'Error when setting new pdfScale to executable\n > '"$TMP_EXTRACTED/pdfScale.sh"
fi fi
local newver="$(./pdfScale.sh --version 2>/dev/null)" local newver="$(./pdfScale.sh --version 2>/dev/null)"
local curver="$(printVersion 3 2>/dev/null)" local curver="$(printVersion 3 2>/dev/null)"
@@ -816,40 +862,50 @@ selfUpgrade() {
echo "It is basically a miracle or you have came from the future with this version!" echo "It is basically a miracle or you have came from the future with this version!"
echo "BE CAREFUL NOT TO DELETE THE BETA/ALPHA VERSION WITH THIS UPDATE!" echo "BE CAREFUL NOT TO DELETE THE BETA/ALPHA VERSION WITH THIS UPDATE!"
else else
exitUpgrade "An unidentified error has ocurred. Exiting..."
exitUpgrade "An unidentified error has ocurred. Exiting..."
fi
if assumeYes; then
echo $'\n'"Assume yes activated, current version will be replaced with master branch"
_answer="y"
else
echo "whyyy?"
echo $'\n'"Are you sure that you want to replace the current installation with the downloaded one?"
read -p "Y/y to continue, anything else to cancel > " _answer
_answer="$(lowercase $_answer)"
fi fi
echo $'\n'"Are you sure that you want to replace the current instalation with the downloaded one?"
read -p "Y/y to continue, anything else to cancel > " _answer
_answer="$(lowercase $_answer)"
echo echo
if [[ "$_answer" = "y" || "$_answer" = "yes" ]]; then if [[ "$_answer" = "y" || "$_answer" = "yes" ]]; then
echo "Upgrading..." echo "Upgrading..."
if cp "./pdfScale.sh" "$CURRENT_LOC" 2>/dev/null; then if cp "./pdfScale.sh" "$CURRENT_LOC" 2>/dev/null; then
exitUpgrade $'\n'"Success! Upgrade finished!"$'\n'" > $CURRENT_LOC" $EXIT_SUCCESS
exitUpgrade $'\n'"Success! Upgrade finished!"$'\n'" > $CURRENT_LOC" $EXIT_SUCCESS
else else
_answer="no" _answer="no"
echo $'\n'"There was an error when copying the new version." echo $'\n'"There was an error when copying the new version."
echo "Do you want to retry using sudo (as root)?"
read -p "Y/y to continue, anything else to cancel > " _answer
if assumeYes; then
echo $'\nAssume yes activated, retrying with sudo.\nEnter password if needed > \n'
_answer="y"
else
echo "Do you want to retry using sudo (as root)?"
read -p "Y/y to continue, anything else to cancel > " _answer
fi
_answer="$(lowercase $_answer)" _answer="$(lowercase $_answer)"
if [[ "$_answer" = "y" || "$_answer" = "yes" ]]; then if [[ "$_answer" = "y" || "$_answer" = "yes" ]]; then
echo "Upgrading with sudo..." echo "Upgrading with sudo..."
if sudo cp "./pdfScale.sh" "$CURRENT_LOC" 2>/dev/null; then if sudo cp "./pdfScale.sh" "$CURRENT_LOC" 2>/dev/null; then
exitUpgrade $'\n'"Success! Upgrade finished!"$'\n'" > $CURRENT_LOC" $EXIT_SUCCESS
exitUpgrade $'\n'"Success! Upgrade finished!"$'\n'" > $CURRENT_LOC" $EXIT_SUCCESS
else else
exitUpgrade "There was an error when copying the new version."
exitUpgrade "There was an error when copying the new version."
fi fi
else else
exitUpgrade "Exiting... (cancelled by user)"
exitUpgrade "Exiting... (cancelled by user)"
fi fi


fi fi
exitUpgrade "An unidentified error has ocurred. Exiting..."
exitUpgrade "An unidentified error has ocurred. Exiting..."
else else
exitUpgrade "Exiting... (cancelled by user)" exitUpgrade "Exiting... (cancelled by user)"
fi fi
exitUpgrade "An unidentified error has ocurred. Exiting..."
exitUpgrade "An unidentified error has ocurred. Exiting..."
} }


# Compares versions with x.x.x format # Compares versions with x.x.x format
@@ -1803,6 +1859,16 @@ useInsecure() {
return $HTTPS_INSECURE return $HTTPS_INSECURE
} }


# Returns $TRUE if we should not ask anything and assume yes as answer
assumeYes() {
return $ASSUME_YES
}

# Returns $TRUE if we should ask the user for input
shouldAskUser() {
assumeYes && return $FALSE
return $TRUE
}


###################### PRINTING TO SCREEN ###################### ###################### PRINTING TO SCREEN ######################


@@ -1861,6 +1927,10 @@ Parameters:
--upgrade, --self-upgrade --upgrade, --self-upgrade
Upgrades itself in-place (same path/name of the pdfScale.sh caller) Upgrades itself in-place (same path/name of the pdfScale.sh caller)
Downloads the master branch tarball and tries to self-upgrade Downloads the master branch tarball and tries to self-upgrade
--insecure, --no-check-certificate
Use curl/wget without SSL library support
--yes, --assume-yes
Will answer yes to any prompt on install or upgrade, use with care
-n, --no-overwrite -n, --no-overwrite
Aborts execution if the output PDF file already exists Aborts execution if the output PDF file already exists
By default, the output file will be overwritten By default, the output file will be overwritten


Cargando…
Cancelar
Guardar