Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:18:50

0001 #!/bin/bash
0002 #
0003 # Creates an index.html file for
0004 # a folder that contains images.
0005 #
0006 # Can recurse through sub-directories
0007 # and place index.html files within
0008 # folders that contain image files.
0009 #
0010 # Michael Anderson
0011 # May 22, 2008
0012 
0013 ##################################################
0014 #### Variables #####
0015 imgFilter=gif
0016 
0017 # Look through sub directories by default?
0018 #      0 = no, 1 = yes
0019 recurseThruDir=0
0020 
0021 # Output dir (if you want it copied somewhere else)
0022 storageDir=
0023 ##################################################
0024 
0025 
0026 ##################################################
0027 PrintUsage() {
0028   echo "USAGE: makeImgWebPage [options] <folderWithImages>"
0029   echo ""
0030   echo "Creates an index.html file for folder(s) that contain"
0031   echo "image files (as specified by --img-filter option)."
0032   echo "Default action is to place index.html in folders themselves."
0033   echo ""
0034   echo "OPTIONS:"
0035   echo "  --img-filter=$imgFilter (or jpg, png, etc...)"
0036   echo "  --output-dir=<folderWithImages>  (will copy folder(s) recursed thru to there)"
0037   echo "  --recurse  (Go through sub directories)"
0038   exit 2
0039 }
0040 ##################################################
0041 
0042 
0043 ##################################################
0044 #### Get command-line options ####
0045 OPTS=`getopt -o "h" -l "help,output-dir:,img-filter:,recurse" -- "$@"`
0046 if [ $? -ne 0 ]; then PrintUsage; fi
0047   
0048 eval set -- "$OPTS"
0049   
0050 while [ ! -z "$1" ]
0051 do
0052   case "$1" in
0053     -h) PrintUsage;;
0054     --help) PrintUsage;;
0055     --output-dir) shift; storageDir=$1;;
0056     --img-filter) shift; imgFilter=$1;;
0057     --recurse) recurseThruDir=1;;
0058     --) shift; break;;
0059     *) die "Unexpected option $1";;
0060   esac
0061   shift
0062 done
0063 
0064 # Check that they specified the one required input argument
0065 if [ "$#" -ne 1 ]; then PrintUsage; fi
0066 
0067 #################################
0068 ## Get the rquired input argument
0069 givenFolder=$1
0070 ##################################################
0071 
0072 
0073 ##################################################
0074 #### Make sure the given directory 
0075 ####  is actually a directory
0076 if [ ! -d "$givenFolder" ]; then
0077   echo "Error: $givenFolder is not a directory."
0078   echo ""
0079   PrintUsage
0080 fi
0081 ##################################################
0082 
0083 
0084 ##################################################
0085 #### Create index.html for given folder
0086 createIndex() {
0087   folderName=$1
0088   title=$1
0089   fileToMake=$1/index.html
0090 
0091   numOfImages=`ls $folderName | grep $imgFilter | wc -l`
0092   if [ $numOfImages -eq 0 ]; then
0093     echo "Error: $folderName contains no $imgFilter images."
0094     echo ""
0095     PrintUsage
0096   else
0097     echo -e "$folderName contains $numOfImages $imgFilter images.  \c"
0098     echo -e "Creating index.html...\c"
0099   fi
0100 
0101   #######
0102   # Start making the html file
0103   echo -e "<html><head><title>$title</title></head>\n<body>" > $fileToMake
0104 
0105   # Loop over all the files in the folder
0106   #  that contain $imgFilter in the name
0107   #  and add them to the html file
0108   counter=0; 
0109 
0110   # store internal field separator
0111   ORIGIFS=$IFS
0112   # set $IFS to end-of-line (to deal with spaces in names)
0113   IFS=`echo -en "\n\b"`
0114   for imageFile in `ls $folderName | grep $imgFilter`; do
0115 
0116     echo "<img src=\"$imageFile\">" >> $fileToMake
0117     counter=$((($counter+1)%2))
0118     # add a line break <br> after every other image
0119     if [ $counter -eq 0 ]; then 
0120       echo "<br>" >> $fileToMake
0121     fi
0122   done
0123   # set $IFS back
0124   IFS=$ORIGIFS
0125 
0126 
0127   echo "</body></html>" >> $fileToMake
0128   # End of making the html
0129   #######
0130 
0131   echo "Done."
0132   return
0133 }
0134 ##################################################
0135 
0136 
0137 ##################################################
0138 #### Recurse through directories ####
0139 r() { 
0140   #cd "$1"
0141   currentDir=$1
0142 
0143   # see if this folder contains img files
0144   numOfImages=`ls "$currentDir" | grep $imgFilter | wc -l`
0145   if [ $numOfImages -gt 0 ]; then
0146     # if so, create index.html
0147     createIndex $currentDir
0148   else
0149     echo "$currentDir contains no $imgFilter images."
0150   fi
0151   
0152   # Go to the sub-directories
0153   # store internal field separator
0154   ORIGIFS=$IFS
0155   # set $IFS to end-of-line (to deal with spaces in names)
0156   IFS=`echo -en "\n\b"`
0157   for d in `ls $currentDir` ; do
0158     if [ -d "$currentDir/$d" ]; then
0159       ( r "$currentDir/$d" )
0160     fi;
0161   done
0162   # set $IFS back
0163   IFS=$ORIGIFS
0164 
0165   return
0166 }
0167 ##################################################
0168 
0169 
0170 ##################################################
0171 #### Main Method
0172 if [ "$recurseThruDir" -eq 1 ]; then
0173   r $givenFolder
0174 else
0175   createIndex $givenFolder
0176 fi
0177 
0178 # Check to see if we are to copy these 
0179 # to an output directory
0180 if [ -n "$storageDir" ]; then
0181  echo -e "Copying $givenFolder to \n$storageDir"
0182 
0183  # Consider using --parents option to copy parent folders as well
0184  cp -r $givenFolder $storageDir/
0185 fi