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 12, 2016
0 parents commit fd2b232
Show file tree
Hide file tree
Showing 9 changed files with 1,524 additions and 0 deletions.
215 changes: 215 additions & 0 deletions bin/cget.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#!/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 retrieves and caches HTTP resources on disk.
A previously cached resource is retrieved via HTTP Conditional
GET [RFC 7232]. If the web server responds with HTTP 200 OK,
the resource is cached and written to stdout. OTOH, if the web
server responds with 304 Not Modified, the cache is refreshed
but no output.
Usage: ${0##*/} [-hv] [-F | -C] URL
This script takes a single command-line argument. The URL
argument is the absolute URL of an HTTP resource. By default,
the script requests the resource at the given URL using the
curl command-line tool.
This script requires two environment variables. CACHE_DIR is
the absolute path to the cache directory (which may or may not
exist) whereas LIB_DIR specifies a directory containing various
helper scripts.
Options:
-h Display this message
-v Write verbose messages to stdout
-F Enables "Force Output Mode"
-C Enables "Cache Only Mode"
Option -h is mutually exclusive of all other options.
The default behavior of the script may be modified by using
option -F or -C, which are mutually exclusive. Force Output Mode
outputs the response body even if the server response is 304.
Cache Only Mode bypasses the GET request altogether and goes
directly to cache. If the resource resides in cache, it is
output on stdout, otherwise an error is thrown.
LIBRARY
Environment variable LIB_DIR specifies a directory containing at
least the following library files, which act as helper scripts for
${0##*/}:
$LIB_FILENAMES
EXAMPLES
${0##*/} URL # Retrieve the resource using HTTP conditional GET
${0##*/} -F URL # Enable Force Output Mode
${0##*/} -C URL # Enable Cache Only Mode
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
http_tools.sh
conditional_get.sh"

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

help_mode=false; verbose_mode=false; local_opts=
force_get_mode=false; cache_only_mode=false
while getopts ":hvFC" opt; do
case $opt in
h)
help_mode=true
;;
v)
verbose_mode=true
local_opts="$local_opts -$opt"
;;
F)
force_get_mode=true
cache_only_mode=false
local_opts="$local_opts -$opt"
;;
C)
cache_only_mode=true
force_get_mode=false
local_opts="$local_opts -$opt"
;;
\?)
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

# determine the location of the web resource
shift $(( OPTIND - 1 ))
if [ $# -ne 1 ]; then
echo "ERROR: $script_name: wrong number of arguments: $# (1 required)" >&2
exit 2
fi
location="$1"

$verbose_mode && printf "$script_name using location URL: %s\n" "$location"

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

# determine the cache directory
if [ -z "$CACHE_DIR" ]; then
echo "ERROR: $script_name requires env var CACHE_DIR" >&2
exit 2
fi
if [ ! -d "$CACHE_DIR" ]; then
# think carefully about this...
/bin/mkdir "$CACHE_DIR"
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: $script_name failed to create dir: $CACHE_DIR" >&2
exit $exit_code
fi
fi
$verbose_mode && printf "$script_name using cache directory: %s\n" "$CACHE_DIR"

# 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 (always source command_paths first)
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"

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

# invoke the function
response_body=$(
conditional_get $local_opts -d "$CACHE_DIR" -t "$tmp_dir" "$location"
)
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "ERROR: ${script_name} failed to get resource: $location" >&2
exit $exit_code
fi

if $verbose_mode; then
/bin/cat "$tmp_dir/conditional_get_log"
else
if [ -n "$response_body" ]; then
echo "$response_body"
fi
fi

exit 0
118 changes: 118 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/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/cget.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/command_paths.sh
$script_bin/lib/compatible_date.sh
$script_bin/lib/compatible_mktemp.sh
$script_bin/lib/conditional_get.sh
$script_bin/lib/http_tools.sh
$script_bin/lib/md_tools.sh
$script_bin/lib/extract_entity.xsl
SOURCES

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

0 comments on commit fd2b232

Please sign in to comment.