Advertisement
Guest User

Untitled

a guest
Mar 11th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.77 KB | None | 0 0
  1. #!/bin/bash
  2. # latest-chrome Version 1.0RC8
  3.  
  4. # This script will find the latest Google Chrome binary package,
  5. # download it and repackage it into Slackware format.
  6.  
  7. # I don't use Chrome for regular browsing but it is handy for
  8. # comparative tests against Opera. :P
  9.  
  10. # Copyright 2013 Ruari Oedegaard, Olso, Norway
  11. # All rights reserved.
  12. #
  13. # Redistribution and use of this script, with or without modification, is
  14. # permitted provided that the following conditions are met:
  15. #
  16. # 1. Redistributions of this script must retain the above copyright
  17. #    notice, this list of conditions and the following disclaimer.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  
  31. # Use the architecture of the current machine or whatever the user has
  32. # set externally
  33. ARCH=${ARCH:-$(uname -m)}
  34.  
  35. if [ "$ARCH" = "x86_64" ]; then
  36.   LIBDIRSUFFIX="64"
  37. elif [[ "$ARCH" = i?86 ]]; then
  38.   ARCH=i386
  39.   LIBDIRSUFFIX=""
  40. else
  41.   echo "The architecture $ARCH is not supported." >&2
  42.   exit 1
  43. fi
  44.  
  45. # Work out the latest stable Google Chrome if VERSION is unset
  46. VERSION=${VERSION:-$(wget -qO- https://dl.google.com/linux/direct/google-chrome-stable_current_i386.rpm | head -c96 | strings | rev | awk -F"[:-]" '/emorhc/ { print $1 "-" $2 }' | rev)}
  47.  
  48. # Error out if $VERISON is unset, e.g. because previous command failed
  49. if [ -z $VERSION ]; then
  50.  echo "Could not work out the latest version; exiting" >&2
  51.  exit 1
  52. fi
  53.  
  54. if echo $VERSION | grep -Fq '-'; then
  55.  GOOGLEREVISION=${VERSION#*-}
  56.  VERSION=${VERSION%-*}
  57. else
  58.  GOOGLEREVISION=""
  59. fi
  60.  
  61. # Don't start repackaging if the same version is already installed
  62. if /bin/ls /var/log/packages/google-chrome-$VERSION-* >/dev/null 2>&1 ; then
  63.   echo "Google Chrome ($VERSION) is already installed; exiting"
  64.   exit 0
  65. fi
  66.  
  67. CWD=$(pwd)
  68. TMP=${TMP:-/tmp}
  69. OUTPUT=${OUTPUT:-/tmp}
  70. BUILD=${BUILD:-1}
  71. TAG=${TAG:-ro}
  72. PKGTYPE=${PKGTYPE:-tgz}
  73. PACKAGE="$OUTPUT/google-chrome-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  74.  
  75. # If the package was made previously, no need to make it again. ;)
  76. if [ -e "$PACKAGE" ]; then
  77.   echo "$PACKAGE already exists; exiting"
  78.   exit 0
  79. fi
  80.  
  81. REPACKDIR=$TMP/repackage-google-chrome
  82.  
  83. # Define this script's name as we will copy into doc directory later on
  84. SCRIPT="${0##*/}"
  85.  
  86. # This function can be used in place of Slackware's makepkg, with the
  87. # added bonus that it is able to make packages with root owned files
  88. # even when run as a regular user.
  89. makepkg() {
  90.  
  91.   # Handle Slackware's makepkg options
  92.   while [ 0 ]; do
  93.     if [ "$1" = "-p" -o "$1" = "--prepend" ]; then
  94.       # option ignored, links are always prepended
  95.       shift 1
  96.     elif [ "$1" = "--linkadd" -o "$1" = "-l" ]; then
  97.       if [ "$2" = "n" ]; then
  98.         echo "\"$1 $2\" ignored, links are always converted" >&2
  99.       fi
  100.       shift 2
  101.     elif [ "$1" = "--chown" -o "$1" = "-c" ]; then
  102.       SETPERMS="$2"
  103.       shift 2
  104.     else
  105.       break
  106.     fi
  107.   done
  108.  
  109.   # Change any symlinks into shell script code
  110.   find * -type l -printf '( cd %h ; rm -rf %f )\n( cd %h ; ln -sf %l %f )\n' -delete > doinst.symlinks
  111.   if grep -q . doinst.symlinks; then
  112.     mkdir -p install
  113.     mv doinst.symlinks install/.
  114.   else
  115.     rm -f doinst.symlinks
  116.   fi
  117.  
  118.   # Prepend symlink shell script to doinst.sh
  119.   if [ -e install/doinst.sh -a -e install/doinst.symlinks ]; then
  120.     printf "\n" | cat - install/doinst.sh >> install/doinst.symlinks
  121.   fi
  122.   if [ -e install/doinst.symlinks ]; then
  123.     mv install/doinst.symlinks install/doinst.sh
  124.   fi
  125.  
  126.   # Reset permissions and ownership
  127.   if [ "${SETPERMS:-y}" = "y" ]; then
  128.     find . -type d -exec chmod 755 {} \;
  129.     TAROWNER="--group 0 --owner 0"
  130.   else
  131.     TAROWNER=""
  132.   fi
  133.  
  134.   # Create package using tar 1.13 directory formatting
  135.   tar $TAROWNER --format=gnu --transform="s,^\./\(.\),\1," --show-stored-names -cavvf "$1" .
  136.   echo "Slackware package $1 created."
  137.  
  138. }
  139.  
  140. # Since packaging is about to begin errors become more important now,
  141. # so exit if things fail.
  142. set -eu
  143.  
  144. # If the repackage is already present from the past, clear it down
  145. # and re-create it.
  146. if [ -d "$REPACKDIR" ]; then
  147.   rm -fr "$REPACKDIR"
  148. fi
  149.  
  150. mkdir -p "$REPACKDIR"/{pkg/install,src}
  151.  
  152. # Save a copy if this script but remove execute persmissions as it will
  153. # larer be moved into the doc directory.
  154. install -m 644 "${0}" "$REPACKDIR/src/$SCRIPT"
  155.  
  156. # Check if the current directory contains the correct Google Chrome
  157. # binary package, otherwise download it. Then check that it matches the
  158. # version number defined.
  159. if [ -e google-chrome-stable-${VERSION}-${GOOGLEREVISION}.${ARCH}.rpm ]; then
  160.   (
  161.     cd "$REPACKDIR/src/"
  162.     ln -s "$CWD/google-chrome-stable-${VERSION}-${GOOGLEREVISION}.${ARCH}.rpm" google-chrome-stable-${VERSION}-${GOOGLEREVISION}.${ARCH}.rpm
  163.   )
  164. else
  165.   echo "Downloading Google Chrome ${VERSION} for ${ARCH}"
  166.   if echo $GOOGLEREVISION | grep -q .; then
  167.     wget https://dl.google.com/linux/chrome/rpm/stable/${ARCH}/google-chrome-stable-${VERSION}-${GOOGLEREVISION}.${ARCH}.rpm -P "$REPACKDIR/src/"
  168.   else
  169.     wget https://dl.google.com/linux/direct/google-chrome-stable_current_${ARCH}.rpm -O "$REPACKDIR/src/google-chrome-stable_current_${ARCH}.rpm"
  170.     DOWNLOADVERSION=$(head -c96 "$REPACKDIR/src/google-chrome-stable_current_${ARCH}.rpm" | strings | rev | awk -F"[:-]" '/emorhc/ { print $2 }' | rev)
  171.     if [ ! "$VERSION" = "$DOWNLOADVERSION" ]; then
  172.       echo "The version downloaded ($DOWNLOADVERSION) is different from the version defined ($VERSION)" >&2
  173.       exit 1
  174.     fi
  175.   fi
  176. fi
  177.  
  178. # Now we have all the sources in place, switch to the package directory
  179. # and start setting things up.
  180. cd "$REPACKDIR/pkg"
  181.  
  182. # Extract the contents of the Google Chrome binary package
  183. if [ -x /usr/bin/bsdtar ]; then
  184.   bsdtar xf ../src/google-chrome-stable*${ARCH}.rpm
  185. elif [ -x /usr/bin/rpm2cpio ]; then
  186.   rpm2cpio ../src/google-chrome-stable*${ARCH}.rpm | cpio --quiet -id
  187. else
  188.   # Since the user has not installed libarchive or rpm, use a hack to extract the rpm contents
  189.   RPMHDRLGTH=$(LANG=C grep -abom1 '.7zXZ\|BZh9' ../src/google-chrome-stable*${ARCH}.rpm)
  190.   case "$RPMHDRLGTH" in
  191.     *7zXZ) COMPRESSOR=xz ;;
  192.     *BZh9) COMPRESSOR=bzip2 ;;
  193.     *)     echo "Unknown compression type in rpm!" >&2; exit 1 ;;
  194.   esac
  195.   tail -c+$[${RPMHDRLGTH%:*}+1] ../src/google-chrome-stable*${ARCH}.rpm | $COMPRESSOR -d | cpio --quiet -id
  196. fi
  197.  
  198. # Remove ./etc as it contains a cron job for updating rpm systems
  199. rm -fr etc
  200.  
  201. # Move any man directories to the correct Slackware location
  202. if [ -d usr/share/man ]; then
  203.   mv usr/share/man usr/
  204. fi
  205.  
  206. # Compress any uncompressed man pages
  207. find usr/man -type f -name "*.1" -exec gzip -9 {} \;
  208. for link in $(find usr/man -type l) ; do
  209.   ln -s $(readlink $link).gz $link.gz
  210.   rm $link
  211. done
  212.  
  213. # Move any doc directory to the correct Slackware location
  214. if [ -d usr/share/doc ]; then
  215.   mv usr/share/doc usr/
  216.   find usr/doc -type d -name "google-chrome*" -maxdepth 1 -exec mv {} usr/doc/tmp \;
  217. fi
  218. mkdir -p usr/doc/tmp
  219. mv usr/doc/tmp usr/doc/google-chrome-$VERSION
  220.  
  221. # Copy this script into the doc directory
  222. cp ../src/$SCRIPT usr/doc/google-chrome-$VERSION/$SCRIPT
  223.  
  224. # Replace google-chrome executable symlink, with a relative one
  225. if [ -h usr/bin/google-chrome ]; then
  226.   rm usr/bin/google-chrome
  227.   (
  228.     cd usr/bin
  229.     ln -fs ../../opt/google/chrome/google-chrome google-chrome
  230.   )
  231. fi
  232.  
  233. # Symlink desktop file
  234. if [ ! -e usr/share/applications/google-chrome.desktop ]; then
  235.   mkdir -p usr/share/applications
  236.   (
  237.     cd usr/share/applications
  238.     ln -s ../../../opt/google/chrome/google-chrome.desktop google-chrome.desktop
  239.   )
  240. fi
  241.  
  242. # Symlink desktop environment icons
  243. for png in opt/google/chrome/product_logo_*.png; do
  244.   pngsize="${png##*/product_logo_}"
  245.   mkdir -p usr/share/icons/hicolor/${pngsize%.png}x${pngsize%.png}/apps
  246.   (
  247.     cd usr/share/icons/hicolor/${pngsize%.png}x${pngsize%.png}/apps/
  248.     ln -s ../../../../../../opt/google/chrome/product_logo_${pngsize} google-chrome.png
  249.   )
  250. done
  251.  
  252. # Now create the post-install to register the desktop file and icons.
  253. cat <<EOS> install/doinst.sh
  254. # Setup menu entries
  255. if command -v update-desktop-database >/dev/null 2>&1; then
  256.   update-desktop-database -q usr/share/applications
  257. fi
  258.  
  259. # Setup icons
  260. touch -c usr/share/icons/hicolor
  261. if command -v gtk-update-icon-cache >/dev/null 2>&1; then
  262.   gtk-update-icon-cache -tq usr/share/icons/hicolor
  263. fi
  264. EOS
  265.  
  266. # Create a description file inside the package.
  267. cat <<EOD> install/slack-desc
  268.                |-----handy-ruler------------------------------------------------------|
  269. google-chrome: google-chrome (Google Chrome web browser)
  270. google-chrome:
  271. google-chrome: Google Chrome is a web browser that combines a minimal design with
  272. google-chrome: sophisticated technology to make the web faster, safer, and easier.
  273. google-chrome:
  274. google-chrome:
  275. google-chrome:
  276. google-chrome:
  277. google-chrome:
  278. google-chrome: Homepage:  http://www.google.com/chrome
  279. google-chrome:
  280. EOD
  281.  
  282. # Make sure the file permissions are ok
  283. chmod -R u+w,go+r-w,a-s .
  284.  
  285. # chrome-sandbox needs to be setuid root for Google Chrome to run
  286. chmod 4711 opt/google/chrome/chrome-sandbox
  287.  
  288. # Create the Slackware package
  289. makepkg --linkadd y --prepend --chown y "$PACKAGE"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement