#!/bin/bash # written by Kristian Knudsen Olesen , August 2012. # WARNING: For each subfolder, $entry say, in the folder given by the variable # $path, the files $entry.zip and $entry.htm will be oweritten if they exist, # on execution of this script. # This script does the following: For each directory, $entry say, in the # directory specified in the variable $path all files execept folders (and # possibly one regular file---see below) are compressed into a .zip archive # which is named $entry.zip and placed in the directory $entry. Afterwards a # file named $entry.htm is created in the directory $entry which lists all the # contents of the directory $entry except possibly subfolders and the file # named $entry.htm. These possible exceptions depends on how the variables # $showHTML and $showSUBDIR are set. # Set the Internal Field Separator to be a linebreak. This is done to make # shure that looping over the output of `ls -1` is possible. This though it is # not inessesary if no files contain whitespaces IFS=$'\n' # This variable should be set to either true or false. If set to true # subdirectories will also be listet in the .htm files. If set to false these # will be ignored. showSUBDIR=true # This variable should be set to either true or false. If set to true # actual .htm file will also be listet in the file list. If set to false it # will be ignored. showHTML=false # This variable should be set to either true or false. If set to true # then the .htm file created will also be added to the .zip archive. If set to # false it will be ignored. zipHTML=false # This variable should contain the path of the folder which the script is # supposed to run from, that is, where the index.htm file is supposed to be # created. The path must NOT end with a '/'. path="/net/www/www.math.ku.dk/noter/filer/exam" # This variable should contain the internet link to the folder specified in # $path. Like with $path it must NOT end with a '/'. parent="http://www.math.ku.dk/noter/filer/exam" cd $path # Loop over files/folders in $path (this loop ends in the bottom of this # script). for entry in `ls -1`; do # An if loop that creates the $entry.htm file in the directory $entry, if # $entry is a directory (this loop ends in the bottom of the script). if [ -d $entry ] then # if loop that removes the file $entry/$entry.zip if it already exist. This is # done because the command 'zip' used later just adds entries to the archive. # Thus if it already exist, new things is just added. if [ -f "$entry/$entry.zip" ] then rm "$entry/$entry.zip" fi # for loop that creates the .zip archive. It loops over all files in $entry # and checks whether they should be added, and adds them if this is the case. for file in `ls -1 $entry` do # If loop that determines whether $file satisfies some conditions. The # conditional statement in this if loop is of the form ( A or B ) and C. It is # true if $file is not a directory and either $file is not equal to $entry.htm # or the $zipHTML option is set to true if ( [ ! "$file" == "$entry.htm" ] || $zipHTML ) && [ ! -d "$entry/$file" ] then # The option -q is for 'quiet' and the option -j is to strip preseeding # folders, remove it and the archive will contain only one folder named $entry # and the contents will be in this one. zip -jq "$entry/$entry.zip" "$entry/$file" fi done # Create a $entry.htm file in $entry with the following contents. echo " Index of $entry

Index of $entry


[x] Parent Directory" > "$entry/$entry.htm"


# for loop that runs over files/folders in $entry. Before looping the list of
# files is sorted. This is done by changing some of the filenames, sorting,
# and then changing the filenames back. First all files starting with two
# digits follows by -- is replaced with two times the two digits, e.g. 85-- is
# changed to 8585. After this every file whose two first digits constituting a
# number which are strictly less than 50 has 20 placed in front of it, so
# files starting with 34 are changed so that they start with 2034. After this,
# files whose two first digits constitute a number greater than or equal to 50
# get 19 placed in front, e.g. files starting with 85 are changed so that they
# start with 1985.  Now the lines are sorted alphabetically, and the filenames
# are changed back to the original.

# CHANGED BY Anders Thorup SO THAT
#    yy in interval 40\le yy \le 39 is
# interpreted as
#   yyyy in interval 1940 \le yyyy \le 2039
# before sorting. 

for file in `ls -1 "$entry/" | sed 's:^\([0-9]\{2\}\)--:\1\1:;s:^\([0123][0-9]\):20\1:;s:^\([456789][0-9]\):19\1:' | sort | sed 's:^[0-9]\{2\}\([0-9]\{2\}\):\1:;s:^\([0-9]\{2\}\)\1:\1--:'`
		do

# if loop that adds a link to $file in the .htm file if $file satisfies some
# conditions. The conditional statement is of the form (A or B) and (C or D).
# It is true if (either $file is not a directory or $showSUBDIR is true) and
# (either $file is not equal $entry.htm or $showHTML is true).
			if ( [ ! -d "$entry/$file" ] || $showSUBDIR ) &&  ( [ ! "$file" == "$entry.htm" ] || $showHTML )
				then
					echo "[ ] $file" >> "$entry/$entry.htm"
			fi
	done


# Add the following contents to the $entry.htm file
echo "

Last updated `date`
" >> "$entry/$entry.htm" fi done