summaryrefslogtreecommitdiff
path: root/photoalbum.sh
blob: 39e149eebb24fb548e0c68ac29f73b2465293cdf (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
#!/bin/bash 

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

source photoalbum.conf

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

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

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

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

function scale () {
  cd ${INCOMING} && find ./ -type f | sort | while read photo; do
  if [ ! -f "../photos/${photo}" ]; then
    echo "Scaling ${photo}"
    convert -auto-orient \
      -geometry ${GEOMETRY} "${photo}" "../photos/${photo}"
  fi
  done

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

  cd ..
}

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

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

  cd photos && 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 ..
  template footer $(cd html;ls -t page-*.html | head -n 1 | sed 's/.html//')

  ls html/*.html | grep -v page- | cut -d'-' -f1 | uniq | 
  while read prefix; do 

    declare page=$(ls -t ${prefix}-*.html |
    head -n 1 | sed 's#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 ${next}redirect
  done
}

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