Skip to content

Commit

Permalink
Modified URL resolution method
Browse files Browse the repository at this point in the history
  • Loading branch information
lelegard committed Aug 5, 2018
1 parent c3666bb commit 85a2217
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 22 deletions.
28 changes: 6 additions & 22 deletions build-dektec-dkms
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ INSTALL=false
export KEEP=false
PREPARE_ONLY=false
UNINSTALL=false
URL="https://www.dektec.com/products/SDK/DTAPI/Downloads/LatestLinuxSDK"
VERBOSE=false
PATCH_OPTION="--silent"

Expand Down Expand Up @@ -94,11 +93,6 @@ Options:
uninstall a package. This is useful on unsupported systems (no .rpm,
no .deb).
-u "url"
--url "url"
URL of the Dektec LinuxSDK. Default:
$URL
-v
--verbose
Display verbose information.
Expand Down Expand Up @@ -151,10 +145,6 @@ while [[ $# -gt 0 ]]; do
--uninstall)
UNINSTALL=true
;;
-u|--url)
[[ $# -gt 1 ]] || usage; shift
URL=$1
;;
-v|--verbose)
VERBOSE=true
PATCH_OPTION=
Expand All @@ -172,11 +162,7 @@ DTAPI_H="$SDKDIR/DTAPI/Include/DTAPI.h"
PKGDIR="$SCRIPTDIR/packages"
export TMPDIR="$SCRIPTDIR/tmp"

# The download URL is a generic one ending in ../LatestLinuxSDK.
# The actual file is redirected as ../LinuxSDK_v5.06.2017.0.tar.gz for instance.
# The file VERSION is created to contain the actual file name.

TARBASE="$SCRIPTDIR/LinuxSDK"
# Text file containing the name of the LinuxSDK tarball.
VERSFILE="$SCRIPTDIR/VERSION"

#-----------------------------------------------------------------------------
Expand All @@ -185,7 +171,7 @@ VERSFILE="$SCRIPTDIR/VERSION"

if $CLEAN; then
verbose "cleaning up non-original files"
rm -rf "${TARBASE}*" "$SDKDIR" "$TMPDIR" "$VERSFILE"
rm -rf "$SCRIPTDIR"/LinuxSDK* "$SDKDIR" "$TMPDIR" "$VERSFILE"
cleanexit 0
fi

Expand All @@ -194,20 +180,18 @@ fi
#-----------------------------------------------------------------------------

# Download the LinuxSDK if not present.
[[ -n "$URL" ]] || error "empty URL specified"
TARNAME=$(cat "$VERSFILE" 2>/dev/null)
TARBALL="$SCRIPTDIR/$TARNAME"
if $FORCE || [[ -z "$TARNAME" ]] || [[ ! -e "$TARBALL" ]]; then
URL=$($SCRIPTDIR/get-dektec-linux-sdk-url.sh)
[[ -n "$URL" ]] || error "got empty URL"
verbose "downloading $URL"
mkdir -p "$TMPDIR"
# Do not download/redirect, just get the HTTP headers
curl -D "$TMPDIR/HEADERS" "$URL" || cleanexit 1
TARNAME=$(grep -i Location: "$TMPDIR/HEADERS" | sed -e 's/.*: *//' -e 's/.*\///' -e 's/\r//g')
TARNAME=$(basename "$URL")
TARBALL="$SCRIPTDIR/$TARNAME"
[[ -n "$TARNAME" ]] || error "relocation header not found"
verbose "actual file name: $TARNAME"
echo "$TARNAME" >"$VERSFILE"
curl -L "$URL" -o "$TARBALL" || cleanexit 1
curl --location "$URL" -o "$TARBALL" || cleanexit 1
fi

# Get the Linux SDL version from the file name.
Expand Down
89 changes: 89 additions & 0 deletions get-dektec-linux-sdk-url.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash
#-----------------------------------------------------------------------------
#
# TSDuck - The MPEG Transport Stream Toolkit
# Copyright (c) 2005-2018, Thierry Lelegard
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
#
#-----------------------------------------------------------------------------
#
# This script retrieves the URL of the latest LinuSDK from Dektec.
#
#-----------------------------------------------------------------------------

URL_BASE=https://www.dektec.com
HTML_URL=$URL_BASE/downloads/SDK/
GENERIC_URL=$URL_BASE/products/SDK/DTAPI/Downloads/LatestLinuxSDK

SCRIPT=$(basename $BASH_SOURCE)
error() { echo >&2 "$SCRIPT: $*"; exit 1; }

# Merge an URL with its base.
merge_url()
{
local ref="$1"
local url
read url

if [[ -n "$url" ]]; then
if [[ $url == *:* ]]; then
echo "$url"
elif [[ $url == /* ]]; then
echo "$URL_BASE$url"
elif [[ $ref == */ ]]; then
echo "$ref$url"
else
ref=$(dirname "$ref")
echo "$ref/$url"
fi
fi
}

# Retrieve the URL using the redirection from a fixed generic URL.
# This should be the preferred method but Dektec may forget to update
# the redirection in the generic URL.
from_generic_url()
{
curl --silent --show-error --dump-header /dev/stdout "$GENERIC_URL" | \
grep -i 'Location:' | \
sed -e 's/.*: *//' -e 's/\r//g' | \
merge_url "$GENERIC_URL"
}

# Retrieve the URL by parsing the HTML from the Dektec download web page.
from_html_page()
{
curl --silent --show-error --location "$HTML_URL" | \
grep 'href=".*LinuxSDK' | \
sed -e 's/.*href="//' -e 's/".*//' | \
merge_url "$HTML_URL"
}

# Try the HTML parsing first, then redirection.
URL=$(from_html_page)
[[ -z "$URL" ]] && URL=$(from_generic_url)
[[ -z "$URL" ]] && error "cannot locate LinuxSDK location from Dektec"

echo "$URL"
exit 0

0 comments on commit 85a2217

Please sign in to comment.