#!/bin/bash #AUTH hwding #DATE Feb 19 2019 #DESC install unstamp as a command user_bin=`echo ~`"/bin/" locl_bin="/usr/local/bin/" jar_name="pdf-unstamper.jar" exe_name="unstamp" _version="0.2.5" jar_durl="https://github.com/hwding/pdf-unstamper/releases/download/$_version/$jar_name" wrapper="#!/bin/bash\njava -jar ${user_bin}${jar_name} \"\$@\"\n" # default option(s) create_link=1 # check if the script is running in root if [ ! $UID -eq 0 ]; then echo -e "$(tput setaf 1)> plz run this script in root permission$(tput sgr 0)" exit -1 fi # parse option(s) while getopts ":d:w" opt; do case "${opt}" in w) create_link=0 ;; d) given_dir="${OPTARG}" # complete the slash at the end of the given directory [[ "${given_dir: -1}" == "/" ]] && user_bin=${given_dir} || user_bin="${given_dir}/" ;; esac done shift $((OPTIND-1)) function chk_f() { if [ ! -f "$1" ]; then return 0; else return 1; fi } function chk_l() { if [ ! -L "$1" ]; then return 0; else return 1; fi } function chk_d() { if [ ! -d $1 ]; then return 0; else return 1; fi } echo -e "\n╭──────────────────────────────────────────────╮" echo -e "│ Installer for pdf-unstamper │" echo -e "│ $(tput setaf 6)https://github.com/hwding/pdf-unstamper$(tput sgr 0) │" echo -e "╰──────────────────────────────────────────────╯\n" # check JRE installation if type -p java > /dev/null 2>&1; then _java=1 echo -e "$(tput setaf 2)> JRE detected in PATH$(tput sgr 0)" elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then _java=1 echo -e "$(tput setaf 2)> JRE detected in JAVA_HOME$(tput sgr 0)" else _java=0 echo -e -n "$(tput setaf 1)> no JRE installed, continue? [ENTER]$(tput sgr 0)" read fi # check JRE version if [ ${_java} -gt 0 ]; then version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}') if [[ "$version" > "1.8" ]]; then echo -e "$(tput setaf 2)> JRE version is at least 1.8$(tput sgr 0)" else echo -e -n "$(tput setaf 1)> JRE version less than 1.8, continue? [ENTER]$(tput sgr 0)" read fi fi # check ~/bin/ chk_d ${user_bin} if [ $? -eq 0 ]; then echo -e -n "$(tput setaf 1)> target dir $user_bin not detected, create? [ENTER]$(tput sgr 0)" read `mkdir ${user_bin}` else echo -e "$(tput setaf 2)> target dir $user_bin detected$(tput sgr 0)" fi # remove old JAR in current dir chk_f "$jar_name" if [ $? -eq 1 ]; then `rm ${jar_name}` fi # check if we have a proper download tool before downloading if ! type -p wget > /dev/null 2>&1; then if ! type -p curl > /dev/null 2>&1; then echo -e "$(tput setaf 1)> you must have wget or curl to download the JAR$(tput sgr 0)" exit -1 fi # download with curl echo -n "> downloading $jar_name version $_version from GitHub using curl..." `curl -L -o ${jar_name} ${jar_durl} > /dev/null 2>&1` else # download with wget echo -n "> downloading $jar_name version $_version from GitHub using wget..." `wget ${jar_durl} > /dev/null 2>&1` fi echo "done" # generate wrapper chk_f ${exe_name} if [ $? -eq 0 ]; then echo "> generating wrapper..." echo -e ${wrapper} > ${exe_name} fi # remove old JAR & wrapper in ~/bin chk_f "${user_bin}${jar_name}" if [ $? -eq 1 ]; then echo "> removing old version of $jar_name" `rm ${user_bin}${jar_name}` fi chk_f "${user_bin}${exe_name}" if [ $? -eq 1 ]; then echo "> removing old version of $exe_name" `rm ${user_bin}${exe_name}` fi # install new JAR & wrapper echo "> installing ${exe_name}..." `mv ${jar_name} ${user_bin}` `mv ${exe_name} ${user_bin}` # grant permission echo "> granting executable permission to wrapper..." `chmod +x ${user_bin}${exe_name}` # create soft link if [ ${create_link} -gt 0 ]; then echo "> linking ${user_bin}${exe_name} to ${locl_bin}${exe_name}" chk_l "${locl_bin}${exe_name}" if [ $? -eq 1 ]; then `rm ${locl_bin}${exe_name}` fi `ln -s ${user_bin}${exe_name} ${locl_bin}${exe_name}` fi echo -e "$(tput setaf 2)> ${exe_name} installed as ${user_bin}${exe_name}$(tput sgr 0)" echo "> hava a nice day" echo