Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
#!/bin/bash
#
# Creates an index.html file for
# a folder that contains images.
#
# Can recurse through sub-directories
# and place index.html files within
# folders that contain image files.
#
# Michael Anderson
# May 22, 2008

##################################################
#### Variables #####
imgFilter=gif

# Look through sub directories by default?
#      0 = no, 1 = yes
recurseThruDir=0

# Output dir (if you want it copied somewhere else)
storageDir=
##################################################


##################################################
PrintUsage() {
  echo "USAGE: makeImgWebPage [options] <folderWithImages>"
  echo ""
  echo "Creates an index.html file for folder(s) that contain"
  echo "image files (as specified by --img-filter option)."
  echo "Default action is to place index.html in folders themselves."
  echo ""
  echo "OPTIONS:"
  echo "  --img-filter=$imgFilter (or jpg, png, etc...)"
  echo "  --output-dir=<folderWithImages>  (will copy folder(s) recursed thru to there)"
  echo "  --recurse  (Go through sub directories)"
  exit 2
}
##################################################


##################################################
#### Get command-line options ####
OPTS=`getopt -o "h" -l "help,output-dir:,img-filter:,recurse" -- "$@"`
if [ $? -ne 0 ]; then PrintUsage; fi
  
eval set -- "$OPTS"
  
while [ ! -z "$1" ]
do
  case "$1" in
    -h) PrintUsage;;
    --help) PrintUsage;;
    --output-dir) shift; storageDir=$1;;
    --img-filter) shift; imgFilter=$1;;
    --recurse) recurseThruDir=1;;
    --) shift; break;;
    *) die "Unexpected option $1";;
  esac
  shift
done

# Check that they specified the one required input argument
if [ "$#" -ne 1 ]; then PrintUsage; fi

#################################
## Get the rquired input argument
givenFolder=$1
##################################################


##################################################
#### Make sure the given directory 
####  is actually a directory
if [ ! -d "$givenFolder" ]; then
  echo "Error: $givenFolder is not a directory."
  echo ""
  PrintUsage
fi
##################################################


##################################################
#### Create index.html for given folder
createIndex() {
  folderName=$1
  title=$1
  fileToMake=$1/index.html

  numOfImages=`ls $folderName | grep $imgFilter | wc -l`
  if [ $numOfImages -eq 0 ]; then
    echo "Error: $folderName contains no $imgFilter images."
    echo ""
    PrintUsage
  else
    echo -e "$folderName contains $numOfImages $imgFilter images.  \c"
    echo -e "Creating index.html...\c"
  fi

  #######
  # Start making the html file
  echo -e "<html><head><title>$title</title></head>\n<body>" > $fileToMake

  # Loop over all the files in the folder
  #  that contain $imgFilter in the name
  #  and add them to the html file
  counter=0; 

  # store internal field separator
  ORIGIFS=$IFS
  # set $IFS to end-of-line (to deal with spaces in names)
  IFS=`echo -en "\n\b"`
  for imageFile in `ls $folderName | grep $imgFilter`; do

    echo "<img src=\"$imageFile\">" >> $fileToMake
    counter=$((($counter+1)%2))
    # add a line break <br> after every other image
    if [ $counter -eq 0 ]; then 
      echo "<br>" >> $fileToMake
    fi
  done
  # set $IFS back
  IFS=$ORIGIFS


  echo "</body></html>" >> $fileToMake
  # End of making the html
  #######

  echo "Done."
  return
}
##################################################


##################################################
#### Recurse through directories ####
r() { 
  #cd "$1"
  currentDir=$1

  # see if this folder contains img files
  numOfImages=`ls "$currentDir" | grep $imgFilter | wc -l`
  if [ $numOfImages -gt 0 ]; then
    # if so, create index.html
    createIndex $currentDir
  else
    echo "$currentDir contains no $imgFilter images."
  fi
  
  # Go to the sub-directories
  # store internal field separator
  ORIGIFS=$IFS
  # set $IFS to end-of-line (to deal with spaces in names)
  IFS=`echo -en "\n\b"`
  for d in `ls $currentDir` ; do
    if [ -d "$currentDir/$d" ]; then
      ( r "$currentDir/$d" )
    fi;
  done
  # set $IFS back
  IFS=$ORIGIFS

  return
}
##################################################


##################################################
#### Main Method
if [ "$recurseThruDir" -eq 1 ]; then
  r $givenFolder
else
  createIndex $givenFolder
fi

# Check to see if we are to copy these 
# to an output directory
if [ -n "$storageDir" ]; then
 echo -e "Copying $givenFolder to \n$storageDir"

 # Consider using --parents option to copy parent folders as well
 cp -r $givenFolder $storageDir/
fi