michael@0: #!/bin/bash michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: # michael@0: # Code shared by update packaging scripts. michael@0: # Author: Darin Fisher michael@0: # michael@0: michael@0: # ----------------------------------------------------------------------------- michael@0: QUIET=0 michael@0: michael@0: # By default just assume that these tools exist on our path michael@0: MAR=${MAR:-mar} michael@0: BZIP2=${BZIP2:-bzip2} michael@0: MBSDIFF=${MBSDIFF:-mbsdiff} michael@0: michael@0: # ----------------------------------------------------------------------------- michael@0: # Helper routines michael@0: michael@0: notice() { michael@0: echo "$*" 1>&2 michael@0: } michael@0: michael@0: verbose_notice() { michael@0: if [ $QUIET -eq 0 ]; then michael@0: notice "$*" michael@0: fi michael@0: } michael@0: michael@0: get_file_size() { michael@0: info=($(ls -ln "$1")) michael@0: echo ${info[4]} michael@0: } michael@0: michael@0: copy_perm() { michael@0: reference="$1" michael@0: target="$2" michael@0: michael@0: if [ -x "$reference" ]; then michael@0: chmod 0755 "$target" michael@0: else michael@0: chmod 0644 "$target" michael@0: fi michael@0: } michael@0: michael@0: make_add_instruction() { michael@0: f="$1" michael@0: filev2="$2" michael@0: # The third param will be an empty string when a file add instruction is only michael@0: # needed in the version 2 manifest. This only happens when the file has an michael@0: # add-if-not instruction in the version 3 manifest. This is due to the michael@0: # precomplete file prior to the version 3 manifest having a remove instruction michael@0: # for this file so the file is removed before applying a complete update. michael@0: filev3="$3" michael@0: michael@0: # Used to log to the console michael@0: if [ $4 ]; then michael@0: forced=" (forced)" michael@0: else michael@0: forced= michael@0: fi michael@0: michael@0: is_extension=$(echo "$f" | grep -c 'distribution/extensions/.*/') michael@0: if [ $is_extension = "1" ]; then michael@0: # Use the subdirectory of the extensions folder as the file to test michael@0: # before performing this add instruction. michael@0: testdir=$(echo "$f" | sed 's/\(.*distribution\/extensions\/[^\/]*\)\/.*/\1/') michael@0: verbose_notice " add-if \"$testdir\" \"$f\"" michael@0: echo "add-if \"$testdir\" \"$f\"" >> "$filev2" michael@0: if [ ! "$filev3" = "" ]; then michael@0: echo "add-if \"$testdir\" \"$f\"" >> "$filev3" michael@0: fi michael@0: else michael@0: verbose_notice " add \"$f\"$forced" michael@0: echo "add \"$f\"" >> "$filev2" michael@0: if [ ! "$filev3" = "" ]; then michael@0: echo "add \"$f\"" >> "$filev3" michael@0: fi michael@0: fi michael@0: } michael@0: michael@0: check_for_add_if_not_update() { michael@0: add_if_not_file_chk="$1" michael@0: michael@0: if [ `basename $add_if_not_file_chk` = "channel-prefs.js" -o \ michael@0: `basename $add_if_not_file_chk` = "update-settings.ini" ]; then michael@0: ## "true" *giggle* michael@0: return 0; michael@0: fi michael@0: ## 'false'... because this is bash. Oh yay! michael@0: return 1; michael@0: } michael@0: michael@0: check_for_add_to_manifestv2() { michael@0: add_if_not_file_chk="$1" michael@0: michael@0: if [ `basename $add_if_not_file_chk` = "update-settings.ini" ]; then michael@0: ## "true" *giggle* michael@0: return 0; michael@0: fi michael@0: ## 'false'... because this is bash. Oh yay! michael@0: return 1; michael@0: } michael@0: michael@0: make_add_if_not_instruction() { michael@0: f="$1" michael@0: filev3="$2" michael@0: michael@0: verbose_notice " add-if-not \"$f\" \"$f\"" michael@0: echo "add-if-not \"$f\" \"$f\"" >> "$filev3" michael@0: } michael@0: michael@0: make_addsymlink_instruction() { michael@0: link="$1" michael@0: target="$2" michael@0: filev2="$3" michael@0: filev3="$4" michael@0: michael@0: verbose_notice " addsymlink: $link -> $target" michael@0: echo "addsymlink \"$link\" \"$target\"" >> "$filev2" michael@0: echo "addsymlink \"$link\" \"$target\"" >> "$filev3" michael@0: } michael@0: michael@0: make_patch_instruction() { michael@0: f="$1" michael@0: filev2="$2" michael@0: filev3="$3" michael@0: michael@0: is_extension=$(echo "$f" | grep -c 'distribution/extensions/.*/') michael@0: if [ $is_extension = "1" ]; then michael@0: # Use the subdirectory of the extensions folder as the file to test michael@0: # before performing this add instruction. michael@0: testdir=$(echo "$f" | sed 's/\(.*distribution\/extensions\/[^\/]*\)\/.*/\1/') michael@0: verbose_notice " patch-if \"$testdir\" \"$f.patch\" \"$f\"" michael@0: echo "patch-if \"$testdir\" \"$f.patch\" \"$f\"" >> "$filev2" michael@0: echo "patch-if \"$testdir\" \"$f.patch\" \"$f\"" >> "$filev3" michael@0: else michael@0: verbose_notice " patch \"$f.patch\" \"$f\"" michael@0: echo "patch \"$f.patch\" \"$f\"" >> "$filev2" michael@0: echo "patch \"$f.patch\" \"$f\"" >> "$filev3" michael@0: fi michael@0: } michael@0: michael@0: append_remove_instructions() { michael@0: dir="$1" michael@0: filev2="$2" michael@0: filev3="$3" michael@0: michael@0: if [ -f "$dir/removed-files" ]; then michael@0: prefix= michael@0: listfile="$dir/removed-files" michael@0: elif [ -f "$dir/Contents/MacOS/removed-files" ]; then michael@0: prefix=Contents/MacOS/ michael@0: listfile="$dir/Contents/MacOS/removed-files" michael@0: fi michael@0: if [ -n "$listfile" ]; then michael@0: # Map spaces to pipes so that we correctly handle filenames with spaces. michael@0: files=($(cat "$listfile" | tr " " "|" | sort -r)) michael@0: num_files=${#files[*]} michael@0: for ((i=0; $i<$num_files; i=$i+1)); do michael@0: # Map pipes back to whitespace and remove carriage returns michael@0: f=$(echo ${files[$i]} | tr "|" " " | tr -d '\r') michael@0: # Trim whitespace michael@0: f=$(echo $f) michael@0: # Exclude blank lines. michael@0: if [ -n "$f" ]; then michael@0: # Exclude comments michael@0: if [ ! $(echo "$f" | grep -c '^#') = 1 ]; then michael@0: # Normalize the path to the root of the Mac OS X bundle if necessary michael@0: fixedprefix="$prefix" michael@0: if [ $prefix ]; then michael@0: if [ $(echo "$f" | grep -c '^\.\./') = 1 ]; then michael@0: if [ $(echo "$f" | grep -c '^\.\./\.\./') = 1 ]; then michael@0: f=$(echo $f | sed -e 's:^\.\.\/\.\.\/::') michael@0: fixedprefix="" michael@0: else michael@0: f=$(echo $f | sed -e 's:^\.\.\/::') michael@0: fixedprefix=$(echo "$prefix" | sed -e 's:[^\/]*\/$::') michael@0: fi michael@0: fi michael@0: fi michael@0: if [ $(echo "$f" | grep -c '\/$') = 1 ]; then michael@0: verbose_notice " rmdir \"$fixedprefix$f\"" michael@0: echo "rmdir \"$fixedprefix$f\"" >> "$filev2" michael@0: echo "rmdir \"$fixedprefix$f\"" >> "$filev3" michael@0: elif [ $(echo "$f" | grep -c '\/\*$') = 1 ]; then michael@0: # Remove the * michael@0: f=$(echo "$f" | sed -e 's:\*$::') michael@0: verbose_notice " rmrfdir \"$fixedprefix$f\"" michael@0: echo "rmrfdir \"$fixedprefix$f\"" >> "$filev2" michael@0: echo "rmrfdir \"$fixedprefix$f\"" >> "$filev3" michael@0: else michael@0: verbose_notice " remove \"$fixedprefix$f\"" michael@0: echo "remove \"$fixedprefix$f\"" >> "$filev2" michael@0: echo "remove \"$fixedprefix$f\"" >> "$filev3" michael@0: fi michael@0: fi michael@0: fi michael@0: done michael@0: fi michael@0: } michael@0: michael@0: # List all files in the current directory, stripping leading "./" michael@0: # Pass a variable name and it will be filled as an array. michael@0: # To support Tor Browser updates, skip the following files: michael@0: # TorBrowser/Data/Browser/profiles.ini michael@0: # TorBrowser/Data/Browser/profile.default/bookmarks.html michael@0: # TorBrowser/Data/Tor/torrc michael@0: list_files() { michael@0: count=0 michael@0: michael@0: find . -type f \ michael@0: ! -name "update.manifest" \ michael@0: ! -name "updatev2.manifest" \ michael@0: ! -name "updatev3.manifest" \ michael@0: ! -name "temp-dirlist" \ michael@0: ! -name "temp-filelist" \ michael@0: | sed 's/\.\/\(.*\)/\1/' \ michael@0: | sort -r > "temp-filelist" michael@0: while read file; do michael@0: if [ "$file" = "TorBrowser/Data/Browser/profiles.ini" -o \ michael@0: "$file" = "TorBrowser/Data/Browser/profile.default/bookmarks.html" -o \ michael@0: "$file" = "TorBrowser/Data/Tor/torrc" ]; then michael@0: continue; michael@0: fi michael@0: eval "${1}[$count]=\"$file\"" michael@0: (( count++ )) michael@0: done < "temp-filelist" michael@0: rm "temp-filelist" michael@0: } michael@0: michael@0: # List all directories in the current directory, stripping leading "./" michael@0: list_dirs() { michael@0: count=0 michael@0: michael@0: find . -type d \ michael@0: ! -name "." \ michael@0: ! -name ".." \ michael@0: | sed 's/\.\/\(.*\)/\1/' \ michael@0: | sort -r > "temp-dirlist" michael@0: while read dir; do michael@0: eval "${1}[$count]=\"$dir\"" michael@0: (( count++ )) michael@0: done < "temp-dirlist" michael@0: rm "temp-dirlist" michael@0: } michael@0: michael@0: # List all symbolic links in the current directory, stripping leading "./" michael@0: list_symlinks() { michael@0: count=0 michael@0: michael@0: find . -type l \ michael@0: | sed 's/\.\/\(.*\)/\1/' \ michael@0: | sort -r > "temp-symlinklist" michael@0: while read symlink; do michael@0: target=$(readlink "$symlink") michael@0: eval "${1}[$count]=\"$symlink\"" michael@0: eval "${2}[$count]=\"$target\"" michael@0: (( count++ )) michael@0: done < "temp-symlinklist" michael@0: rm "temp-symlinklist" michael@0: }