summaryrefslogtreecommitdiff
path: root/lib/builder-utils.sh
blob: ae08bf5617b606bd7cffeeb4846670bc26d76d27 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

#############################################
# void retrieve_file (src, dst)
#
# Description: retrieve file from an URL
#
# Parameter
#  downloader: program to download the files
#  src: source url to get file from
#  dst: path to store file to
#
#############################################
function  retrieve_file() {
  local downloader=$1
  local src=$2
  local dst=$3
  
  # Check, if the URL is a file URL starting with file://
  if [ -f $dst ] && [ -z ${DIST_FORCE_DOWNLOAD} ]; then
    echo "  Info: File still cached/downloaded. To force a download, set DIST_FORCE_DOWNLOAD=1"
    elif [[ $src == file://* ]]; then
    fileurl=`echo $src | sed "s/^file:\/\///"`
    cp  $fileurl $dst  2>/dev/null
  else
    rm -f $dst
    if [ "$downloader" = "wget" ]; then
      wget  --no-check-certificate --tries=1 -O $dst --timeout=5 -q --no-verbose "$src"
      if [ "$?" == "1" ] ; then
        rm $dst
      fi
      elif [ "$downloader" = "plowdown" ]; then
      plowdown --max-retries=1 -o ${dst%/*} "$src"
      elif [ "$downloader" = "aria2c" ]; then
      #aria2c --seed-time=0 -d ${dst%/*} -o ${dst##*/} "$src"
      aria2c --seed-time=0 --allow-overwrite=true -o ${dst##*/} "$src"
      mv -f ${dst##*/} ${dst%/*}
    else
      fatal_error "Downloader not implemented: $downloader"
    fi
  fi
}

#############################################
# void extract_file (format, src, dst)
#
# Description: Extract a file
#
# Parameter
#  format: compression format
#  src: source file to be used
#  dst: path to extract the file
#
#############################################
function  process_file() {
  
  local format=$1
  local src=$2
  local dst=$3
  local option=$4
  
  log_debug "Compression format: $format"
  
  if [ "$format" = "cp" ]; then
    cp $src $dst
    elif [ "$format" = "7zip" ]; then
    $CMD_7z x -y $option -o$dst $src
    elif [ "$format" = "unzip" ]; then
    $CMD_unzip $option -o $src -d $dst
    elif [ "$format" = "unrar" ]; then
    $CMD_unrar x -y $option $src $dst
    elif [ "$format" = "targz" ]; then
    $CMD_tar xzvf $option $src -C $dst
    elif [ "$format" = "tarbz2" ]; then
    $CMD_tar xjvf $option $src -C $dst
    elif [ "$format" = "cab" ]; then
    $CMD_cabextract $option -d $dst $src
  else
    fatal_error "Unknown compression format: $format"
  fi
}


#############################################
# check if method is available and call it
#############################################
function  call_entry_point() {
  local _resultvar=$1
  local func=$2
  
  # Entry point
  type $func &>/dev/null
  if [ $? == 0 ] ; then
    $func
    eval $_resultvar="0"
  else
    eval $_resultvar="1"
  fi
  
}

###################
# Fata Error
###################
fatal_error() {
  echo "FATAL: $1"
  exit 1
}

###################
# Check error
###################
builder_check_error() {
  if [ "$?" == "1" ] ; then
    fatal_error "$1"
  fi
}

###################
# Logging Debug
###################
log_debug() {
  local str=$1
  
  if [ "$DEBUG_LEVEL" = "debug" ] ||  [ "$DEBUG_LEVEL" = "info" ]  ; then
    echo $str
  fi
}

###################
# Logging Info
###################
log_info() {
  local str=$1
  
  if [ "$DEBUG_LEVEL" = "info" ]  ; then
    echo $str
  fi
}

#####################
# void calc_release()
#
# Description:
#   Calculate new release based on
#   the latest one found in the repository.
#
# $RELEASE is set to the calculated release.
#
####################
function calc_release() {
  
  # Find all revision files and sort them
  local file_list=`mktemp /tmp/aio-builder.calc_release.XXXXXXXXXXX`
  for cfg_file in `find ${AIO_REPOS_BASE_DIR} -name "${PN}-${VERSION}-${CREATOR_TAG}*.cfg" -print ` ; do
    . ${cfg_file}
    printf "%08d;$cfg_file\n" $REV_RELEASE >> ${file_list}
  done
  local oldest_cfg=`sort -n ${file_list} | cut -f 2 -d ";" | tail -1`
  rm -f ${file_list}
  
  if [ ! -f "${oldest_cfg}" ] ; then
    echo "Warning: no cfg-file found for this product. RELEASE will be set to 1"
    RELEASE=1
  else
    log_debug "calc_release() oldest_cfg: ${oldest_cfg}"
    . ${oldest_cfg}
    log_debug "  latest release: $REV_RELEASE"
    RELEASE=`expr ${REV_RELEASE} + 1 2> /dev/null`
    builder_check_error "Cannot incrememnt REV_RELEASE from file ${oldest_cfg}"
  fi
}

###################
# Write Package Ini file
#
# Create a ini file containing needed information for package uninstall
#
# Parameter
#  file: file to create / modify
#
###################
write_ini_file() {
  local ini_file=$1
  local ini_section=$2
  local ini_option=$3
  local ini_value=$4

  touch $ini_file
  $CMD_inifile $(cygpath -pw "$ini_file") [$ini_section] $ini_option=$ini_value
}


###################
# Get Verison of File
#
# Copy file only if it is newer
#
# Parameter
#  File: to process
#  Var:
#
###################
vercomp() {
  [ ! $(echo -e "$1\n$2" | sort --version-sort | head -1) = "$1" ]
}

###################
# Get Verison of File
#
# Copy file only if it is newer
#
# Parameter
#  File: to process
#  Var:
#
###################


get_file_ver() {
  local getver_file=$2
  local __resultvar=$1
  local myresult=`$CMD_showver $(cygpath -pw "$getver_file") | grep FileVersion: | rev | cut -d: -f1 | rev | tr -d ' '`
  echo "$getver_file -> $myresult"
  eval $__resultvar="'$myresult'"
}

###################
# Copy Verison
#
# Copy file only if it is newer
#
# Parameter
#  source: sourcefiel
#  target: Targetfile
#
###################
copyver() {
  local source_file=$1
  local target_file=$2
  local source_ver=""
  local target_ver=""
  
  echo "Source File: $source_file Target Version: $target_file"
  if [ -f "$source_file" ] && [ -f "$target_file" ]  ; then
    get_file_ver source_ver $source_file
    get_file_ver target_ver $target_file
    if [ vercomp $source_ver $target_ver ] ; then
      echo "Source Verssion: $source_ver > Target Version: $target_ver"
      echo "Copying File"
      cp $source_file $target_file
    else
      echo "Source Verssion: $source_ver =< Target Version: $target_ver"
      echo "Not Copying File"
    fi
  elif [ -f "$source_file" ] && [ ! -f "$target_file" ]  ; then
      source_ver=$(get_file_version $source_file)
      echo "Source Verssion: $source_ver"
      echo "Copying File"
      cp $source_file $target_file
  else
    echo "Nothing to copy"
 fi
}

###################
# Tidy xml
#
# Tidy XML files (mainly Sereby package.xml)
#
# Parameter
#  source: sourcefiel
#  target: Targetfile
#
###################
tidy_xml() {
  local source_file=$1
  local target_file=$2
  
  cat $source_file | $CMD_tidy -utf8 -xml -w 255 -i -c -q -asxml | sed -e 's!&gt;!>!g' -e 's!&lt;!<!g' > $target_file
}

#############################################
# void extract_file (format, src, dst)
#
# Description: Extract a file
#
# Parameter
#  format: compression format
#  src: source file to be used
#  dst: path to extract the file
#
#############################################
function  extract_file() {
  
  local format=$1
  local src=$2
  local dst=$3
  local option=$4
  local src_dir=`dirname "$src"`
  local src_file=`basename "$src"`
  local dst_dir=`dirname "$dst"`
  local dst_file=`basename "$dst"`
  
  log_debug "Compression format: $format"
  
  mkdir -p $dst
  if [ "$format" = "msi-a" ]; then
    mkdir /cygdrive/c/msi-adm
    pushd $src_dir
    echo $src_file
    $CMD_msiexec /a "$src_file" $option TARGETDIR="c:\msi-adm"
    popd
    cp -r /cygdrive/c/msi-adm/* $dst
    rm -rf /cygdrive/c/msi-adm
    elif [ "$format" = "msp-a" ]; then
    pushd $dst_dir
    cp $src $dst_dir
    $CMD_msiexec /p "$src_file" /a "$dst_file" SHORTFILENAMES=TRUE /qb 
    popd
    elif [ "$format" = "7zip" ]; then
    $CMD_7z x -y $option -o$dst $src
    elif [ "$format" = "unzip" ]; then
    $CMD_unzip $option -o $src -d $dst
    elif [ "$format" = "unrar" ]; then
    $CMD_unrar x -y $option $(cygpath -pw "$src") $(cygpath -pw "$dst")
    elif [ "$format" = "cab" ]; then
    $CMD_cabextract $option -d $dst $src
    elif [ "$format" = "cab-sfx" ]; then
    $CMD_cabextract $option -d $dst $src
    $CMD_xmlstarlet sel -N X="http://schemas.microsoft.com/wix/2008/Burn" -t -m "//X:Payload" -v "concat(@SourcePath,'-',@FilePath)" -n $dst/0 | tr '\\' '/' > $dst/rename.list
    for i in `cat $dst/rename.list`
    do
	pushd $dst
	local src_var=$(echo $i | cut -f1 -d-)
	local dst_var=$(echo $i | cut -f2 -d-)
	mkdir -p $(dirname $dst_var)
        mv -v $src_var $dst_var
        popd
    done
    elif [ "$format" = "inno" ]; then
    $CMD_innounp $option -d $dst $src
  else
    fatal_error "Unknown compression format: $format"
  fi
}

Powered by Domaintechnik.at | Hosting - Domain - Webspace