Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Scavo committed Oct 13, 2016
0 parents commit fd9c5e7
Show file tree
Hide file tree
Showing 7 changed files with 1,036 additions and 0 deletions.
238 changes: 238 additions & 0 deletions bin/http_xsltproc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
#!/bin/bash

#######################################################################
# Copyright 2016 Tom Scavo
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#######################################################################

#######################################################################
# Help message
#######################################################################

display_help () {
/bin/cat <<- HELP_MSG
This script is a wrapper around the xsltproc command-line tool.
Unlike xsltproc, this script fetches the target XML document
from an HTTP server.
Usage: ${0##*/} [-hv] [-o OUT_FILE] STYLESHEET URL
This script takes two command-line arguments. The STYLESHEET
argument is the absolute path to an XSL document in the local
file system. The URL argument is the absolute URL of an XML
document. The script fetches the XML document at the given
URL using the curl command-line tool and then applies the XSL
stylesheet to the XML document using the xsltproc tool.
This script requires environment variable LIB_DIR, which
specifies a directory containing various XSL stylesheets and
helper scripts including:
$LIB_FILENAMES
Options:
-h Display this message
-v Write verbose messages to stdout
-o Output the transformed document to OUT_FILE
Option -h is mutually exclusive of all other options.
If option -o is omitted, the transformed document is written
to stdout.
HELP_MSG
}

#######################################################################
# Bootstrap
#######################################################################

script_name=${0##*/} # equivalent to basename $0

# library filenames (always list command_paths first)
LIB_FILENAMES="command_paths.sh
compatible_mktemp.sh"

#######################################################################
# Process command-line options and arguments
#######################################################################

help_mode=false; verbose_mode=false
while getopts ":hvo:" opt; do
case $opt in
h)
help_mode=true
;;
v)
verbose_mode=true
;;
o)
out_file="$OPTARG"
;;
\?)
echo "ERROR: $script_name: Unrecognized option: -$OPTARG" >&2
exit 2
;;
:)
echo "ERROR: $script_name: Option -$OPTARG requires an argument" >&2
exit 2
;;
esac
done

if $help_mode; then
display_help
exit 0
fi

# check the output file
if [ -z "$out_file" ]; then
$verbose_mode && echo "$script_name using stdout"
else
$verbose_mode && printf "$script_name using output file: %s\n" "$out_file"
fi

# check the number of remaining arguments
shift $(( OPTIND - 1 ))
if [ "$#" -ne 2 ]; then
echo "ERROR: $script_name found $# command-line arguments (2 required)" >&2
exit 2
fi

# check the stylesheet
xsl_file=$1
if [ ! -f "$xsl_file" ]; then
echo "ERROR: $script_name: file does not exist: $xsl_file" >&2
exit 2
fi
$verbose_mode && printf "$script_name using XSL stylesheet: %s\n" "$xsl_file"

# capture the location of the XML file
xml_location=$2
$verbose_mode && printf "$script_name using XML file at location: %s\n" "$xml_location"

#######################################################################
# Initialization
#######################################################################

# determine the source lib directory
if [ -z "$LIB_DIR" ]; then
echo "ERROR: $script_name requires env var LIB_DIR" >&2
exit 2
fi
if [ ! -d "$LIB_DIR" ]; then
echo "ERROR: $script_name: directory does not exist: $LIB_DIR" >&2
exit 2
fi
$verbose_mode && printf "$script_name using source lib directory: %s\n" "$LIB_DIR"

# source lib files
for lib_filename in $LIB_FILENAMES; do
lib_file="$LIB_DIR/$lib_filename"
if [ ! -f "$lib_file" ]; then
echo "ERROR: $script_name: file does not exist: $lib_file" >&2
exit 2
fi
$verbose_mode && printf "$script_name sourcing lib file: %s\n" "$lib_file"
source "$lib_file" >&2
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: $script_name failed to source script $lib_file" >&2
exit $exit_code
fi
done

# create a temporary directory
tmp_dir=$( make_temp_file -d )
if [ ! -d "$tmp_dir" ] ; then
printf "ERROR: $script_name unable to create temporary dir\n" >&2
exit 2
fi
$verbose_mode && printf "$script_name creating temp dir: %s\n" "$tmp_dir"

# determine temporary directory
if [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ]; then
# use system temporary directory (remove trailing slash)
TMP_DIR="${TMPDIR%%/}/md-transforms"
$verbose_mode && printf "$script_name using temp dir: %s\n" "$TMP_DIR"
else
# create temporary directory
tmp_dir="$( make_temp_file -d )"
if [ ! -d "$tmp_dir" ] ; then
printf "ERROR: $script_name unable to create temporary dir\n" >&2
exit 2
fi
# use temporary directory (remove trailing slash)
TMP_DIR="${tmp_dir%%/}/md-transforms"
$verbose_mode && printf "$script_name creating temp dir: %s\n" "$TMP_DIR"
fi
if [ ! -d "$TMP_DIR" ]; then
/bin/mkdir "$TMP_DIR"
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "ERROR: $script_name failed to create dir: $TMP_DIR" >&2
exit $exit_status
fi
fi

# temporary files
xml_file="${TMP_DIR}/http_response_$$.xml"
xsltproc_out_file="${TMP_DIR}/xsltproc_output_$$"

#######################################################################
# Main processing
#######################################################################

# GET the XML document
$verbose_mode && printf "$script_name writing curl output to file: %s\n" "$xml_file"
curl_output=$( /usr/bin/curl --silent \
--output "$xml_file" \
--write-out 'redirects:%{num_redirects};response:%{http_code};dns:%{time_namelookup};tcp:%{time_connect};ssl:%{time_appconnect};total:%{time_total}' \
"$xml_location"
)
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: ${script_name}: curl failed with status code: $exit_code" >&2
echo "$curl_output" >&2
exit $exit_code
fi
$verbose_mode && printf "$script_name successfully executed curl: %s\n" "$curl_output"

# transform the XML document
$verbose_mode && printf "$script_name writing xsltproc output to file: %s\n" "$xsltproc_out_file"
/usr/bin/xsltproc --output "$xsltproc_out_file" "$xsl_file" "$xml_file" >&2
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: ${script_name}: xsltproc failed with status code: $exit_code" >&2
exit $exit_code
fi
$verbose_mode && printf "$script_name successfully executed xsltproc\n"

if [ -z "$out_file" ]; then
/bin/cat "$xsltproc_out_file"
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: ${script_name} unable to cat output to stdout" >&2
exit $exit_code
fi
else
$verbose_mode && printf "$script_name writing output to file: %s\n" "$out_file"
/bin/cp "$xsltproc_out_file" "$out_file"
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: ${script_name} unable to copy output to file: $out_file" >&2
exit $exit_code
fi
fi

exit 0
116 changes: 116 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/bin/bash

#######################################################################
# Copyright 2016 Tom Scavo
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#######################################################################

################################################################
#
# Usage: install.sh BIN_DIR LIB_DIR
#
# Example: Install in /tmp
#
# $ export BIN_DIR=/tmp/bin
# $ export LIB_DIR=/tmp/lib
# $ install.sh $BIN_DIR $LIB_DIR
#
# Example: Install in $HOME
#
# $ export BIN_DIR=$HOME/bin
# $ export LIB_DIR=$HOME/lib
# $ install.sh $BIN_DIR $LIB_DIR
#
################################################################

script_bin=${0%/*} # equivalent to dirname $0
script_name=${0##*/} # equivalent to basename $0

# generalize
verbose_mode=true

# get command-line args
if [ $# -ne 2 ]; then
echo "ERROR: $script_name: wrong number of arguments: $# (2 required)" >&2
exit 2
fi
bin_dir=$1
lib_dir=$2

# check bin dir
if [ -z "$bin_dir" ]; then
echo "ERROR: $script_name requires bin directory (BIN_DIR)" >&2
exit 2
fi
if [ -d "$bin_dir" ]; then
$verbose_mode && echo "$script_name using bin dir: $bin_dir"
else
$verbose_mode && echo "$script_name creating bin dir: $bin_dir"
/bin/mkdir "$bin_dir"
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "ERROR: $script_name failed to create bin dir: $bin_dir" >&2
exit $exit_status
fi
fi

# check lib dir
if [ -z "$lib_dir" ]; then
echo "ERROR: $script_name requires lib directory (LIB_DIR)" >&2
exit 2
fi
if [ -d "$lib_dir" ]; then
$verbose_mode && echo "$script_name using lib dir: $lib_dir"
else
$verbose_mode && echo "$script_name creating lib dir: $lib_dir"
/bin/mkdir "$lib_dir"
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "ERROR: $script_name failed to create lib dir: $lib_dir" >&2
exit $exit_status
fi
fi

# initialize bin dir
while read script_file; do
$verbose_mode && echo "$script_name copying executable file: $script_file"
/bin/cp $script_file $bin_dir
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "ERROR: $script_name failed to copy script: $script_file" >&2
exit $exit_status
fi
done <<SCRIPTS
$script_bin/bin/http_xsltproc.sh
SCRIPTS

# initialize lib dir
while read source_file; do
echo "$script_name copying source file: $source_file"
/bin/cp $source_file $lib_dir
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "ERROR: $script_name failed to copy source file: $source_file" >&2
exit $exit_status
fi
done <<SOURCES
$script_bin/lib/list_all_IdP_DisplayNames_csv.xsl
$script_bin/lib/list_all_IdPs_csv.xsl
$script_bin/lib/list_all_RandS_IdPs_csv.xsl
$script_bin/lib/list_all_RandS_SPs_csv.xsl
$script_bin/lib/list_all_SPs_csv.xsl
SOURCES

$verbose_mode && echo "$script_name: installation complete"
exit 0
Loading

0 comments on commit fd9c5e7

Please sign in to comment.