Skip to content

Instantly share code, notes, and snippets.

@smoser
Created February 26, 2013 17:01
Show Gist options
  • Save smoser/5040116 to your computer and use it in GitHub Desktop.
Save smoser/5040116 to your computer and use it in GitHub Desktop.
brother printer driver downloader
#!/bin/bash
#
# This scraps the $URL below
# and downloads all the .deb (and .ppd) files
#
# originally plan was to somehow integrate it with brother-cups-wraper-extra
# for now, it downloads ~ 230M of data.
URL="http://welcome.solutions.brother.com/bsc/public_s/id/linux/en/download_prn.html"
TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX")
declare -A alt_mod2drv
declare -A alt_drv2mod
dfile="$TEMP_D/data"
altdata="$TEMP_D/short"
modeldata="$TEMP_D/models"
altdir="$TEMP_D/altdata"
wget -q "$URL" -O "$dfile"
set -f
egrep "(AltDriver|Please use)" "$dfile" > "$altdata"
while read line; do
case "$line" in
*AltDriver*)
model="${line#*name=\"}"
model=${model%%\"*}
;;
*Please\ use*)
driver=${line#*href=\"#}
driver=${driver%%\"*}
alt_mod2drv[$model]="$driver"
calt="${alt_drv2mod[$driver]}"
alt_drv2mod[$driver]="${calt:+${calt},}${model}"
echo "driver: $driver model=$model"
;;
esac
done < "$altdata"
sed 's,^\(<tr>\)\(<td><a href="#\),\1\n\2,' "$dfile" |
sed -n 's,^<td><a href="#\([^"]*\).*,\1,p' > "$modeldata"
while read model; do
if [ -n "${alt_mod2drv[$model]}" ]; then
continue
fi
drivers[${#drivers[@]}]="$model"
done < "$modeldata"
out_d="output"
for driver in "${drivers[@]}"; do
xdriver="${driver//\//_}"
xdriver="${xdriver// /}"
models=${alt_drv2mod[$driver]}
[ -n "$models" ] && models="$driver,${models}" || models="$driver"
echo "== $driver [$models] =="
coutd="${out_d}/$xdriver"
mkdir -p "$coutd"
sed -n "/<p><a name=\"$xdriver\">/,/<\/table>/ p" "$dfile" |
sed -n 's,.*href=".*dlfile=\([^&]*\)&.*,\1,p' |
grep -v ".rpm" > "$coutd/downloads"
oifs="$IFS"; IFS=","; for x in $models; do echo $x; done > "$coutd/models";
IFS="$oifs"
echo "$driver" > "$coutd/driver"
while read url; do
out="${coutd}/${url##*/}"
[ -f "$out" ] && { echo "#had $url" 1>&2; continue; }
echo "#download $url" 1>&2
wget -q "$url" -O "$out.tmp" && mv "$out.tmp" "$out" || exit
done < "$coutd/downloads"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment