Guest User

working apt-cyg 9/3/13

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