Advertisement
Guest User

apt-cyg offline-install supported - v 2.4b

a guest
Aug 15th, 2012
3,291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 14.19 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # apt-cyg: install tool for cygwin similar to debian apt-get
  4. #
  5. # Copyright (C) 2005-9, Stephen Jungels
  6. # Modified by GreenFox v2.4b
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. # GNU General Public License for more details.
  17. #
  18. # (http://www.fsf.org/licensing/licenses/gpl.html)
  19.  
  20. # this script requires some packages
  21.  
  22. WGET=`which wget 2> /dev/null`
  23. BZIP2=`which bzip2 2> /dev/null`
  24. TAR=`which tar 2> /dev/null`
  25. GAWK=`which awk 2> /dev/null`
  26. if test "-$WGET-" = "--" || test "-$BZIP2-" = "--" || test "-$TAR-" = "--" \
  27.   || test "-$GAWK-" = "--"
  28. then
  29.   echo You must install wget, tar, gawk and bzip2 to use apt-cyg.
  30.   exit 1
  31. fi
  32.  
  33.  
  34. function usage()
  35. {
  36.   echo apt-cyg: Installs and removes Cygwin packages.
  37.   echo "  \"apt-cyg install <package names>\" to install packages"
  38.   echo "  \"apt-cyg remove <package names>\" to remove packages"
  39.   echo "  \"apt-cyg update\" to update setup.ini"
  40.   echo "  \"apt-cyg show\" to show installed packages"
  41.   echo "  \"apt-cyg find <patterns>\" to find packages matching patterns"
  42.   echo "  \"apt-cyg describe <patterns>\" to describe packages matching patterns"
  43.   echo "  \"apt-cyg packageof <commands or files>\" to locate parent packages"
  44.   echo "Options:"
  45.   echo "  --mirror, -m <url> : set mirror"
  46.   echo "  --cache, -c <dir>  : set cache"
  47.   echo "  --file, -f <file>  : read package names from file"
  48.   echo "  --noupdate, -u     : don't update setup.ini from mirror"
  49.   echo "  --localdiskcache   : Use local cache as source, if available. Used for offline instalation."
  50.   echo "  --force            : Force re-installing package. USE WITH CAUTION AND CARE!"
  51.   echo "  --ForceUpdate      : Force re-initalizeing setup.ini.  "
  52.   echo "  --help"
  53.   echo "  --version"
  54.   echo "  --find-setup-exe   :experemental"
  55. }
  56.  
  57.  
  58.  
  59. function version()
  60. {
  61.   echo "apt-cyg version 0.58 custombuild"
  62.   echo "Written by Stephen Jungels"
  63.   echo "Modified by GreenFox v2.4b"
  64.   echo "Copyright (c) 2005-2012.  Released under the GPL."
  65. }
  66.  
  67. function findsetupexe()
  68. {
  69.   echo "Attempting to load setup.exe, or loading from network..."
  70.   [ -e "${cache}/setup.exe" ] && echo "Try using [ ${cache}/setup.exe ]"
  71. }
  72.  
  73. function findworkspace()
  74. {
  75.   # default working directory and mirror
  76.  
  77.   mirror="ftp://mirror.mcs.anl.gov/pub/cygwin"
  78.   mirror="ftp://ftp.iij.ad.jp/pub/cygwin"
  79.   cache="/cygdrive/c/archive/cygwin"
  80.   # Note: path should be in unix format.
  81.  
  82.   # work wherever setup worked last, if possible
  83.  
  84.   if test -e /etc/setup/last-cache
  85.   then
  86.     tmp="`head -1 /etc/setup/last-cache`"
  87.     cache="`cygpath -au "$tmp"`"
  88.   fi
  89.  
  90.   if test -e /etc/setup/last-mirror
  91.   then
  92.     mirror="`head -1 /etc/setup/last-mirror`"
  93.   fi
  94.   if test "$localdiskcache" == "1"
  95.   then
  96.     echo Working directory is $cache
  97.     echo Mirror is $mirror
  98.     echo We are using the cache as package source.
  99.     cd "$cache"
  100.   else
  101.     # Old method that uses network. Left for compatibility purpose.
  102.     mirrordir="`echo "$mirror" | sed -e "s/:/%3a/g" -e "s:/:%2f:g"`"
  103.     echo Working directory is $cache
  104.     echo Mirror is $mirror
  105.     mkdir -p "$cache/$mirrordir"
  106.     cd "$cache/$mirrordir"
  107.   fi
  108. }
  109.  
  110.  
  111. function getsetup()
  112. {
  113.   if test "$noscripts" == "0" -a "$noupdate" == "0" -a "$localdiskcache" == "0"
  114.   then
  115.     touch "setup.ini"
  116.     mv "setup.ini" "setup.ini-save"
  117.     wget -N "$mirror/setup.bz2"
  118.     if test -e "setup.bz2" && test $? -eq 0
  119.     then
  120.       bunzip2 "setup.bz2"
  121.       mv "setup" "setup.ini"
  122.       echo "Updated setup.ini"
  123.     else
  124.       wget -N "$mirror/setup.ini"
  125.       if test -e setup.ini && test $? -eq 0
  126.       then
  127.         echo "Updated setup.ini"
  128.       else
  129.         mv "setup.ini-save" "setup.ini"
  130.         echo "Error updating setup.ini, reverting"
  131.       fi
  132.     fi
  133.   fi
  134. }
  135.  
  136.  
  137. function checkpackages()
  138. {
  139.   if test "-$packages-" = "--"
  140.   then
  141.     echo Nothing to do, exiting
  142.     exit 0
  143.   fi
  144. }
  145. function apt-cyg-func-install(){
  146.   for pkg in $packages
  147.     do
  148.       already="`grep -c "^$pkg " /etc/setup/installed.db`"
  149.       if test $already -ge 1 -a "$forceinstall" == "0"
  150.       then
  151.         echo "Package $pkg is already installed, skipping"
  152.         continue
  153.       fi
  154.       echo ""
  155.       echo Installing $pkg
  156.       # look for package and save desc file
  157.       mkdir -p "release/$pkg"
  158.       cat setup.ini | awk > "release/$pkg/desc" -v package="$pkg" \
  159.         'BEGIN{RS="\n\n@ "; FS="\n"} {if ($1 == package) {desc = $0; px++}} \
  160.         END{if (px == 1 && desc != "") print desc; else print "Package not found"}'
  161.    
  162.       desc=`cat "release/$pkg/desc"`
  163.       if test "-$desc-" = "-Package not found-"
  164.       then
  165.         echo Package $pkg not found or ambiguous name, exiting
  166.         rm -r "release/$pkg"
  167.         exit 1
  168.       fi
  169.       echo Found package $pkg
  170.  
  171.       # download and unpack the bz2 file
  172.  
  173.       # pick the latest version, which comes first
  174.       install=`cat "release/$pkg/desc" | awk '/^install: / { print $2; exit }'`
  175.  
  176.       if test "-$install-" = "--"
  177.       then
  178.         echo "Could not find \"install\" in package description: obsolete package?"
  179.         echo "Debug output of [$(realpath "release/$pkg/desc")]"
  180.         echo "-----------------------------------------------------"
  181.         cat "release/$pkg/desc"
  182.         echo "-----------------------------------------------------"
  183.         exit 1
  184.       fi
  185.       file=`basename $install`
  186.       if test "$localdiskcache" == "1"
  187.       then
  188. ############################################
  189. # This section needs refactoring. TODO
  190. ############################################
  191.         echo "DBG:pwd=[`pwd`]pkg=[$pkg]file=[$file]mirror=[$mirror]install=[$install]"
  192.         cd "$cache"
  193.         cd "release/$pkg"
  194.         if ! test -e "$file"
  195.         then
  196.           # Scan dir for matching name
  197.           echo "Scanning cache for leftover..."
  198.           local leftover_file="`find "$cache" -name "$file" |head -n 1`";
  199.           if test "$leftover_file" == "" ; then
  200.             #
  201.             echo "Package $pkg missing from cache. Downloading file from mirror $mirror."
  202.             wget -nc $mirror/$install
  203.             # The mirror might not have the file.TODO.
  204.           else
  205.             echo "Trying to use [$leftover_file] as alternative to [$file]"
  206.             file="$leftover_file";
  207.           fi
  208.         fi
  209.         echo "DBG:169:pwd=[`pwd`]pkg=[$pkg]file=[$file]mirror=[$mirror]install=[$install]"
  210.  
  211.           digest="`cat "desc" | awk '/^install: / { print $4; exit }'`";
  212.           digactual="`md5sum "$file" | awk '{print $1}'`";
  213.           echo "Checking md5hash [$digest] == [$digactual] "
  214.           if test "$digest" != "$digactual" ;
  215.           then
  216.             echo "Package $pkg in cache data is broken, partial, or empty. Downloading file from mirror."
  217.             wget -c $mirror/$install
  218.           fi
  219.           digactual=`md5sum $file | awk '{print $1}'`
  220. ############################################
  221.       else
  222.         cd "release/$pkg"
  223.         wget -nc $mirror/$install
  224.         digest=`cat "desc" | awk '/^install: / { print $4; exit }'`
  225.         digactual=`md5sum $file | awk '{print $1}'`
  226.       fi
  227.  
  228.       if ! test $digest = $digactual
  229.       then
  230.         echo "MD5 sum did not match, exiting"
  231.         exit 1
  232.       fi
  233.    
  234.       echo "Unpacking..."
  235.       #####
  236.       # Dumps to root dir. We need to add code for chroot creating options
  237.       ####
  238.       cat "$file" | bunzip2 | tar > "/etc/setup/$pkg.lst" xvf - -C /
  239.       gzip -f "/etc/setup/$pkg.lst"
  240.       cd ../..
  241.    
  242.    
  243.       # update the package database
  244.    
  245.       cat /etc/setup/installed.db | awk > /tmp/awk.$$ -v pkg="$pkg" -v bz=$file \
  246.         '{if (ins != 1 && pkg < $1) {print pkg " " bz " 0"; ins=1}; print $0} \
  247.         END{if (ins != 1) print pkg " " bz " 0"}'
  248.       mv /etc/setup/installed.db /etc/setup/installed.db-save
  249.       mv /tmp/awk.$$ /etc/setup/installed.db
  250.    
  251.    
  252.       # recursively install required packages
  253.    
  254.       echo > /tmp/awk.$$ '/^requires: / {s=gensub("(requires: )?([^ ]+) ?", "\\2 ", "g", $0); print s}'
  255.       requires=`cat "release/$pkg/desc" | awk -f /tmp/awk.$$`
  256.    
  257.       warn=0
  258.       if ! test "-$requires-" = "--"
  259.       then
  260.         echo Package $pkg requires the following packages, installing:
  261.         echo $requires
  262.         for package in $requires
  263.         do
  264.           already=`grep -c "^$package " /etc/setup/installed.db`
  265.           if test $already -ge 1
  266.           then
  267.             echo Package $package is already installed, skipping
  268.             continue
  269.           fi
  270.           apt-cyg --noscripts install $package
  271.           if ! test $? = 0 ; then warn=1; fi
  272.         done
  273.       fi
  274.       if ! test $warn = 0
  275.       then
  276.         echo "Warning: some required packages did not install, continuing"
  277.       fi
  278.    
  279.       # run all postinstall scripts
  280.    
  281.       pis=`ls "/etc/postinstall/*.sh" 2>/dev/null | wc -l`
  282.       if test $pis -gt 0 && ! test $noscripts -eq 1
  283.       then
  284.         echo Running postinstall scripts
  285.         for script in "/etc/postinstall/*.sh"
  286.         do
  287.           $script
  288.           mv $script $script.done
  289.         done
  290.       fi
  291.      
  292.       echo Package $pkg installed
  293.       done
  294.  
  295. }
  296.  
  297. function main(){
  298.   # process options
  299.   local noscripts=0
  300.   local noupdate=0
  301.   local file=""
  302.   local dofile=0
  303.   local command=""
  304.   local filepackages=""
  305.   local packages=""
  306.   local localdiskcache=1
  307.   local forceinstall=0
  308.  
  309. # use vars = [ noscripts noupdate localdiskcache forceinstall file dofile command packages ]
  310.   while test $# -gt 0
  311.   do
  312.   case "$1" in
  313.     --mirror|-m)
  314.       echo "$2" > /etc/setup/last-mirror
  315.       shift ; shift
  316.     ;;
  317.     --cache|-c)
  318.       cygpath -aw "$2" > /etc/setup/last-cache
  319.       shift ; shift
  320.     ;;
  321.     --noscripts)
  322.       noscripts=1
  323.       shift
  324.     ;;
  325.     --noupdate|-u)
  326.       noupdate=1
  327.       shift
  328.     ;;
  329.     --ForceUpdate)
  330.       noupdate=0
  331.       localdiskcache=0
  332.       shift
  333.     ;;
  334.     --localdiskcache)
  335.       localdiskcache=1
  336.       shift
  337.     ;;
  338.     --force)
  339.       forceinstall=1
  340.       shift
  341.     ;;
  342.     --help)
  343.       usage
  344.       exit 0
  345.     ;;
  346.     --version)
  347.       version
  348.       exit 0
  349.     ;;
  350.     --find-setup-exe)
  351.       findsetupexe
  352.       exit 0
  353.     ;;
  354.     --file|-f)
  355.       if ! test "-$2-" = "--"
  356.       then
  357.         file="$2"
  358.         dofile=1
  359.         shift
  360.       else
  361.         echo 1>&2 No file name provided, ignoring $1
  362.       fi
  363.       shift
  364.     ;;
  365.     update|show|find|describe|packageof|install|remove)
  366.       if test "-$command-" = "--"
  367.       then
  368.         command=$1
  369.       else
  370.         packages="$packages $1"
  371.       fi
  372.       shift
  373.     ;;
  374.     *)
  375.       packages="$packages $1"
  376.       shift
  377.     ;;
  378.   esac
  379.   done
  380.   ######################################################
  381.   # Done reading the options. Now process accordingly
  382.   ######################################################
  383.   if test $dofile = 1
  384.   then
  385.     if test -f "$file"
  386.     then
  387.       filepackages="$filepackages `cat "$file" | awk '{printf "%s ", $0}'`"
  388.     else
  389.       echo File $file not found, skipping
  390.     fi
  391.     packages="$packages $filepackages"
  392.   fi
  393.  
  394.   case "$command" in
  395.   update)
  396.     findworkspace
  397.     getsetup
  398.   ;;
  399.   show)
  400.     echo 1>&2 The following packages are installed:
  401.     cat /etc/setup/installed.db | awk '/[^ ]+ [^ ]+ 0/ {print $1}'
  402.   ;;
  403.   find)
  404.     checkpackages
  405.     findworkspace
  406.     getsetup
  407.     for pkg in $packages
  408.     do
  409.       echo ""
  410.       echo Searching for installed packages matching $pkg:
  411.       awk '/[^ ]+ [^ ]+ 0/ {if ($1 ~ query) print $1}' query="$pkg" /etc/setup/installed.db
  412.       echo ""
  413.       echo Searching for installable packages matching $pkg:
  414.       cat setup.ini | awk -v query="$pkg" \
  415.         'BEGIN{RS="\n\n@ "; FS="\n"; ORS="\n"} {if ($1 ~ query) {print $1}}'
  416.     done
  417.   ;;
  418.   describe)
  419.     checkpackages
  420.     findworkspace
  421.     getsetup
  422.     for pkg in $packages
  423.     do
  424.       echo ""
  425.       cat setup.ini | awk -v query="$pkg" \
  426.         'BEGIN{RS="\n\n@ "; FS="\n"; ORS="\n"} {if ($1 ~ query) {print $0 "\n"}}'
  427.     done
  428.   ;;
  429.   packageof)
  430.     checkpackages
  431.     for pkg in $packages
  432.     do
  433.       key=`which "$pkg" 2>/dev/null | sed "s:^/::"`
  434.       if test "-$key-" = "--"
  435.       then
  436.         key="$pkg"
  437.       fi
  438.       for manifest in "/etc/setup/*.lst.gz"
  439.       do
  440.         found=`cat "$manifest" | gzip -d | grep -c "$key"`
  441.         if test $found -gt 0
  442.         then
  443.           package=`echo "$manifest" | sed -e "s:/etc/setup/::" -e "s/.lst.gz//"`
  444.           echo Found $key in the package $package
  445.         fi
  446.       done
  447.     done
  448.   ;;
  449.   install)
  450.     checkpackages
  451.     findworkspace
  452.     getsetup
  453.     apt-cyg-func-install
  454.     ;;
  455.   remove)
  456.     checkpackages
  457.     for pkg in $packages
  458.     do
  459.       already=`grep -c "^$pkg " /etc/setup/installed.db`
  460.       if test $already = 0
  461.       then
  462.         echo Package $pkg is not installed, skipping
  463.         continue
  464.       fi
  465.       dontremove="cygwin coreutils gawk bzip2 tar wget bash"
  466.       for req in $dontremove
  467.       do
  468.         if test "-$pkg-" = "-$req-"
  469.         then
  470.           echo apt-cyg cannot remove package $pkg, exiting
  471.           exit 1
  472.         fi
  473.       done
  474.       if ! test -e "/etc/setup/$pkg.lst.gz"
  475.       then
  476.         echo Package manifest missing, cannot remove $pkg.  Exiting
  477.         exit 1
  478.      fi
  479.       echo Removing $pkg
  480.       # run preremove scripts
  481.       if test -e "/etc/preremove/$pkg.sh"
  482.       then
  483.         "/etc/preremove/$pkg.sh"
  484.         rm "/etc/preremove/$pkg.sh"
  485.       fi
  486.       cat "/etc/setup/$pkg.lst.gz" | gzip -d | awk '/[^\/]$/ {print "rm -f \"/" $0 "\""}' | sh
  487.       rm "/etc/setup/$pkg.lst.gz"
  488.       rm -f /etc/postinstall/$pkg.sh.done
  489.       cat /etc/setup/installed.db | awk > /tmp/awk.$$ -v pkg="$pkg" '{if (pkg != $1) print $0}'
  490.       mv /etc/setup/installed.db /etc/setup/installed.db-save
  491.       mv /tmp/awk.$$ /etc/setup/installed.db
  492.       echo Package $pkg removed
  493.     done
  494.   ;;
  495.   *)
  496.     usage
  497.   ;;
  498. esac
  499.  
  500. }
  501. main "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement