| 1 |
#!/bin/bash |
|---|
| 2 |
# |{wocdownloader}| |
|---|
| 3 |
# |
|---|
| 4 |
# This script is used by woc |
|---|
| 5 |
|
|---|
| 6 |
# |
|---|
| 7 |
# TODO: |{TODO generic downloader}| |
|---|
| 8 |
# This script should download a file from a generic |
|---|
| 9 |
# URL (http://, ftp://, scp://, file://, cvs://, ...) |
|---|
| 10 |
# However right now it just uses wget. |
|---|
| 11 |
# Do you know if a generic URL downloader exists already? |
|---|
| 12 |
# Remote.py of a-a-p, is a good start, but it should be hacked a bit. |
|---|
| 13 |
# |
|---|
| 14 |
download_files() { |
|---|
| 15 |
local something_downloaded |
|---|
| 16 |
something_downloaded=0 |
|---|
| 17 |
|
|---|
| 18 |
for i in $@ |
|---|
| 19 |
do |
|---|
| 20 |
# Substitute '%s' in the URL |
|---|
| 21 |
u=`echo "$url" | sed -e "s#%s#$i#g"` |
|---|
| 22 |
|
|---|
| 23 |
odir=`echo $i | sed -e 's#[^/]*$##'` |
|---|
| 24 |
if [ ! -z "$odir" -a ! -d "$odir" ] |
|---|
| 25 |
then |
|---|
| 26 |
mkdir $odir |
|---|
| 27 |
fi |
|---|
| 28 |
|
|---|
| 29 |
if [ -f "$i" ] |
|---|
| 30 |
then |
|---|
| 31 |
# file already exists |
|---|
| 32 |
something_downloaded=1 |
|---|
| 33 |
continue |
|---|
| 34 |
fi |
|---|
| 35 |
|
|---|
| 36 |
if ! wget $wget_opt $u -O $i |
|---|
| 37 |
then |
|---|
| 38 |
rm $i |
|---|
| 39 |
else |
|---|
| 40 |
something_downloaded=1 |
|---|
| 41 |
fi |
|---|
| 42 |
done |
|---|
| 43 |
|
|---|
| 44 |
return $something_downloaded |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
# |
|---|
| 48 |
# Note: a valid '%s' token must be specified in the URL (see the doc about |
|---|
| 49 |
# `woc_url') |
|---|
| 50 |
# |
|---|
| 51 |
wget_opt="-q " |
|---|
| 52 |
|
|---|
| 53 |
url="$1" |
|---|
| 54 |
if [ -z "$url" ] |
|---|
| 55 |
then |
|---|
| 56 |
echo "wocdownloader must be called only by woc.vim!" |
|---|
| 57 |
fi |
|---|
| 58 |
|
|---|
| 59 |
shift |
|---|
| 60 |
files="$@" |
|---|
| 61 |
|
|---|
| 62 |
bz2tags_files=".woc/index.woc.bz2 .woc/tags.woc.bz2 .woc/tags.rev.woc.bz2 .woc/tags.bz2 .woc/TAGS.bz2 .woc/cscope.out.bz2" |
|---|
| 63 |
tags_files="index.woc tags.woc tags.rev.woc tags TAGS cscope.out" |
|---|
| 64 |
|
|---|
| 65 |
if [ -z "$WOC_HOME" ] |
|---|
| 66 |
then |
|---|
| 67 |
WOC_HOME="`echo ~/.vim/woc/`" |
|---|
| 68 |
if [ ! -d $WOC_HOME ] |
|---|
| 69 |
then |
|---|
| 70 |
mkdir -p $WOC_HOME |
|---|
| 71 |
fi |
|---|
| 72 |
fi |
|---|
| 73 |
cd $WOC_HOME |
|---|
| 74 |
|
|---|
| 75 |
# Go to the cache |
|---|
| 76 |
if [ ! -d "cache/" ]; then mkdir cache/; fi |
|---|
| 77 |
cd cache |
|---|
| 78 |
|
|---|
| 79 |
# Calculate the md5 of the URL |
|---|
| 80 |
sum=`echo $url | md5sum | awk '{print $1}' 2> /dev/null` |
|---|
| 81 |
if [ -z "$sum" ] |
|---|
| 82 |
then |
|---|
| 83 |
sum=`echo $url | md5 2> /dev/null` # BSD |
|---|
| 84 |
fi |
|---|
| 85 |
|
|---|
| 86 |
# Go to the directory associated to the url |
|---|
| 87 |
if [ ! -d "$sum" ]; then mkdir $sum; fi |
|---|
| 88 |
cd $sum |
|---|
| 89 |
|
|---|
| 90 |
# |
|---|
| 91 |
# Download the tags files |
|---|
| 92 |
# |
|---|
| 93 |
if [ ! -f .woc/.tagsdone ] |
|---|
| 94 |
then |
|---|
| 95 |
if [ ! -d .woc/ ] |
|---|
| 96 |
then |
|---|
| 97 |
mkdir .woc/ |
|---|
| 98 |
fi |
|---|
| 99 |
|
|---|
| 100 |
download_files $bz2tags_files |
|---|
| 101 |
something_downloaded="$?" |
|---|
| 102 |
if [ "$something_downloaded" == 0 ] |
|---|
| 103 |
then |
|---|
| 104 |
download_files $tags_files |
|---|
| 105 |
something_downloaded="$?" |
|---|
| 106 |
else |
|---|
| 107 |
# Uncompress the compressed tags files |
|---|
| 108 |
for i in `ls -1 .woc/*.bz2` |
|---|
| 109 |
do |
|---|
| 110 |
bunzip2 -fk $i |
|---|
| 111 |
inoext=`echo $i | sed -e 's#\.bz2##'` |
|---|
| 112 |
mv $inoext . # move to .woc/../ |
|---|
| 113 |
done |
|---|
| 114 |
fi |
|---|
| 115 |
|
|---|
| 116 |
# Create the dir tree |
|---|
| 117 |
OLD_IFS=$IFS |
|---|
| 118 |
IFS=$'\n' |
|---|
| 119 |
for i in $(cat tags tags.woc tags.rev.woc 2> /dev/null | grep -v \ |
|---|
| 120 |
'^!_TAG' | awk '{print $2}' | grep '/') |
|---|
| 121 |
do |
|---|
| 122 |
d=$(echo $i | sed -e 's#/[^/]*$##') |
|---|
| 123 |
if [ -z "$d" ]; then continue; fi |
|---|
| 124 |
if [ ! -d "./$d" ]; then |
|---|
| 125 |
mkdir -p "./$d" |
|---|
| 126 |
fi |
|---|
| 127 |
done |
|---|
| 128 |
IFS="$OLD_IFS" |
|---|
| 129 |
|
|---|
| 130 |
touch .woc/.tagsdone |
|---|
| 131 |
else |
|---|
| 132 |
something_downloaded=1 |
|---|
| 133 |
fi |
|---|
| 134 |
|
|---|
| 135 |
if [ ! -z "$files" ] |
|---|
| 136 |
then |
|---|
| 137 |
download_files $files |
|---|
| 138 |
something_downloaded="$?" |
|---|
| 139 |
fi |
|---|
| 140 |
|
|---|
| 141 |
if [ "$something_downloaded" == 1 ] |
|---|
| 142 |
then |
|---|
| 143 |
echo `pwd` # we are in $sum/ |
|---|
| 144 |
exit 0 |
|---|
| 145 |
else |
|---|
| 146 |
cd .. |
|---|
| 147 |
rm -r $sum |
|---|
| 148 |
exit 1 |
|---|
| 149 |
fi |
|---|