script-examples/__relpipe_in_filesystem_script_pdf
branchv_0
changeset 28 9172bd97ae99
equal deleted inserted replaced
27:532953173cd5 28:9172bd97ae99
       
     1 #!/bin/bash
       
     2 
       
     3 # Relational pipes
       
     4 # Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
       
     5 #
       
     6 # This program is free software: you can redistribute it and/or modify
       
     7 # it under the terms of the GNU General Public License as published by
       
     8 # the Free Software Foundation, version 3 of the License.
       
     9 #
       
    10 # This program is distributed in the hope that it will be useful,
       
    11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13 # GNU General Public License for more details.
       
    14 #
       
    15 # You should have received a copy of the GNU General Public License
       
    16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17 
       
    18 
       
    19 # Quite dirty hack to get some information about given PDF file
       
    20 # TODO: better field names, more stable API
       
    21 # TODO: call a PDF library rather than parse output of a commandline tool with human readable output
       
    22 
       
    23 if [[ $# == 0 ]]; then
       
    24 	echo "1";
       
    25 	if [[ "x$field" == "xPages" ]]; then echo "integer";
       
    26 	elif [[ -z "${field+x}" ]]; then echo "boolean";
       
    27 	else echo "string";
       
    28 	fi
       
    29 elif [[ -f "$1" || -d "$1" ]]; then
       
    30 	info="`pdfinfo -isodates "$1"`";
       
    31 	valid=$?;
       
    32 	if [[ "x$field" == "xPages" ]]; then
       
    33 		if [[ $valid == 0 ]]; then
       
    34 			echo "$info" | grep "^$field:" | sed -E 's/[^:]+:\s+(.*)/\1/g' | tr -d '\n';
       
    35 		else
       
    36 			printf 0;
       
    37 			# exit 40; # TODO: null
       
    38 		fi
       
    39 	elif [[ -z "${field+x}" ]]; then
       
    40 		if [[ $valid == 0 ]]; then printf "true"; else printf "false"; fi
       
    41 	else
       
    42 		echo "$info" | grep "^$field:" | sed -E 's/[^:]+:\s+(.*)/\1/g' | tr -d '\n';
       
    43 	fi
       
    44 else
       
    45 	exit 40;
       
    46 fi