linkapi.sh

 

#
### linkapi.sh
###
### Usage:
###
###    linkapi.sh linkdir file.html phrase
###
### Create link to file.html in all html files in linkdir that have phrase 
### Run this from the directory where file.html is located
###
### linkapi.sh only looks for matches in one directory.
### See link.sh for a recursive search for matches.
###
### A link is only made if package matches an import statement
### 
 
 
 
 if [ "$3" = "" ]
 then
    echo "Usage: $0 $package linkdir filename phrase"
    exit 1
 fi
 
 
 package=$1
 linkdir=$2
 file=$3
 key1=$4
 key2=$5
 key3=$6
 key4=$7
 

 
 path=`pwd | sed "s/\/usr\/tomcat\/webapps\/ROOT//" | sed "s/\////g"`
 
 
    phrase=$key1
 
    if [ "$key2" != "" ]
    then
       phrase="${key1} ${key2}" 
    fi
    
    if [ "$key3" != "" ]
    then
       phrase="${key1} ${key2} ${key3}"
    fi
    
    if [ "$key4" != "" ]
    then
       phrase="${key1} ${key2} ${key3} ${key4}"
    fi
    
    if [ "$linkdir" = "" ]
    then
       echo "Must enter link directory"
       exit 1
    fi

 

 

cd $linkdir
for i in `ls *.html 2>/dev/null`
do

     if `grep -w "$package" $i >/dev/null 2>/dev/null`
     then
         if `grep -w "$grep" $i >/dev/null 2>/dev/null`
         then
 
             atag=`/usr/local/bin/atag.sh $i $grep`
             if `echo $atag | grep 1 >/dev/null 2>/dev/null`
             then
                 echo "disqualified atag"
                 continue
             fi
     
             titletag=`/usr/local/bin/titletag.sh $i $grep`
             if `echo $titletag | grep 1 >/dev/null 2>/dev/null`
             then
                 echo "disqualified titletag"
                 continue
             fi
     
             h1tag=`/usr/local/bin/h1tag.sh $i $grep`
             if `echo $h1tag | grep 1 >/dev/null 2>/dev/null`
             then
                 echo "disqualified h1tag"
                 continue
             fi
     
     
             Atag=`/usr/local/bin/Atag.sh $i $grep`
             if `echo $Atag | grep 1 >/dev/null 2>/dev/null`
             then
                 echo "disqualified Atag"
                 continue
             fi
     
             TITLEtag=`/usr/local/bin/TITLEtag.sh $i $grep`
             if `echo $TITLEtag | grep 1 >/dev/null 2>/dev/null`
             then
                 echo "disqualified TITLEtag"
                 continue
             fi
     
             H1tag=`/usr/local/bin/H1tag.sh $i $grep`
             if `echo $H1tag | grep 1 >/dev/null 2>/dev/null`
             then
                 echo "disqualified H1tag"
                 continue
             fi
     
     
             xfile=`echo $file | sed "s/\////g"`
         
             p="http://www.setgetweb.com${path}/${xfile}"
     
             echo " "
             echo "Creating link in:"
             echo "    $i"
             echo " "
             echo "using the phrase: "
             echo "    $grep"
     
             echo " "
     
             ###
             ### The syntax below fails if term is on line twice, with one bad term and one good term.
             ### It matches against the good term, and then replaces the bad term.  For example
             ### if term is target, and the line contains "xyztargetxyz" and " target ", it will match
             ### against the latter, and replace the former.  Quel dommage.
             ###
    
             #sed "/[ 	\.,\'\/\"(]$grep[ 	,;\'\"($]/s/$phrase/<a href=\"$p\">$phrase<\/a>/"  $i | \
    

             ###
             ### Here is the compromise syntax
             ###
    
             sed "s/ $grep / <a href=\"$p\">$phrase<\/a> /"  $i | \
             sed "s/ $grep\./ <a href=\"$p\">$phrase<\/a>\./"  | \
             sed "s/ $grep\,/ <a href=\"$p\">$phrase<\/a>\,/"  | \
             sed "s/($grep / <a href=\"$p\">$phrase<\/a>(/"  | \
             sed "s/ $grep(/ <a href=\"$p\">$phrase<\/a>(/"  | \
             sed "s/ $grep=/ <a href=\"$p\">$phrase<\/a>=/"  | \
             sed "s/ $grep$/ <a href=\"$p\">$phrase<\/a>/"  | \
             sed "s/ $grep)/ <a href=\"$p\">$phrase<\/a>)/"  | \
             sed "s/ $grep:/ <a href=\"$p\">$phrase<\/a>:/"  | \
             sed "s/ $grep;/ <a href=\"$p\">$phrase<\/a>;/"  | \
             sed "s/^$grep$/<a href=\"$p\">$phrase<\/a>/"  | \
             sed "s/^$grep /<a href=\"$p\">$phrase<\/a> /"  | \
             sed "s/\.$grep;/\.<a href=\"$p\">$phrase<\/a>;/"  | \
             sed "s/\.$grep\"/\.<a href=\"$p\">$phrase<\/a>\"/"  | \
             sed "s/>$grep<\/td>/><a href=\"$p\">$phrase<\/a><\/td>/"  | \
             sed "s/top>$grep />top><a href=\"$p\">$phrase<\/a> /"  | \
             sed "s/<b>$grep<\/b>/<a href=\"$p\">$phrase<\/a>/"  | \
             sed "s/<br>$grep/<br><a href=\"$p\">$phrase<\/a>/"  | \
             sed "s/<p>$grep/<p><a href=\"$p\">$phrase<\/a>/"  | \
             sed "s/<li>$grep/<li><a href=\"$p\">$phrase<\/a>/"  | \
             sed "s/<code>$grep<\/code>/<a href=\"$p\">$phrase<\/a>/"  | \
             sed "s/<tt>$grep<\/tt>/<a href=\"$p\">$phrase<\/a>/"  | \
             sed "s/<em>$grep<\/em>/<a href=\"$p\">$phrase<\/a>/"  | \
             sed "s/<i>$grep<\/i>/<a href=\"$p\">$phrase<\/a>/"  | \
             sed "s/<tt>$grep<\/tt>/<a href=\"$p\">$phrase<\/a>/"  | \
             sed "s/\/$grep>/\/<a href=\"$p\">$phrase<\/a>>/"  | \
             sed "s/;$grep>/;<a href=\"$p\">$phrase<\/a>>/"  | \
             sed "s/;$grep /;<a href=\"$p\">$phrase<\/a> /"  | \
             sed "s/($grep /(<a href=\"$p\">$phrase<\/a> /"  | \
             sed "s/{$grep}/{<a href=\"$p\">$phrase<\/a>}/"  | \
             sed "s/\/$grep\'/\/<a href=\"$p\">$phrase<\/a>\"/"  | \
             sed "s///\//g"  > tmp.$$
             mv tmp.$$ $i
    
         fi
     fi
 
 done