@make.directory.index 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #!/bin/tcsh
  2. # usage: script [dir1 dir2 ...]
  3. # make an index.html file out of the files in the current
  4. # directory, or all directories on the command line
  5. # trees to avoid:
  6. # pub/dist/HOWTO/howto
  7. # pub/dist/doc/program_help
  8. set backup = 1 # make a backup of any current copy
  9. set clean = 0 # nuke old files
  10. set curdir = 1 # include current directory
  11. set dir = $cwd:t
  12. set hfile = index.html
  13. set htmp = .tmp.index.html
  14. set date_form = '+%Y_%m_%d_%H%M'
  15. set date = `date $date_form`
  16. set show_date = 1 # include timestamp in web page
  17. set dirs = 0 # run this within directories
  18. set nested = 0 # nest this for directories
  19. set testing = 0 # just running a test
  20. set verb = 1 # verbose level (0=quiet, 1=default, 2=verbose)
  21. set warn = 1 # warn about non-overwrite files
  22. set indent = 0 # indent for printing (to show nesting depth)
  23. set prog = `basename $0`
  24. # look for a very specific string, to test for old generation by this script
  25. set match_string = '^.center. .h2.contents of directory: '
  26. # ======================================================================
  27. # skipping directories is no longer needed, as we check for $match_string
  28. # ======================================================================
  29. #
  30. # skip particular directories (looks like or tail is)
  31. # if ( $dir =~ '*_files' || $dir =~ '*_doxy' || $dir =~ *tutorial || \
  32. # $dir == howto || $dir == program_help || $dir == html.nifti || \
  33. # $dir == AFNI_MatlabLib || $dir == cvs || $dir == CVS || \
  34. # $dir == afni_src ) then
  35. # echo "-- skipping $cwd"
  36. # exit
  37. # endif
  38. # if no file names are specified, all files will be applied
  39. # else, all files will be used
  40. set files = ( ) # init to empty list
  41. set pass_opts = () # options to pass for recur
  42. set narg = 1
  43. while ( $narg <= $#argv )
  44. if ( "$argv[$narg]" == "-help" ) then
  45. echo "usage: $prog [-dirs/-nested] file file ..."
  46. echo ""
  47. echo "examples:"
  48. echo ""
  49. echo " 1. just make a file in the current directory"
  50. echo ""
  51. echo " @make.directory.index"
  52. echo ""
  53. echo " 2. specify which files to include"
  54. echo ""
  55. echo " @make.directory.index *.txt some other files"
  56. echo ""
  57. echo " 3. run on a specific list of directory trees"
  58. echo ""
  59. echo " @make.directory.index -dirs dir1 dir2 /some/dir3"
  60. echo ""
  61. echo " 4. recursively fill a tree; nuke old copies index.html files"
  62. echo ""
  63. echo " @make.directory.index -nested -clean -dirs ."
  64. echo ""
  65. echo " 5. and if not in PATH"
  66. echo ""
  67. echo " ./@make.directory.index -nested -clean -use_path -dirs ."
  68. echo ""
  69. echo ""
  70. echo "With no options, all files/dirs are included."
  71. echo "Else, specify a list of files/dirs."
  72. echo ""
  73. echo " -clean : delete old index.html files"
  74. echo " -date Y/N : specify whether to use the date in output [Y]"
  75. echo " -indent N : spaces to indent text"
  76. echo " -dirs : argumets are directories to enter and run on"
  77. echo " -nested : nest command to get entire directory trees"
  78. echo " -quiet : try to be quiet"
  79. echo " -test : just show what would be done"
  80. echo " -use_path : if not in PATH, include path to prog on recur"
  81. echo " -verb : be more verbose (can use multiple options)"
  82. echo ""
  83. exit
  84. else if ( "$argv[$narg]" == "-clean" ) then
  85. set clean = 1
  86. set backup = 0
  87. set pass_opts = ( $pass_opts "$argv[$narg]" )
  88. else if ( "$argv[$narg]" == "-indent" ) then
  89. @ narg ++
  90. if ( $narg > $#argv ) then
  91. echo "** need NUM_SPACES after -indent"
  92. exit
  93. endif
  94. set indent = $argv[$narg]
  95. else if ( "$argv[$narg]" == "-date" ) then
  96. @ narg ++
  97. if ( $narg > $#argv ) then
  98. echo "** need Y/N after -date"
  99. exit
  100. endif
  101. if ( "$argv[$narg]" == "N" || "$argv[$narg]" == "n" ) then
  102. set show_date = 0
  103. else if ( "$argv[$narg]" == "Y" || "$argv[$narg]" == "y" ) then
  104. set show_date = 1
  105. else
  106. echo "** -date takes y or n as parameter, have $argv[$narg]"
  107. exit
  108. endif
  109. set pass_opts = ( $pass_opts -date $argv[$narg] )
  110. else if ( "$argv[$narg]" == "-dirs" ) then
  111. if ( $narg > $#argv ) then
  112. echo "** need dirs after -enter_dirs"
  113. exit
  114. endif
  115. set dirs = 1
  116. else if ( "$argv[$narg]" == "-nested" ) then
  117. set nested = 1
  118. set pass_opts = ( $pass_opts "$argv[$narg]" )
  119. else if ( "$argv[$narg]" == "-quiet" ) then
  120. set verb = 0
  121. set pass_opts = ( $pass_opts "$argv[$narg]" )
  122. else if ( "$argv[$narg]" == "-test" ) then
  123. set testing = 1
  124. set pass_opts = ( $pass_opts "$argv[$narg]" )
  125. else if ( "$argv[$narg]" == "-use_path" ) then
  126. # use path to prog, if not in PATH
  127. set prog = $0
  128. set pass_opts = ( $pass_opts "$argv[$narg]" )
  129. else if ( "$argv[$narg]" == "-verb" ) then
  130. @ verb += 1
  131. set pass_opts = ( $pass_opts "$argv[$narg]" )
  132. else
  133. # otherwise, assume as have a list of files
  134. set files = ( $argv[$narg-] )
  135. break
  136. endif
  137. @ narg ++
  138. end
  139. # convert indent level to spaces string and prepare for recursion
  140. set spaces = "`printf '%*s' $indent ''`"
  141. @ indent += 3
  142. set pass_opts = ( $pass_opts -indent $indent )
  143. # if we do not yet have files, get some
  144. if ( $#files == 0 ) then
  145. if ( $dirs ) then
  146. echo "** error: need directory list with -dirs"
  147. exit
  148. endif
  149. set files = ( `\ls -1 | grep -v $hfile` )
  150. endif
  151. # ------------------------------------------------------------
  152. # show where we are
  153. if ( $verb ) then
  154. echo "${spaces}dir: `pwd`"
  155. endif
  156. # ------------------------------------------------------------
  157. # do we make one in the current directory
  158. set make_one_here = 1
  159. if ( -f $hfile ) then
  160. grep -q "$match_string" index.html
  161. if ( $status ) then
  162. if ( $warn ) echo "NO: skip `pwd`/$hfile"
  163. set make_one_here = 0
  164. endif
  165. endif
  166. # ------------------------------------------------------------
  167. # initialize html file
  168. if ( $make_one_here && ! $testing && ! $dirs ) then
  169. echo -n "" > $htmp
  170. echo "<html>" >> $htmp
  171. echo "<head> <title>$dir</title> </head>" >> $htmp
  172. echo "<body>" >> $htmp
  173. # this should be matchable by $match_string
  174. echo "<!-- autogenerated by @make.directory.index -->" >> $htmp
  175. echo "<center> <h2>contents of directory: $dir</h2> </center>" >> $htmp
  176. echo "" >> $htmp
  177. echo "<pre>" >> $htmp
  178. echo "" >> $htmp
  179. # ------------------------------------------------------------
  180. # create a header row
  181. if ( $show_date ) then
  182. set hstr = " type date name"
  183. set dstr = " ---- --------------- ------------------------"
  184. else
  185. set hstr = " type name"
  186. set dstr = " ---- ------------------------"
  187. endif
  188. echo "$hstr" >> $htmp
  189. echo "$dstr" >> $htmp
  190. echo "" >> $htmp
  191. endif
  192. # ------------------------------------------------------------
  193. # add contents and/or recur
  194. foreach file ( $files )
  195. if ( "$file" =~ "$hfile*" ) continue
  196. if ( -f $file ) then
  197. set ftype = "file"
  198. else if ( -d $file ) then
  199. set ftype = "dir "
  200. if ( $file != .. ) then
  201. # maybe do some more work here
  202. if ( $dirs ) then
  203. # echo "$spaces++ processing directory $file ..."
  204. cd $file
  205. $prog $pass_opts
  206. cd -
  207. continue # nothing else to do
  208. else if ( $nested ) then
  209. cd $file
  210. # echo "$spaces++ processing nested directory `pwd`"
  211. $prog $pass_opts
  212. cd -
  213. endif
  214. endif
  215. else
  216. set ftype = " "
  217. endif
  218. if ( $make_one_here && ! $testing ) then
  219. if ( $show_date ) then
  220. set fdate = `\ls -ld --time-style=$date_form $file | awk '{print $6}'`
  221. set dstr = "`echo $fdate | sed 's/_/./g'` "
  222. else
  223. set dstr = ''
  224. endif
  225. echo " $ftype $dstr<a href=$file>$file</a>" >> $htmp
  226. endif
  227. end
  228. # if -dirs, those were just starting points, so we are done
  229. if ( $dirs ) exit
  230. # if testing, just mention what is here
  231. if ( $testing ) then
  232. echo "${spaces}have $#files files under $dir"
  233. exit
  234. endif
  235. # if we are not making a new file, we are done
  236. if ( ! $make_one_here ) exit
  237. # ------------ finalize result (temp file) and make index ------------
  238. echo "" >> $htmp
  239. echo "</pre>" >> $htmp
  240. echo "</body>" >> $htmp
  241. echo "</html>" >> $htmp
  242. # --- decide what to do with the new and old files
  243. # if cleaning, do that first
  244. if ( -f $hfile && $clean ) then
  245. # nuke any old stuff and overwrite
  246. echo "$spaces == nuking old $hfile* under $dir ..."
  247. \rm -f $hfile*
  248. endif
  249. # note whether there is any change to make
  250. set changed = 1
  251. if ( -f $hfile ) then
  252. cmp -s $htmp $hfile
  253. set changed = $status
  254. endif
  255. # if there is a change to make, do so
  256. if ( $changed ) then
  257. echo "$spaces -- saving results in $dir" # nothing to do
  258. if ( -f $hfile && $backup ) \mv -f $hfile $hfile.$date # maybe make backup
  259. mv $htmp $hfile # the main step
  260. else
  261. echo "$spaces -- no change for $dir" # nothing to do
  262. endif