xsay: add support for sselp and xclip; output error message
[grml-scripts.git] / usr_bin / dir2html
1 #!/bin/sh
2
3 # Karsten Kruse - www.tecneeq.de
4 #
5 # dir2html - create HTML-listung for a directory
6 #
7 #  Copyright (c) 2001 - 2004, Karsten Kruse tecneeq(at)tecneeq(dot)de
8 #  All rights reserved.
9 #
10 #  Redistribution and use in source and binary forms, with or without
11 #  modification, are permitted provided that the following conditions
12 #  are met:
13 #
14 #  1. Redistributions of source code must retain the above copyright
15 #     notice, this list of conditions and the following  disclaimer.
16 #  2. Redistributions in binary form must reproduce the above copyright
17 #     notice, this list of conditions and the following disclaimer in
18 #     the documentation and/or other materials provided with the
19 #     distribution.
20 #  3. Neither the name of the author nor the names of its contributors
21 #     may be used to endorse or promote products derived from this
22 #     software without specific prior written permission.
23 #
24 #  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 #  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 #  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 #  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 #  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 #  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 #  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 #  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 #  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 #  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 #  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 # any last words before we die?
37 die(){
38         echo ERROR: $1
39         exit 1
40 }
41
42 # print some help
43 usage(){
44 cat <<EOF
45
46  Usage:
47    `basename $0` [Options]
48
49  Options:
50   -h          => print this help
51   -o out.html => output to file             => default: $my_output
52   -t title    => title                      => default: $my_title
53   -s file.css => path to a css              => default: $my_style
54   -e exclude  => exclude this from listing  => default: $my_exclude
55   -k keywords => keywords for metatag       => default: $my_keywords
56   -d diricon  => icon for directorys        => default: $my_diricon
57   -f fileicon => icon for files             => default: $my_fileicon
58   -p path     => directory to HTMLifiy      => default: $my_dir
59
60 EOF
61 }
62
63 # hardwired defaults
64 my_output="stdout"
65 my_title="Filelisting"
66 my_style="unset"
67 my_exclude="index.html"
68 my_keywords="Files, Stuff"
69 my_diricon="/icons/folder.gif"
70 my_fileicon="/icons/"
71 my_dir=$(pwd)
72
73 #parse commandline
74 while getopts ho:t:s:e:k:i:d:f:p: opt ; do
75   case "$opt" in
76     h)  usage ; exit          ;;
77     o)  my_output="$OPTARG"   ;;
78     t)  my_title="$OPTARG"    ;;
79     s)  my_style="$OPTARG"    ;;
80     e)  my_exclude="$OPTARG"  ;;
81     k)  my_keywords="$OPTARG" ;;
82     d)  my_diricon="$OPTARG"  ;;
83     f)  my_fileicon="$OPTARG" ;;
84     p)  my_dir="$OPTARG"      ;;
85     \?) usage >&2 ; exit 1    ;;
86   esac
87 done
88 shift `expr $OPTIND - 1`
89
90 do_work(){
91
92   cd $my_dir || die "Could not change directory to $my_dir"
93
94   cat <<EOF
95 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
96 <html>
97   <head>
98     <title>$my_title</title>
99     <meta name="description" content="$my_title">
100     <meta name="keywords" content="$my_keywords">
101     <meta name="generator" content="dir2html from www.tecneeq.de">
102     <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
103 EOF
104
105   if [ ! $my_style = unset ] ; then
106     echo "      <link rel=\"stylesheet\" href=\"'$my_style'\" type=\"text/css\">"
107   fi
108
109   cat <<EOF
110     $style
111   </head>
112   <body>
113     <h1 align="center">$my_title</h1>
114     <table summary="Verzeichnislisting" align="center" border="1" cellpadding="6" cellspacing="0">
115       <tr bgcolor="grey">
116         <td>&nbsp;</td>
117         <td>Name</td>
118         <td>Grösse</td>
119         <td>Mimetype</td>
120       </tr>
121 EOF
122
123 for i in `find -type d -maxdepth 1 -printf "%f\n" | sort | sed 's/ /+++A_BLANK+++/g'` ; do
124   doit=true
125   for e in $(echo "$my_exclude .") ; do
126     if [ "$i" = "$e" ] ; then
127       doit=false
128       break
129     fi
130   done
131   if [ $doit = true ] ; then
132     rname="$(echo $i | sed 's/+++A_BLANK+++/ /g')"
133     echo "      <tr>"
134     echo "        <td><img src=\"$my_diricon\" alt=\"Verzeichnis\"></td>"
135     echo "        <td><a href=\"$rname\">$rname/</a></td>"
136     echo "        <td>&nbsp;</td>"
137     echo "        <td>Verzeichnis</td>"
138     echo "      </tr>"
139   fi
140 done
141
142 for i in `find ! -type d -maxdepth 1 -printf "%f\n" | sort | sed 's/ /+++A_BLANK+++/g'` ; do
143   doit=true
144   for e in $(echo "$my_exclude") ; do
145     if [ "$e" = "$i" ] ; then
146       doit=false
147       break
148     fi
149   done
150   if [ $doit = true ] ; then
151     rname="$(echo $i | sed 's/+++A_BLANK+++/ /g')"
152     echo "      <tr>"
153     echo "        <td><img src=\"$my_fileicon\"   alt=\"File     \"></td>"
154     echo "        <td><a href=\"$rname\">$rname</a></td>"
155     echo "        <td>$([ -h "$rname" ] || du -h "$rname" | awk '{print $1}')&nbsp;</td>"
156     echo "        <td>$(file -biL "$rname" | awk '{print $1}' | sed 's/;//g')&nbsp;</td>"
157     echo "      </tr>"
158   fi
159 done
160
161   cat <<EOF
162     </table>
163     <p align="center"><small>Created with <a href="http://www.tecneeq.de/">dir2html</a>.</small></p>
164   </body>
165 </html>
166
167 EOF
168 }
169
170 if [ $my_output = "stdout" ] ; then
171   do_work
172 else
173   echo Writing to $my_output
174   do_work > $my_output
175 fi
176
177 # eof