Vimwiki生成SiteMap脚本

#!/bin/sh
#Scriptname: sitemap.sh
#Auther: YuanXing
#Create: 2013-12-17
#Update: 2015-06-20

WikiDir=/data/soft/Vimwiki/Mywiki
WikiHtmlDir=/data/soft/xstarcd.github.io/wiki
SiteMap=${WikiDir}/SiteMap.wiki
SiteMapold=${WikiDir}/SiteMap.old
SiteMaptxt=${WikiHtmlDir}/SiteMap.txt
WikiUrl=http://w.gdu.me/wiki
EXCLUDE='/personal|/ogames|/gmxzd'

export PATH=/usr/local/bin:/usr/bin:/bin

# -----------------------------------------------------------------------------
kernelname=$(uname -s)
case "${kernelname%_*}" in
Linux|CYGWIN)
    alias STAT="stat -c %y"
    alias ECHO_N="echo -n"
    alias ECHO_BR="echo"
    [ "${SHELL##*/}" = "bash" ] && alias ECHO_BR="echo -e"
    ;;
Darwin)
    # MacOS
    alias STAT="stat -f %Sm -t '%Y-%m-%d %H:%M:%S'"
    alias ECHO_N="printf"
    alias ECHO_BR="echo"
    ;;
*)
    echo "Sorry,The system type unknow(${kernelname%_*}),I cannot use stat to get file motify time."
    exit 1
esac
rm -f "$SiteMapold"
cp "$SiteMap" "$SiteMapold"
ECHO_BR "%title 站点地图\n%toc 站点地图\n<pre id='no-comment'>本页不允许评论</pre>" > "$SiteMap"
ECHO_BR "\n* Update: $(date +'%Y-%m-%d %H:%M:%S')" >> "$SiteMap"
echo -n "" > "$SiteMaptxt"
for D in `find $WikiDir -type d|grep -vE "$EXCLUDE"|sort`;do
    ECHO_N "${D}:"
    dirurl=`echo $D|sed "s#$WikiDir/##g"`
    if [ -n "$dirurl" -a "$D" != "$WikiDir" ];then
        basename=$(basename "$D")
        dirlevel=`echo "$dirurl"|sed -e 's#[^/]##g' -e 's#/#\*#g'`
        if [ -z "$dirlevel" ];then
            ECHO_BR "\n== [[$dirurl/index|$basename]] ==" >> "$SiteMap"
        else
            ECHO_BR "\n$dirlevel [[$dirurl/index|$basename]]" >> "$SiteMap"
        fi
        echo "$WikiUrl/$dirurl/index.html" >> "$SiteMaptxt"
  
        out=
        cnt=0
        for F in `find $D -maxdepth 1 -type f \( -name "*.wiki" -and ! -name "index.wiki" \)|grep -vE 'personal'|sort`;do
            cnt=$(expr $cnt + 1)
            #let cnt=cnt+1
            url=`echo "$F"|sed -e "s#$WikiDir/##g" -e 's#\.wiki##g'`
            #level=`echo "${url}"|sed -e 's#[^/]##g' -e 's#/#    #g'|awk '{print $0"*"}'`
            level="`echo "${url}"|sed -e 's#[^/]##g' -e 's#/#    #g'`*"
            title=$(head "$F"|grep '^%title'|head -1|sed 's/%title //g')
            title=${title:-$url}
            modify=`STAT $F|awk '{print $1}'`
            echo "$WikiUrl/${url}.html" >> "$SiteMaptxt"
            [ -n "$out" ] && out="$out\n"
            out="${out}$level [$modify][[$url|$title]]"
            grep "\[\[$url\|$title]]" "$SiteMapold" > /dev/null 2>&1
            if [ "$?" = "0" ];then
                ECHO_N "."
            else
                ECHO_N "+"
            fi
        done
        ECHO_N "$cnt"
        ECHO_BR "$out"|sort -r >> "$SiteMap"
    fi
    echo ""
done
ECHO_N "Total:";cat "$SiteMaptxt"|wc -l