All pastes #2065920 Raw Edit

Something

public text v1 · immutable
#2065920 ·published 2011-05-20 18:04 UTC
rendered paste body
#! /bin/sh
#set -x

# TOOLS NEEDED
# SED
# CURL
# TEMPFILE
# TOUCH
# RM
# GREP
# CAT
# MKDIR
# LS
# TAIL

URL="http://www.xkcd.com"
CONFDIR="${CONFIG:="$HOME/.xkcd/test"}"
CONF="details"
FIRST_RUN="0"
LINK="/tmp/xkcd_image"
KEEP="30"

RM=$(which rm) || { echo 1>&2 "we require the rm utility, exiting ..."; exit 1; }
CURL=$(which curl) || { echo 1>&2 "we require the curl utility, exiting ..."; exit 1; }
SED=$(which sed) || { echo 1>&2 "we require the sed utility, exiting ..."; exit 1; }
TCH=$(which touch) || { echo 1>&2 "we require the touch utility, exiting ..."; exit 1; }
GREP=$(which grep) || { echo 1>&2 "we require the grep utility, exiting ..."; exit 1; }
LN=$(which ln) || { echo 1>&2 "we require the ln utility, exiting ..."; exit 1; }
CAT=$(which cat) || { echo 1>&2 "we require the cat utility, exiting ..."; exit 1; }
MKDIR=$(which mkdir) || { echo 1>&2 "we require the mkdir utility, exiting ..."; exit 1; }
LS=$(which ls) || { echo 1>&2 "we require the ls utility, exiting ..."; exit 1; }
TAIL=$(which tail) || { echo 1>&2 "we require the tail utility, exiting ..."; exit 1; }


getOldest(){

  if [ $# -ne 1 ]; then
    return 1
  fi

  case "$1" in [0-9]|[0-9][0-9]*)
    :
    ;;
    *)
    return 2
    ;;
  esac
  
  if [ $($LS -t *.png *.PNG 2>/dev/null| wc -l) -gt "$1" ]; then
    # yes there is TTCTTO race here 
    $LS -t *.png *.PNG 2>/dev/null| $TAIL +"$(($1+1))" || return 1
  else
    return 1
  fi

}


which tempfile &>/dev/null || { 
tempfile(){
  # this should suffice since we only need one tempfile
  local tmp=/tmp/"$(basename "$0")".$$
  $TCH "$tmp" || return 1
  echo "$tmp"
}
}

getOptFromConfig()
{

  local answer
  [ $# -ne 1 ] && return 2

  $GREP -q "$1" "$CONFDIR"/"$CONF" 2>/dev/null && { 

  answer="$($GREP "$1" "$CONFDIR"/"$CONF" | $SED -e 's/^[^=][^=]*=\(.*\)$/\1/g' -e 's/\ \ *$//g')"

  if  [ ! -n "$answer" -o x"$answer" = x"" ]; then
    return 1
  else
    echo "$answer"
  fi

} || return 1

}

TMPF="$(tempfile)"

trap "$RM -f "$TMPF"" KILL QUIT EXIT ABRT HUP INT STOP

if [ ! -f "$TMPF" ]; then
  echo 1>&2 "tempfile: $TMPF is not a regular file"
  exit 1
fi

if [ ! -d "$CONFDIR" ]; then
  $MKDIR "$CONFDIR" || { echo 1>&2 "configdir: $CONFDIR is not a directory or not writeable"
  exit 1
}
fi


if [ ! -f "$CONFDIR"/"$CONF" ]; then
  $TCH "$CONFDIR"/"$CNF"
  FIRST_RUN=1
fi


if [ "$FIRST_RUN" -ne 1 ]; then
  FIRST_RUN=0
  ID=$(getOptFromConfig "ID")  || FIRST_RUN=1
  FILE=$(getOptFromConfig "FILE")  || FIRST_RUN=1
fi

if [ x"$ID" = x"" ]; then ID=0; fi
if [ x"$FILE" = x"" ]; then FILE="null"; fi

# DUMP PAGE

$CURL -s -o "$TMPF" "$URL" || { echo 1>&2 "something went wrong while dumping the file"; exit 1; }

NEW_ID="$($GREP '<h3>Permanent\ link\ to\ this\ comic:' < "$TMPF" | $SED -e 's/[^:]*:\ \(.*\)$/\1/' -e 's/^\ \ *//' -e 's/\ \ *$//' -e 's/<\/*h3>//g' -e 's/\/$//' -e 's/^/\//' -e 's/[.:]//g')"
NEW_ID="$(basename "$NEW_ID")"

FILE_URL="$($GREP '<h3>Image\ URL' < "$TMPF" | $SED -e 's/[^:]*:\ \(.*\)$/\1/' -e 's/^\ \ *//' -e 's/\ \ *$//' -e 's/<\/*h3>//g' -e 's/\/$//' )"

NEW_FILE="$(echo "$FILE_URL" | $SED -e 's/^/\//' -e 's/://g' -e 's/\/$//')"
NEW_FILE=$(basename "$NEW_FILE")

## HERE WE NEED TO DETERMINE IF FILE IS NEW
## CHECK FOR EITHER NEW_ID OR FILE

if [ "$NEW_ID" -le "$ID" -o x"$NEW_FILE" = x"$FILE" ];
then
  exit 0
fi

cd "$CONFDIR" && $CURL -s -o "$NEW_FILE" "$FILE_URL"
if [ -h "$LINK" ]; then
  $RM -f "$LINK" 
fi
$LN -s "$CONFDIR"/"$NEW_FILE" "$LINK"

$CAT > "$CONFDIR"/"$CONF" << EOF
ID=$NEW_ID
FILE=$NEW_FILE
EOF

# REMOVE THE OLDEST MINUS KEEP (AMOUNT WE WANT TO KEEP)

getOldest "$KEEP" | while read f; do 
if [ x"$f" != x"" -a -f "$f" ]; then
  $RM -f "$f"
fi
done

#set +x