#!/bin/sh # # Script de gestion d'URL - ©Thomas Nemeth, 03.2002 #################################### # # VARIABLES # #################################### # Variables par défaut : modifications autorisées # (mettre un & après le navigateur s'il ne se met # pas automatiquement en background). BROWSER="nav" BOOKSD=.bookmarks BOOKSF=.bookmarks.html LINKTO="" COPYTO="" OWNER=`(echo -n "$USER" | sed -e 's/^\(.\).*/\1/' | tr "[:lower:]" "[:upper:]") && echo "$USER" | sed -e 's/.//'` CONFIGFILE=$HOME/.bmrc if [ -f $CONFIGFILE ]; then . $CONFIGFILE fi if [ "$TODAY" = "" ]; then TODAY=`date "+%A %d %B %Y"` fi # Variables internes : NE PAS TOUCHER # VERSION="0.5.0" BMDIR=$HOME/$BOOKSD BMFILE=$HOME/$BOOKSF NOTGOOD="BROKEN" KEY="" URL="" COM="" FILE="" NAME="" GOOD="" UPDATE=0 TIMEOUT=60 WAIT=6 EXCLUDE="" # Mode debogage : options -d et -v # DEBUG=0 VERBOSE=0 #################################### # # FONCTIONS DIVERSES # #################################### do_badkey () { if [ "$KEY" = "" ]; then do_usage "Missing argument KEY" exit fi } # Fonction de débogage # do_debug () { if [ $DEBUG = 1 ]; then echo "> $1" fi } do_very_verbose () { if [ $VERBOSE = 2 -o $DEBUG = 1 ]; then echo "$1" fi } do_verbose () { if [ $VERBOSE != 0 -o $DEBUG = 1 ]; then echo "$1" fi } # Fonctions de traitement # do_search_first () { do_very_verbose "Searching into bookmarks tree files..." #FILE=`find $BMDIR -name "*.html" | grep $KEY | head -n 1` FILE="" for i in `find $BMDIR/ -name "*.html"`; do gn=`echo "$i" | grep $KEY` gc=`grep "$KEY" "$i"` if [ "$FILE" = "" -a "$gn" != "" ]; then FILE="$i" elif [ "$FILE" = "" -a "$gc" != "" ]; then FILE="$i" fi if [ "$FILE" != "" ]; then break; fi done } do_search_in_dir () { do_very_verbose "Looking in dir for $KEY" if [ "$1" = "-d" ]; then FILE=`find $BMDIR/ -type d | grep $KEY | head -n 1` elif [ "$1" = "-k" ]; then FILE=`find $BMDIR/ -name "*.html" | grep $KEY` do_debug "Found element(s) : $FILE" if [ "$FILE" != "" ]; then ELEMENTS=$FILE FILE="" for elt in $ELEMENTS; do KEYELT=`echo "$elt" | xargs -i XX basename XX '.html'` do_debug "Got $KEYELT" if [ "$FILE" = "" ]; then FILE=$KEYELT else FILE="$FILE $KEYELT" fi done fi else do_search_first fi } do_search_in_file () { do_very_verbose "Looking in file for $KEY" if [ "$1" = "-d" ]; then FILE=`grep -E ".*(.*$KEY[^/]*)" $BMFILE` elif [ "$1" = "-k" ]; then FILE=`grep -E "^ *
[^<]*$KEY[^<]*" $BMFILE` if [ "$FILE" != "" ]; then ELEMENTS=`echo "$FILE" | wc -l | awk '{print $1}'` do_debug "Elements number : $ELEMENTS" FILE="" elt=0 while [ $elt != $ELEMENTS ]; do elt=`expr $elt + 1` line=`grep -E "^ *
[^<]*$KEY[^<]*" $BMFILE \ | head -n $elt \ | tail -n 1` do_debug "Line : $line" KEYELT=`echo "$line" | sed -e 's/^ *
//g' -e 's/ (.*//g'` do_debug "Got $KEYELT" if [ "$FILE" = "" ]; then FILE=$KEYELT else FILE="$FILE $KEYELT" fi do_debug "Total keys : $FILE" done fi else FILE=`grep "^ *
.*$KEY.* (" $BMFILE | head -n 1` fi } do_search () { do_debug "Looking for $KEY" if [ -f $BMFILE ]; then do_search_in_file $1 else do_search_in_dir $1 fi do_debug "Found : [$FILE]" if [ "$FILE" = "" ]; then echo "Could not find key '$KEY' in bookmarks." fi } do_geturl () { if [ "$FILE" != "" ]; then do_debug "Getting url in [$FILE]" if [ -f $BMFILE ]; then NAME=`echo "$FILE" | sed -e 's§.*href=.*>\(.*\).*§\1§'` URL=`echo "$FILE" | sed -e 's§.*.*.*§\1§'` else NAME=`sed -e 's§.*>\(.*\)§\1§' $FILE ` URL=`sed -e 's§.*§\1§' $FILE ` fi fi do_debug "$KEY => $NAME : $URL" } do_browse () { if [ "$URL" != "" ]; then do_debug "Launching $BROWSER $URL" else do_debug "Not launching $BROWSER : no URL" fi if [ $DEBUG = 0 -a "$URL" != "" ]; then do_verbose "Opening $URL" $BROWSER $URL sleep $WAIT fi } do_testurl () { GOOD=`lynx -dump -connect_timeout=$TIMEOUT -head $URL 2> /dev/null \ | grep "200 OK"` if [ "$GOOD" != "" ]; then GOOD="X" else GOOD=" " fi } do_extractkey () { do_verbose "Extracting key from URL $URL..." ishtml=`echo "$URL" | grep '.htm'` if [ "$ishtml" != "" ]; then KEY=`echo "$URL" | sed -e 's§.*/\([^/]*\)\.htm.*$§\1§'` if [ "$KEY" = "index" ]; then KEY=`echo "$URL" \ | sed -e 's§http:..\([^/]*\).*/\([^/]*\)\.htm.*$§\1-\2§'` fi else tmpstr=`echo "$URL" | sed \ -e 's/http:..//' \ -e 's/ftp:..//' \ -e 's§/§ §g' \ -e 's/www\.//g' \ -e 's§/$§§' \ -e 's/\?//g' \ -e 's/=//g' \ -e 's/;//g' \ -e 's/,//g' \ -e 's/~//g' \ -e 's/\.php.*$//' \ -e 's/^ //'` do_debug "$tmpstr" nbm=`echo "$tmpstr" | wc -w` prem=`echo "$tmpstr" | cut -d " " -f 1` dern=`echo "$tmpstr" | cut -d " " -f $nbm` do_debug "$nbm ==> $prem $dern" if [ "$prem" != "$dern" ]; then KEY="$prem-$dern" else KEY="$prem" fi fi } # Fonctions relatives aux commandes # do_open () { do_verbose "Open $KEY" do_badkey do_search do_geturl do_browse } do_opendir () { do_verbose "Opendir $KEY" do_badkey do_search -d LIST=$FILE if [ -f $BMFILE ]; then echo "$LIST" | while read line; do FILE=$line do_geturl do_browse done else for FILE in $LIST/*.html; do do_geturl do_browse done fi } do_show () { do_debug "Display $KEY" do_badkey do_search do_geturl echo "$URL" } do_display () { do_debug "Show $KEY" do_badkey do_search do_geturl echo "Bookmark for $KEY :" echo "Title : $NAME" echo "URL : $URL" echo "Comment : $COM" } do_find () { do_verbose "Find $KEY" do_badkey do_search -k if [ "$FILE" != "" ]; then echo "KEYS : $FILE" fi } do_update_dir () { do_verbose "Entering $1..." LASTCAT=`echo "$1" | sed -e 's,^.*/\([^/]*\)$,\1,'` if [ "$1" != "$BMDIR" ]; then echo "

$LASTCAT

" >> $BMFILE else echo "

Bookmarks for $OWNER

" >> $BMFILE fi echo "

" >> $BMFILE GOOD="YES" for SUBDIR in `find $1/ -type d -mindepth 1 -maxdepth 1 | sort`; do if [ "$EXCLUDE" != "" ]; then GOOD="YES" LASTPART=`basename $SUBDIR` for XDIR in $EXCLUDE; do do_very_verbose "Testing excluded dir $XDIR with $LASTPART..." if [ "$XDIR" = "$LASTPART" ]; then GOOD="NO" fi do_very_verbose "$GOOD take it" done fi if [ "$GOOD" = "YES" ]; then do_update_dir $SUBDIR fi done for FILE in `ls -1 $1/*.html 2> /dev/null`; do do_very_verbose "Working on $FILE..." DATA=`grep href $FILE` do_debug "DATA : $DATA" KEY=`echo "$FILE" | xargs basename | sed -e 's/.html//g'` do_debug "KEY : $KEY" CAT=`echo "$FILE" | sed -e "s§$BMDIR/§§g" -e "s§/$KEY.html§§g"` do_debug "CAT : $CAT" echo "

$KEY ($CAT) $DATA" >> $BMFILE done echo "
" >> $BMFILE echo "
" >> $BMFILE do_verbose "Closing $1..." } do_update () { if [ -d $BMDIR ]; then do_verbose "Updating $BMFILE" else echo "Error : $BMDIR does not exist..." exit fi EXCLUDE="$KEY $*" OLDCAT="" LEVEL=0 PREVLEVEL=0 echo "" > $BMFILE echo " " >> $BMFILE echo " Bookmarks for $OWNER" >> $BMFILE echo " " >> $BMFILE echo " " >> $BMFILE do_update_dir $BMDIR echo " " >> $BMFILE echo "" >> $BMFILE if [ "$LINKTO" != "" ]; then for alink in $LINKTO; do do_verbose "Linking $BMFILE to $alink..." ln -f $BMFILE $alink done fi if [ "$COPYTO" != "" ]; then for afile in $COPYTO; do do_verbose "Copying $BMFILE to $afile..." cp -f $BMFILE $afile done fi } do_htmlhead () { SUBTITLE="$1" if [ "$1" = "Bookmarks for $OWNER" ]; then SUBTITLE="main index" fi cat << EOF > $2 Bookmarks for $OWNER : $SUBTITLE

$1


EOF } do_htmlfoot () { cat << EOF >> $1

Page last modified on $TODAY
Page created with url $VERSION. EOF } do_export_files () { NBEF=`ls -1 $1/*.html 2> /dev/null | wc -l | awk '{print $1}'` if [ $NBEF != 0 ]; then echo "

" >> $2 echo " Entries :" >> $2 echo "

" >> $2 fi } do_export_dir () { do_verbose "Entering $1 : $2..." LASTCAT=`echo "$1" | sed -e 's,^.*/\([^/]*\)$,\1,'` if [ "$1" != "$BMDIR" ]; then do_htmlhead $LASTCAT $2 CAT=`echo "$1" | sed -e "s§$BMDIR/§§" -e 's§/$§§' -e "s§/§ -> §g"` do_debug "Full category : $CAT" else do_htmlhead "Bookmarks for $OWNER" $2 fi NBSD=`find $1/ -type d -mindepth 1 -maxdepth 1 | wc -l | awk '{print $1}'` if [ $NBSD != 0 ]; then echo "

" >> $2 echo " Sub-categories :" >> $2 echo "

" >> $2 fi do_export_files $1 $2 do_htmlfoot $2 do_verbose "Closing $1..." } do_export () { do_badkey if [ -d $BMDIR ]; then do_verbose "Exporting $BMDIR to $KEY..." else echo "Error : $BMDIR does not exist..." exit fi EXCLUDE="$*" OLDCAT="" LEVEL=0 PREVLEVEL=0 DESTDIR=$KEY if [ ! -d $DESTDIR ]; then mkdir $DESTDIR || exit fi do_export_dir $BMDIR $DESTDIR/index.html } do_add () { URL="$1" CAT="$2" COM="$3" doupdate="$4" if [ "$CAT" = "" ]; then echo 'Warning : category not given !' echo "Using $BMDIR to store datas..." fi bkspec=`echo "$CAT" | grep "$BOOKS"` if [ "$bkspec" = "" ]; then CAT="$BMDIR/$CAT" fi if [ -f $BMFILE -a "$doupdate" != "no-update" ]; then UPDATE=1 fi do_verbose "Add url \"$URL\" to category \"$CAT\"" do_very_verbose "Other arguments : COM=$COM doupdate=$doupdate" do_badkey # Création du nom de fichier FILE=$CAT/$KEY.html num=1 while [ -f $FILE ]; do num=`expr $num + 1` FILE="${FILE}-$num" done # Création du nom du lien if [ "$COM" = "" ]; then NAME=`lynx -connect_timeout=$TIMEOUT -source $URL 2> /dev/null \ | grep "" | sed -e 's§<title>\(.*\)§\1§'` if [ "$NAME" = "" ]; then NAME="$KEY - $URL" fi else NAME="$COM" fi # Création du fichier-lien if [ $DEBUG = 0 ]; then mkdir -p $CAT 2> /dev/null echo "$NAME" > $FILE else echo "mkdir -p $CAT 2> /dev/null" echo "Write $NAME in $FILE" fi if [ $UPDATE = 1 ]; then do_update fi } do_verify () { do_verbose "Verify all URLs" for FILE in `find $BMDIR/ -name "*.html"`; do do_geturl do_testurl if [ "$GOOD" != "X" ]; then NEWFILE=`echo "$FILE" | sed -e "s§\.html§-$NOTGOOD\.html§"` mv $FILE $NEWFILE fi done nb=`find $BMDIR/ -name "*-$NOTGOOD.html" | wc -l` if [ $nb -gt 0 ]; then echo "Found $nb broken URLs." else echo "Did not find any broken URL." fi } do_import () { do_verbose "Import $KEY" do_badkey test=1 if [ "$1" = "notest" ]; then test=0 fi GOOD="X" CAT=$BMDIR sep="/" titre="" decalage="" IMPORT=$KEY KEY="" do_verbose " BOOKMARKS FOR $OWNER" cat $IMPORT | while read line; do # Détection des éléments # Titre de section tsection=`echo "$line" | grep "

"` # Fin de section fsection=`echo "$line" | grep ""` # Entrée d'URL usection=`echo "$line" | grep "

" | grep -v "]*>\(.*\)§\1§' \ -e 's/é/é/g' \ -e 's/è/è/g' \ -e 's/\&/et/g' \ -e 's/ - /-/g' \ -e "s/'/-/g" \ -e 's/ /-/g' \ -e 's§/§-§g' \ -e 's/\$//g'` CAT="$CAT$sep$titre" do_verbose "$decalage-$titre ($CAT)" fi # Traitement d'une URL if [ "$msection" != "" ]; then if [ "$usection" = "" -a "$tsection" = "" ]; then URL=`echo "$line" | sed -e 's,.*]*>\([^<]*\),\1,'` KEY=`echo "$line" | sed -e 's,.*
\([^ ]*\) .*,\1,'` elif [ "$usection" != "" ]; then URL=`echo "$line" \ | sed -e 's§
§\1§'` COM="" fi if [ $test = 1 ]; then do_testurl fi if [ "$usection" != "" -a "$GOOD" = "X" ]; then do_extractkey elif [ "$GOOD" != "X" ]; then KEY="/dev/null" fi if [ "$KEY" != "/dev/null" ]; then tmpverbose=$VERBOSE VERBOSE=0 do_add $URL $CAT "$COM" "no-update" VERBOSE=$tmpverbose fi do_verbose "$decalage-[$GOOD] $KEY ($COM) : $URL" fi # Affichage et fin de section if [ "$dsection" != "" ]; then decalage="$decalage |" fi if [ "$fsection" != "" ]; then CAT=`echo "$CAT" | sed -e 's§\(.*\)/[^/]*$§\1§'` decalage=`echo "$decalage" | sed -e 's/..$//'` echo "$decalage" fi done if [ -f $BMFILE ]; then do_update fi } #################################### # # AIDE EN LIGNE # #################################### do_help_open () { echo " url open KEY :" echo " open the URL specifyed by KEY in the default browser." echo " The default browser is configured within the 'url' script." } do_help_opendir () { echo " url opendir KEY :" echo " open all the URLs in the directory specifyed by KEY in the" echo " default browser." echo " The default browser is configured within the 'url' script." } do_help_show () { echo " url show KEY :" echo " show the URL of the bookmark that matches KEY." } do_help_display () { echo " url display KEY :" echo " display informations about the URL that matches KEY." } do_help_find () { echo " url find KEY :" echo " show all the keys that matches KEY." } do_help_add () { echo " url add KEY URL CATEGORY [comment] :" echo " add an 'URL' in 'CATEGORY' with key 'KEY' and an optional" echo " argument 'comment' describing the given URL." echo " The category can be preceded by :" echo " $BMDIR or $BOOKSD" echo " (if the current dir is ~) in order to use the shell's" echo " completion facility." echo " eg : url add google 'http://www.google.com' $BOOKSD/Search" echo " Comments are not taken in count if you use a bookmark file." } do_help_verify () { echo " url verify :" echo " Verify broken URLs." echo " Broken URLs have their bookmark files' names added a" echo " -$NOTGOOD.html end." } do_help_import () { echo " url import KEY [notest] :" echo " import bookmarks from netscape or mozilla (specified as" echo " KEY), and verify their validity if argument 'notest' is" echo " not given." } do_help_update () { echo " url update [exclude-dir ...] :" echo " update $BMFILE with $BMDIR" echo " hierarchy. Optional arguments 'exclude-dir' are directories" echo " you do not want to be included in your bookmark file. These" echo " are space-separated directory names. Each directory that" echo " matches one of these names will not be included no more" echo " than its sub-directories (do not use full paths : only the" echo " last path word will be tested)." } do_help_export () { echo " url export KEY [exclude-dir ...] :" echo " export bookmarks to a website-like form. Categories become" echo " a web page linked to all sub-categories and entries it" echo " contains. All pages are copied to the 'KEY' directory." echo " Optional arguments 'exclude-dir' are directories you do not" echo " want to be included in your export directory. These are" echo " space-separated directory names. Each directory that" echo " matches one of these names will not be included no more" echo " than its sub-directories (do not use full paths : only the" echo " last path word will be tested)." } do_help_config () { echo " url's configuration file ($CONFIGFILE)" echo echo " It can contain some options :" echo " BROWSER=\"your favorite browser\" # double quote it for spaces" echo " BOOKSD=your/bookmarks/spool/dir/ # a directory relative to ~" echo " BOOKSF=your/bookmarks/file.html # file for your favorite browser relative to ~" echo " OWNER=\"Your name\" # your name, with double quotes" echo " TODAY=\`date \"+your prefered date format\" # how you want the date to be displayed" echo " LINKTO=a/bookmark/file.html # a file you want to be hardlinked to BOOKSF" echo " COPYTO=a/bookmark/file.html # a file you want BOOKSF to be copied to" echo echo " Default values :" echo " BROWSER=$BROWSER" echo " BOOKSD=$BOOKSD" echo " BOOKSF=$BOOKSF" echo " OWNER=$OWNER" echo " TODAY=\`date \"+%A %d %B %Y\"\`" if [ "$LINKTO" != "" ]; then echo " LINKTO=$LINKTO" else echo " LINKTO=\"\"" fi if [ "$COPYTO" != "" ]; then echo " COPYTO=$COPYTO" else echo " COPYTO=\"\"" fi } do_help_list () { echo "help" echo "open" echo "opendir" echo "show" echo "display" echo "find" echo "add" echo "verify" echo "import" echo "update" } do_help () { if [ "$1" != "" ]; then case "$1" in "open") do_help_open;; "opendir") do_help_opendir;; "show") do_help_show;; "display") do_help_display;; "find") do_help_find;; "add") do_help_add;; "verify") do_help_verify;; "import") do_help_import;; "update") do_help_update;; "export") do_help_export;; "-l") do_help_list;; "config") do_help_config;; *) echo "help : unkown command resquested..." esac else echo " COMMANDS :" echo " url help [COMMAND]" echo " url open KEY" echo " url opendir KEY" echo " url show KEY" echo " url display KEY" echo " url find KEY" echo " url add KEY URL CATEGORY [comment]" echo " url verify" echo " url import KEY [notest]" echo " url update [exclude-dir ...]" echo " url export KEY [exclude-dir ...]" fi } do_usage () { if [ "$1" != "" ]; then echo -n "url: error = $1" if [ "$2" != "" ]; then echo -n " ($2)" fi echo "." echo fi echo "url $VERSION (c) Thomas Nemeth" echo "Usage : url [-d] [-v] [-u] [-ln file] [-cp file] command [OPTIONS]" echo " -d : debug mode" echo " -v : verbose mode" echo " -vv : very verbose mode" echo " -ln file : hard link $BMFILE to file argument (update cmd only)" echo " -cn file : copy $BMFILE to file argument (update cmd only)" echo do_help echo echo " If $BMFILE exist, search is done within it." echo echo " You can have a configuration file ($CONFIGFILE) : see 'url help config'" } #################################### # # PARTIE PRINCIPALE # #################################### # Récupération des arguments # what="" opts="" dolink=0 for arg in $*; do case $arg in "-d") DEBUG=1;; "-v") VERBOSE=1;; "-vv") VERBOSE=2;; "-ln") dolink=1;; "-cp") dolink=2;; "help"|"open"|"opendir"|"show"|"display"|"find"|"add"|"verify"|"import"|"update"|"export") if [ "$what" = "" ]; then what=$arg else if [ "$arg" = "help" ]; then KEY=$what what="help" elif [ "$KEY" = "" ]; then KEY="$arg" else opts="$opts $arg" fi fi if [ $dolink != 0 ]; then dolink=0 fi;; *) if [ "$what" != "" ]; then if [ "$KEY" = "" ]; then KEY="$arg" else opts="$opts $arg" fi elif [ $dolink = 1 ]; then LINKTO="$LINKTO $arg" elif [ $dolink = 2 ]; then COPYTO="$COPYTO $arg" fi;; esac do_debug "arg = $arg" done # Traitement de la commande # do_debug "Bookmarks dir : $BMDIR/ --- Bookmarks file : $BMFILE" case $what in "help") if [ "$KEY" != "" ]; then do_help $KEY else do_usage fi;; "open") do_open;; "opendir") do_opendir;; "show") do_show;; "display") do_display;; "find") do_find;; "add") do_add $opts;; "verify") do_verify;; "import") do_import $opts;; "update") do_update $opts;; "export") do_export $opts;; *) do_usage "Unknown command" $what;; esac