summaryrefslogtreecommitdiff
path: root/src/photoalbum.sh
blob: c89dd501052076f1244acb42f87d7580475403a5 (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
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
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash 

# photoalbum (c) 2011, 2012, 2013 by Paul Buetow
# http://photoalbum.buetow.org

declare -r ARG1="${1}" ; shift
declare -r VERSION='PHOTOALBUMVERSION'

usage () {
  cat - <<USAGE >&2
  Usage: 
  $0 [clean|init|version|generate|all]
USAGE
}

init () {
  for dir in "${INCOMING_DIR}" "${DIST_DIR}/photos" "${DIST_DIR}/thumbs" "${DIST_DIR}/html"; do 
    [ -d "${dir}" ] || mkdir -vp "${dir}"
  done

  if [ "${TARBALL_INCLUDE}" = yes ]; then
    local -r BASE=$(basename "${INCOMING_DIR}")
    local -r NOW=$(date +'%Y-%m-%d-%H%M%S')
    # New global variable
    TARBALL_NAME="${BASE}-${NOW}.${TARBALL_SUFFIX}"
  fi
}

clean () {
  echo "Not deleting ${INCOMING_DIR}"
  [ -d "${DIST_DIR}" ] && rm -Rf "${DIST_DIR}"
}

tarball () {
  # Cleanup tarball from prev run if any
  find "${DIST_DIR}" -maxdepth 1 -type f -name \*.tar -delete

  if [ "${TARBALL_INCLUDE}" = yes ]; then
    local -r BASE=$(basename "${INCOMING_DIR}")

    echo "Creating tarball ${DIST_DIR}/${TARBALL_NAME} from ${INCOMING_DIR}"
    cd $(dirname "${INCOMING_DIR}")
    tar $TAR_OPTS  -f "${DIST_DIR}/${TARBALL_NAME}" "${BASE}"
    cd - &>/dev/null
  fi
}

generate () {
  if [ ! -d "${INCOMING_DIR}" ]; then
    echo "ERROR: You may run init first, no such directory: ${INCOMING_DIR}" >&2
    exit 1
  fi
  if [ ! -d "${DIST_DIR}" ]; then
    echo "ERROR: You may run init first, no such directory: ${DIST_DIR}" >&2
    exit 1
  fi

  scale
  find "${DIST_DIR}/html" -type f -name \*.html -delete
  makedist 1
  template index ../index
  tarball
}

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

  source "${TEMPLATE_DIR}/${template}.tmpl" >> "${DIST_DIR}/html/${html}.html"
}

scale () {
  cd "${INCOMING_DIR}" && find ./ -type f | sort | while read photo; do
  if [ ! -f "${DIST_DIR}/photos/${photo}" ]; then

    # Flatten directories / to __
    if [[ "${photo}" =~ / ]]; then
      destphoto="${photo//\//__}"
    else
      destphoto="${photo}"
    fi

    destphoto="${destphoto/./}"

    echo "Scaling ${photo} to ${DIST_DIR}/photos/${destphoto}"

    convert -auto-orient \
      -geometry ${GEOMETRY} "${photo}" "${DIST_DIR}/photos/${destphoto}"
  fi
done

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

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

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

  cd "${DIST_DIR}/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 "${DIST_DIR}/thumbs/${photo}" ]; then 
      echo "Creating thumb for ${photo}";
      convert -geometry x${THUMBGEOMETRY} "${photo}" \
        "${DIST_DIR}/thumbs/${photo}"
    fi
  done

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

  cd "${DIST_DIR}/html" && ls *.html | grep -v page- | cut -d'-' -f1 | uniq |
  while read prefix; do 
    declare page=$(ls -t ${prefix}-*.html |
    head -n 1 | sed 's#\(.*\)-.*.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
}

source /etc/default/photoalbum

if [ -f ~/.photoalbumrc ]; then
  source ~/.photoalbumrc
fi

case "${ARG1}" in
  all)
    clean
    init
    generate
    ;;
  init)
    init
    ;;
  clean)
    clean
    ;;
  generate)
    generate
    ;;
  version)
    echo "This is Photoalbum Version ${VERSION}"
    ;;
  *)
    usage
    ;;
esac