summaryrefslogtreecommitdiff
path: root/photoalbum.sh
blob: 57d953e709ef632d050207bac92f7e5081c3abb3 (plain)
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
#!/bin/bash 

# Small quick n dirty photo album script
# 2011, 2012, 2013 Paul Buetow

source photoalbum.conf

function template () {
  local -r template=${1} ; shift
  local -r html=${1}     ; shift
  local destdir=${1}     ; shift

  if [ -z "${destdir}" ]; then
    destdir=./dist/html/
  fi

  if [ -d ./templates/ ]; then
    source ./templates/${template}.tmpl >> ${destdir}/${html}.html
  else
    source ../../templates/${template}.tmpl >> ../../${destdir}/${html}.html
  fi
}

function createdirs () {
  for dir in ./dist/{photos,thumbs,html}; do 
    [ -d ${dir} ] || mkdir -vp ${dir}
  done
}

function scale () {
  cd "${INCOMING}" || exit 1
  find . -type f | sed 's#^\./##' | 
  while read photo; do
    # Flatten directories / to __
    destphoto="${photo//\//__}"

    if [ ! -f "../dist/photos/${destphoto}" ]; then
      echo "Scaling ${photo} to ../dist/photos/${destphoto}"

      convert -auto-orient \
        -geometry ${GEOMETRY} "${photo}" "../dist/photos/${destphoto}"
    fi
  done
  cd - &>/dev/null

  echo 'Removing spaces from file names'
  find ./dist/photos -type f -name '* *' |
  while read file; do
    rename 's/ /_/g' "${file}" 
  done
}

function generate () {
  local num=1
  local name=page-${num}
  local -i i=0

  template header ${name} 
  template header-first-add ${name}

  cd ./dist/photos || exit 1

  find ./ -type f | sort | sed 's#^\./##' |
  while read photo; do 
    : $(( i++ ))

    if [ ${i} -gt ${MAXPREVIEWS} ]; then
      i=1
      : $(( num++ ))

      next=page-${num}
      template next ${name}
      template footer ${name}

      prev=${name}
      name=${next}
      template header ${name} 
      template prev ${name}
    fi

    # Preview page
    template preview ${name}

    # View page
    template header ${num}-${i}
    template view ${num}-${i}
    template footer ${num}-${i}

    if [ ! -f "../thumbs/${photo}" ]; then 
      echo "Creating thumb for ${photo}";
      convert -geometry x${THUMBGEOMETRY} "${photo}" \
        "../thumbs/${photo}"
    fi
  done

  cd - &>/dev/null

  template footer $(cd ./dist/html;ls -t page-*.html | head -n 1 | sed 's/.html//')

  # Generate HTTP redirect pages
  ls ./dist/html/*.html | grep -v page- | cut -d'-' -f1 | uniq |
  while read prefix; do 
    declare page=$(ls -t ${prefix}-*.html | head -n 1 | sed 's#./dist/html/\(.*\)-.*.html#\1#')
    declare lastview=$(ls -t ${prefix}-*.html | head -n 1 | sed 's/.*-\(.*\).html/\1/')

    declare prevredirect=${page}-0
    declare nextredirect=${page}-$((lastview+1))

    redirectpage=$(( page-1 ))-${MAXPREVIEWS}
    template redirect ${prevredirect}

    if [ ${lastview} -eq ${MAXPREVIEWS} ]; then
      redirectpage=$(( page+1 ))-1
    else
      redirectpage=${page}-${lastview}
      template redirect 0-${MAXPREVIEWS}

      redirectpage=1-1
    fi

    template redirect ${nextredirect}
  done
}

function tarball () {
  # Cleanup tarball from prev run if any
  find ./dist/ -maxdepth 1 -type f -name \*.tar -delete

  if [ "${INCLUDETARBALL}" = 'yes' ]; then
    echo Creating tarball
    mv "${INCOMING}" "${TARBALLNAME}" 
    tar $TAROPTS  -f "./dist/${TARBALLNAME}${TARBALLSUFFIX}" "${TARBALLNAME}"
    mv "${TARBALLNAME}" "${INCOMING}"
  fi
}

createdirs
scale
find ./dist/ -type f -name \*.html -delete
template index index ./dist
generate
tarball