1#!/usr/bin/env bash
2#
3# Copyright (C) 2008-2014 Tvheadend Foundation CIC
4#
5# This file is part of Tvheadend
6#
7# Tvheadend is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# Tvheadend is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with Tvheadend.  If not, see <http://www.gnu.org/licenses/>.
19#
20# For more details, including opportunities for alternative licensing,
21# please read the LICENSE file.
22#
23# ############################################################################
24
25# ############################################################################
26# 3rd Party library processing support
27#
28# Provides support routine for the management of 3rd party libraries for
29# which we include our own static build to simplify integration.
30# ############################################################################
31
32#set -x
33
34# ############################################################################
35# Config
36# ############################################################################
37
38[ -z "$PCLOUD_BASEDIR" ] && PCLOUD_BASEDIR=misc
39[ -z "$PCLOUD_HASHDIR" ] && PCLOUD_HASHDIR=kZ54ee7ZUvsSYmb9VGSpnmoVzcAUhpBXLq8k
40[ -z "$ARCH" ] && ARCH=$(uname -m)
41if [ -z "$CODENAME" ]; then
42  CODENAME=$(lsb_release -irs 2> /dev/null)
43  if [ -z "$CODENAME" -a -f /etc/lsb-release ]; then
44    . /etc/lsb-release
45    CODENAME=${DISTRIB_CODENAME}
46  fi
47  if [ -z "$CODENAME" -a -f /etc/redhat-release ]; then
48    CODENAME=$(cat /etc/redhat-release | \
49               cut -d '(' -f 1 | sed -e 's/Red Hat Enterprise Linux/rhel/g' -e 's/ release / /g' -e 's/ Linux / /g')
50  fi
51  if [ -z "$CODENAME" ]; then
52    CODENAME="unknown"
53  fi
54fi
55CODENAME=$(echo "$CODENAME" | tr '\n' ' ' | sed -e 's/[[:blank:]]*$//g')
56
57# Convert amd64 to x86_64 (ensure uniformity)
58[ "${ARCH}" = "amd64" ] && ARCH=x86_64
59
60# ############################################################################
61# Config
62# ############################################################################
63
64
65# ############################################################################
66# Functions
67# ############################################################################
68
69function hash ()
70{
71  LIB_NAME="$1"
72  T="
73$(cat ${ROOTDIR}/Makefile.${LIB_NAME})
74$(grep ^CONFIG_ ${ROOTDIR}/.config.mk)
75"
76  H=$(echo "${T}" | sha1sum | cut -d' ' -f1)
77  echo "${H}"
78}
79
80#
81# Attempt to download
82#
83function download
84{
85  LIB_NAME="$1"
86  LIB_HASH=$(hash ${LIB_NAME})
87  P="${BUILDDIR}/.${LIB_NAME}-${LIB_HASH}.tgz"
88
89  # Cleanup
90  rm -f "${BUILDDIR}/.${LIB_NAME}"*.tmp
91
92  # Already downloaded
93  [ -f "${P}" ] && return 0
94
95  # Create directory
96  [ ! -d "${BUILDDIR}" ] && mkdir -p "${BUILDDIR}"
97
98  # Attempt to fetch
99  N="${PCLOUD_BASEDIR}/staticlib/${CODENAME}/${ARCH}/${LIB_NAME}-${LIB_HASH}.tgz"
100
101  echo "DOWNLOAD        ${N} / ${PCLOUD_HASHDIR}"
102  ${ROOTDIR}/support/pcloud.py publink_download "${PCLOUD_HASHDIR}" "${N}" "${P}.tmp"
103
104  R=$?
105
106  # Failed
107  if [ ${R} -ne 0 ]; then
108    echo "FAILED TO DOWNLOAD ${URL} (BUT THIS IS NOT A FATAL ERROR! DO NOT REPORT THAT!)"
109    rm -f "${P}.tmp"
110    return ${R}
111  fi
112
113  # Move tmp file
114  mv "${P}.tmp" "${P}" || return 1
115
116  return ${R}
117}
118
119#
120# Unpack tarball
121#
122function unpack
123{
124  LIB_NAME="$1"
125  LIB_HASH=$(hash ${LIB_NAME})
126  P="${BUILDDIR}/.${LIB_NAME}-${LIB_HASH}.tgz"
127  U="${BUILDDIR}/.${LIB_NAME}.unpack"
128
129  # Not downloaded
130  [ ! -f "${P}" ] && return 1
131
132  # Already unpacked?
133  if [ -f "${U}" ]; then
134    H=$(cat "${U}")
135    [ "${LIB_HASH}" = "${H}" ] && return 0
136  fi
137
138  # Cleanup
139  rm -rf "${BUILDDIR}/${LIB_NAME}" || return 1
140  mkdir -p "${BUILDDIR}/${LIB_NAME}" || return 1
141
142  # Unpack
143  echo "UNPACK          ${P}"
144  tar -C "${BUILDDIR}/${LIB_NAME}" -xf "${P}" || return 1
145
146  # Record
147  echo "${LIB_HASH}" > "${U}"
148}
149
150#
151# Upload
152#
153function upload
154{
155  LIB_NAME=$1; shift
156  LIB_FILES=$*
157  LIB_HASH=$(hash ${LIB_NAME})
158  P="${BUILDDIR}/.${LIB_NAME}-${LIB_HASH}.tgz"
159
160  # Can't upload
161  [ -z "${PCLOUD_USER}" -o -z "${PCLOUD_PASS}" ] && return 0
162
163  # Don't need to upload
164  [ -f "${P}" ] && return 0
165
166  # Make relative paths
167  LIB_FILES=$(echo "${LIB_FILES}" | sed "s#${BUILDDIR}/${LIB_NAME}/##g")
168
169  # Build temporary file
170  echo "PACK            ${P} [${LIB_FILES}]"
171  tar -C ${BUILDDIR}/${LIB_NAME} -zcf ${P}.tmp ${LIB_FILES} || return 1
172
173  # Upload
174  N="${PCLOUD_BASEDIR}/staticlib/${CODENAME}/${ARCH}/${LIB_NAME}-${LIB_HASH}.tgz"
175  echo "UPLOAD          ${N}"
176  ${ROOTDIR}/support/pcloud.py upload "${N}" "${P}.tmp" || return 1
177
178  # Done
179  mv "${P}.tmp" "${P}" || return 1
180}
181
182# Run command
183C=$1; shift
184case "${C}" in
185  hash)
186    hash $*
187    ;;
188  download)
189    download $*
190    ;;
191  unpack)
192    unpack $*
193    ;;
194  upload)
195    upload $*
196    ;;
197esac
198
199# ############################################################################
200# Editor Configuration
201#
202# vim:sts=2:ts=2:sw=2:et
203# ############################################################################
204