diff options
| author | Eric Anderson <ejona86@gmail.com> | 2013-06-15 17:51:38 -0700 | 
|---|---|---|
| committer | Eric Anderson <ejona86@gmail.com> | 2013-06-15 17:56:35 -0700 | 
| commit | 6c9e8d2a34fe5f4965494c292b359edb2843e64c (patch) | |
| tree | e4520e38c6e8e0a44894679d51d568b9dde21417 | |
| parent | 3a80e4a5832d63417aad5634c464f16b172d0893 (diff) | |
| download | hib-dlagent-6c9e8d2a34fe5f4965494c292b359edb2843e64c.tar.gz hib-dlagent-6c9e8d2a34fe5f4965494c292b359edb2843e64c.zip | |
Refactor download location handling
We now handle many more cases and show clear error messages when using
conflicting features.
| -rwxr-xr-x | hib-dlagent | 125 | 
1 files changed, 82 insertions, 43 deletions
| diff --git a/hib-dlagent b/hib-dlagent index 84f8898..5afc79d 100755 --- a/hib-dlagent +++ b/hib-dlagent @@ -9,7 +9,6 @@ COOKIE_JAR=  FILE=  DESTINATION=  DOWNLOAD=1 -LISTING_PAGES=()  USERNAME=  PASSWORD=  KEYS=() @@ -32,13 +31,15 @@ usage() {    echo    echo "Usage: $0 [OPTIONS] FILE"    echo "Options:" -  echo " -d         Storage directory to look first" +  echo " -d <dir>   Directory for searching and saving files. The normal save" +  echo "            location is still used, but will be a symbolic link"    echo " -h         This help"    echo " -k <key>   Search key's files. Use multiple times for multiple keys"    echo " -o <file>  Name to use when saving file"    echo " -p <pass>  Use pass to login. If specified multiple times, the last is"    echo "            used" -  echo " -s         Print URL to stdout instead of downloading" +  echo " -s         Print URL to stdout instead of downloading. Incompatible" +  echo "            with -d"    echo " -u <user>  Use user to login. Search account's files. If specified"    echo "            multiple times, the last is used"    echo @@ -48,6 +49,47 @@ usage() {    echo "specify -k for bundles associated with an account."  } +handle_download() { +  local SAVE_FILE="$1" +  local LISTING_PAGES=() + +  COOKIE_JAR=$(mktemp) + +  if [ -n "$USERNAME" ]; then +    login +    LISTING_PAGES+=("$HOME_PAGE") +  fi + +  for KEY in "${KEYS[@]}"; do +    LISTING_PAGES+=("https://www.humblebundle.com/downloads?key=$KEY") +  done + +  if [ -z "$LISTING_PAGES" ]; then +    echo "You must specify at least one of -u and -k" >&2 +    exit 1 +  fi + +  local URL +  for LISTING_PAGE in "${LISTING_PAGES[@]}"; do +    URL=$(discover_url "$LISTING_PAGE") +    if [ -n "$URL" ]; then break; fi +  done + +  # Cookie no longer necessary. +  rm "$COOKIE_JAR" + +  if [ ! -n "$URL" ]; then +    echo "Could not find URL for file: $FILE" >&2 +    exit 2 +  fi + +  if [ $DOWNLOAD -eq 0 ]; then +    echo "$URL" +  else +    /usr/bin/curl -C - --retry 3 --retry-delay 3 -o "$SAVE_FILE" "$URL" +  fi +} +  main() {    if [ $# -eq 0 ]; then      usage @@ -96,57 +138,54 @@ main() {      exit 1    fi -  COOKIE_JAR=$(mktemp) -  FILE="${1#hib://}" - -  if [ -z "$DESTINATION" ]; then -    DESTINATION="${FILE##*/}" +  if [ $DOWNLOAD -eq 0 -a -n "$STORAGE" ]; then +    echo "-s and -d are incompatible" >&2 +    exit 1    fi -  if [ -n "$STORAGE" ]; then -    STORAGE_FILE=$(/usr/bin/find "$STORAGE" -name "$FILE") -    if [ -n "$STORAGE_FILE" ]; then -      echo "Found file: $STORAGE_FILE" -      ln -s "$STORAGE_FILE" "$DESTINATION" -      return; -    fi -  fi +  FILE="$1" -  if [ -n "$USERNAME" ]; then -    login; -    LISTING_PAGES+=("$HOME_PAGE") +  if [ "$FILE" != "$(basename "$FILE")" ]; then +    # We need a simple file name without any slashes. Instead of erroring, we +    # strip the file name out so that hib://filename is a valid argument. +    FILE=$(basename "$FILE") +    # Since we are doing something fancy, give the user a clue that we modified +    # their input. +    echo "Searching for $FILE" >&2    fi -  for KEY in "${KEYS[@]}"; do -    LISTING_PAGES+=("https://www.humblebundle.com/downloads?key=$KEY") -  done - -  if [ -z "$LISTING_PAGES" ]; then -    echo "You must specify at least one of -u and -k" >&1 -    exit 1 +  if [ -z "$DESTINATION" ]; then +    DESTINATION="."    fi -  for LISTING_PAGE in "${LISTING_PAGES[@]}"; do -    URL=$(discover_url "$LISTING_PAGE") -    if [ -n "$URL" ]; then break; fi -  done - -  # Cookie no longer necessary. -  rm "$COOKIE_JAR" - -  if [ ! -n "$URL" ]; then -    echo "Could not find URL for file: $FILE" >&2 -    exit 2 +  if [ -d "$DESTINATION" ]; then +    DESTINATION="$DESTINATION/$FILE"    fi -  if [ $DOWNLOAD -eq 0 ]; then -    echo "$URL" +  if [ -z "$STORAGE" ]; then +    handle_download "$DESTINATION"    else -    if [ -n "$STORAGE" ]; then -      ln -s "$STORAGE/$FILE" "$DESTINATION" -      DESTINATION="$STORAGE/$FILE" +    local STORAGE_FILE +    STORAGE_FILE=$(/usr/bin/find "$STORAGE" -name "$FILE") +    if [ -z "$STORAGE_FILE" ]; then +      STORAGE_FILE="$STORAGE/$FILE" +      handle_download "$STORAGE_FILE" +    fi + +    if [ ! "$STORAGE_FILE" -ef "$DESTINATION" ]; then +      ln -s "$STORAGE_FILE" "$DESTINATION" +      if [ ! "$STORAGE_FILE" -ef "$DESTINATION" ]; then +        # If DESTINATION is in a different directory than the CWD and STORAGE +        # is a relative path, then ln -s will produce a broken link. To produce +        # a valid link we resolve the path with readlink. This is unfortunate +        # because having relative links is nicer and we will undo any symlinks +        # in the path. Therefore, we detect when things have broken and only +        # use readlink when we must. +        STORAGE_FILE=$(/usr/bin/readlink -f "$STORAGE_FILE") +        rm "$DESTINATION" +        ln -s "$STORAGE_FILE" "$DESTINATION" +      fi      fi -    /usr/bin/curl -C - --retry 3 --retry-delay 3 -o "$DESTINATION" "$URL"    fi  } | 
