]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - src/pakfire/pakfire
Fixed bug, pakfire isntalls dependencies twice -> needs testing.
[people/pmueller/ipfire-2.x.git] / src / pakfire / pakfire
index 008382ce7ed8230505ac355a880422338cb20f5f..e46b73410bd5287f265a2026a6136226878897d9 100644 (file)
@@ -1,10 +1,35 @@
 #!/usr/bin/perl
+###############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2007  Michael Tremer & Christian Schmidt                      #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+       ### Clean up our environment
+       #
+       delete @ENV{qw(IFS CDPATH ENV BASH_ENV PATH)};
+       $< = $>;
 
        require "/opt/pakfire/lib/functions.pl";
        
        my $interactive = 1;
+       my $force = "noforce";
        
-       &Pakfire::logger("### IPFire Pakfire $Conf::version started!");
+       &Pakfire::logger("PAKFIRE INFO: IPFire Pakfire $Conf::version started!");
        &Pakfire::checkcryptodb;
 
        ### Check if we are running as root
        my $user = qx(whoami);
        chomp($user);
        unless ( "$user" eq "root" ) {
-         &Pakfire::message("You must run pakfire as user root!");
+         &Pakfire::message("PAKFIRE ERROR: You must run pakfire as user root!");
          exit 1;
        }
        
+       unless ( -e "/var/ipfire/red/active" ) {
+               &Pakfire::message("PAKFIRE ERROR: You need to be online to run pakfire!");
+               exit 2;
+       }
+       
+       ### Check if we are started by another name
+       #
+       if ( $0 =~ /pakfire-update$/ ) {
+               &Pakfire::message("CRON INFO: Running an update");
+               my $random = int(rand(60));
+               &Pakfire::logger("CRON INFO: Waiting for $random seconds.");
+               sleep($random);
+               $ARGV[0] = "update";
+               $interactive = 0;
+       } elsif ( $0 =~ /pakfire-upgrade$/ ) {
+               &Pakfire::message("CRON INFO: Running an upgrade");
+               my $random = int(rand(3600));
+               &Pakfire::logger("CRON INFO: Waiting for $random seconds.");
+               sleep($random);
+               $ARGV[0] = "upgrade";
+               $interactive = 0;
+       }
+       
        unless (@ARGV) {
                &Pakfire::usage;
        }
        
        foreach (@ARGV) {
                if ("$_" =~ "^-") {
+                       # Turn off interactive mode
                        $interactive = 0 if ("$_" eq "--non-interactive");
                        $interactive = 0 if ("$_" eq "-y");
+                       
+                       # Turn off shell colors - Bad for displaying in webinterface
+                       $Pakfire::enable_colors = 0 if ("$_" eq "--no-colors");
+                       
+                       # Turn on force mode
+                       $force = "force" if ("$_" eq "-f" );
+                       $force = "force" if ("$_" eq "--force" );
                }
        }
 
        if ("$ARGV[0]" eq "install") {
-               &Pakfire::lock("on");
                shift;
                
                ### Make sure that the list is not outdated. 
                my @temp;
                my @templine;
                my $found = 0;
+               my $return;
                my @all;
                foreach $pak (@ARGV) {
                        unless ("$pak" =~ "^-") {
+                               $return = &Pakfire::isinstalled($pak);
+                               if ($return eq 0) {
+                                       &Pakfire::message("PAKFIRE INFO: $pak is already installed");
+                                       next; 
+                               }
                                $found = 0;
                                foreach (@db) {
                                        @templine = split(/;/,$_);
                                                break;
                                        }
                                }
-                               &Pakfire::message("\nThe pak \"$pak\" is not known. Please try running \"pakfire update\".") if ($found == 0);
+                               if ($found == 0) {
+                                       &Pakfire::message("");
+                                       &Pakfire::message("PAKFIRE WARN: The pak \"$pak\" is not known. Please try running \"pakfire update\".");
+                               }
                        }
                }
+               
+               unless (@paks) {
+                       &Pakfire::message("PAKFIRE ERROR: No packages to install. Exiting...");
+                       exit 1;
+               }
 
-               &Pakfire::message("\n\n### Packages to install:");
-               foreach (sort @paks) {
-                 my $size = &Pakfire::getsize("$_");
+               if (@deps) {
+                       my %sort = map{ $_, 1 } @deps;
+                       @deps = sort keys %sort;
+                       &Pakfire::message("");
+                       &Pakfire::message("PAKFIRE INFO: Packages to install for dependencies:");
+               }
+               foreach $dep (@deps) {
+                 my $size = &Pakfire::getsize("$dep");
                        $size = &Pakfire::beautifysize($size);
-                 &Pakfire::message("# $_  \t\t - $size");
+                 &Pakfire::message("PAKFIRE INFO: $dep \t - $size");
                }
-               
-               &Pakfire::message("\n### Packages to install for dependencies:") if @deps;
-               foreach (sort @deps) {
-                 my $size = &Pakfire::getsize("$_");
+
+               &Pakfire::message("");
+               &Pakfire::message("");
+               &Pakfire::message("PAKFIRE INFO: Packages to install:");
+               foreach $pak (@paks) {
+                 my $size = &Pakfire::getsize("$pak");
                        $size = &Pakfire::beautifysize($size);
-                 &Pakfire::message("# $_  \t\t - $size");
+                 &Pakfire::message("PAKFIRE INFO: $pak \t - $size");
                }
                
                my $totalsize;
-               foreach (@all) {
-                       $totalsize = ($totalsize + &Pakfire::getsize("$_"));
+               foreach $pak (@all) {
+                       $totalsize = ($totalsize + &Pakfire::getsize("$pak"));
                }
                $totalsize = &Pakfire::beautifysize($totalsize);
-               &Pakfire::message("\n### Total size: \t ~ $totalsize\n");
+               &Pakfire::message("");
+               &Pakfire::message("PAKFIRE INFO: Total size: \t ~ $totalsize");
+               &Pakfire::message("");
                
                if ($interactive) {
-                 &Pakfire::message("Is this okay? [y/N]");
+                 &Pakfire::message("PAKFIRE INFO: Is this okay? [y/N]");
                        my $ret = <STDIN>;
                        chomp($ret);
-                       &Pakfire::logger("Answer: $ret");
+                       &Pakfire::logger("PAKFIRE INFO: Answer: $ret");
                        if ( $ret ne "y" ) {
-                         &Pakfire::message("Installation aborted.");
+                         &Pakfire::message("PAKFIRE ERROR: Installation aborted.");
                          exit 1;
                        }
+               } else {
+                       &Pakfire::logger("PAKFIRE INFO: Interaction skipped.");
                }
+               
+#              my %sort = map{ $_, 1 } @all;
+#              @all = sort keys %sort;
 
                ### Download first
-               foreach (sort @all) {
-                       &Pakfire::message("# --> Downloading: $_");
-                       &Pakfire::getpak("$_", "");
+               foreach $pak (@all) {
+                       &Pakfire::getpak("$pak", "");
                }
 
-               foreach (sort @all) {
-                       &Pakfire::setuppak("$_");
+               &Pakfire::message("");
+
+               foreach $pak (@deps) {
+                       &Pakfire::setuppak("$pak") if ($pak ne "");
                }
-               &Pakfire::lock("off");
                
+
+               foreach $pak (@paks) {
+                       &Pakfire::setuppak("$pak") if ($pak ne "");
+               }
+               
+
        } elsif ("$ARGV[0]" eq "remove") {
+               shift;
+               
+               my @paks;
+               my $pak;
+               foreach $pak (@ARGV) {
+                       unless ("$pak" =~ "^-") {
+                               $return = &Pakfire::isinstalled($pak);
+                               if ($return ne 0) {
+                                       &Pakfire::message("PAKFIRE WARN: $pak is not installed");
+                                       next;
+                               }
+                               push(@paks, $pak);
+                       }
+               }
+               
+               unless (@paks) {
+                       &Pakfire::message("PAKFIRE ERROR: No packages to remove. Exiting...");
+                       exit 1;
+               }
+               
+               &Pakfire::message("");
+               &Pakfire::message("");
+               &Pakfire::message("PAKFIRE INFO: Packages to remove:");
+               foreach $pak (sort @paks) {
+                 my $size = &Pakfire::getsize("$pak");
+                       $size = &Pakfire::beautifysize($size);
+                 &Pakfire::message("PAKFIRE INFO: $pak \t - $size");
+               }
+               
+               if ($interactive) {
+                 &Pakfire::message("PAKFIRE INFO: Is this okay? [y/N]");
+                       my $ret = <STDIN>;
+                       chomp($ret);
+                       &Pakfire::logger("PAKFIRE INFO: Answer: $ret");
+                       if ( $ret ne "y" ) {
+                         &Pakfire::message("PAKFIRE ERROR: Installation aborted.");
+                         exit 1;
+                       }
+               }
+               
+               foreach $pak (@paks) {
+                       &Pakfire::removepak("$pak");
+               }
        
        
        } elsif ("$ARGV[0]" eq "update") {
-               my $force = "noforce";
                &Pakfire::makeuuid();
                &Pakfire::senduuid();
-               &Pakfire::getmirrors();
-               $force = "force" if ("$ARGV[1]" eq "--force");
+               &Pakfire::getmirrors("$force");
                &Pakfire::dbgetlist("$force");
-               
-       
+               &Pakfire::getcoredb("$force");
+
        } elsif ("$ARGV[0]" eq "upgrade") {
+               &Pakfire::upgradecore();
                my @upgradepaks = &Pakfire::dblist("upgrade", "noweb");
-               my @temp;
+               my @temp, $pak;
                
                foreach (@upgradepaks) {
                        @temp = &Pakfire::resolvedeps("$_");
                
                if (@upgradepaks) {
                        &Pakfire::message("");
-                       &Pakfire::message("We are going to install all packages listed above.");
+                       &Pakfire::message("PAKFIRE UPGR: We are going to install all packages listed above.");
                        if ($interactive) {
-                         &Pakfire::message("Is this okay? [y/N]");
+                         &Pakfire::message("PAKFIRE INFO: Is this okay? [y/N]");
                                my $ret = <STDIN>;
                                chomp($ret);
-                               &Pakfire::logger("Answer: $ret");
+                               &Pakfire::logger("PAKFIRE INFO: Answer: $ret");
                                if ( $ret ne "y" ) {
-                                 &Pakfire::message("Installation aborted.");
+                                 &Pakfire::message("PAKFIRE ERROR: Installation aborted.");
                                  exit 1;
                                }
                        }
                }
                
                ### Download first
-               foreach (sort @upgradepaks) {
-                       &Pakfire::message("# --> Downloading: $_");
-                       &Pakfire::getpak("$_", "");
+               foreach $pak (@upgradepaks) {
+                       system("mv $Conf::dbdir/meta/meta-$pak $Conf::dbdir/meta/old-meta-$pak");
+                       &Pakfire::getpak("$pak", "");
                }
                
-               foreach (sort @upgradepaks) {
-                       &Pakfire::upgradepak("$_");
+               foreach $pak (@upgradepaks) {
+                       if (&Pakfire::upgradepak("$pak")) {
+                               system("mv $Conf::dbdir/meta/old-meta-$pak $Conf::dbdir/meta/meta-$pak");
+                       }
                }
                
        } elsif ("$ARGV[0]" eq "list") {
-               &Pakfire::dblist("all", "noweb");
+               if ("$ARGV[1]" =~ /installed|notinstalled/) {
+                       &Pakfire::dblist("$ARGV[1]", "noweb");
+               } else {
+                       &Pakfire::message("PAKFIRE WARN: Not a known option $ARGV[1]") if ($ARGV[1]); 
+                       &Pakfire::dblist("all", "noweb");
+               }
                
        } elsif ("$ARGV[0]" eq "resolvedeps") {
                foreach (@ARGV) {
                        next if ("$_" eq "resolvedeps");
+                       next if ("$_" =~ "^-");
                        &Pakfire::resolvedeps("$_");
                }
-               
+       } elsif ("$ARGV[0]" eq "enable") {
+               if ("$ARGV[1]" eq "updates") {
+                       system("ln -s ../../opt/pakfire/pakfire /etc/fcron.daily/pakfire-update");
+               } elsif ("$ARGV[1]" eq "upgrades") {
+                       system("ln -s ../../opt/pakfire/pakfire /etc/fcron.daily/pakfire-upgrade");
+               }
+       } elsif ("$ARGV[0]" eq "disable") {
+               if ("$ARGV[1]" eq "updates") {
+                       system("rm -f /etc/fcron.daily/pakfire-update");
+               } elsif ("$ARGV[1]" eq "upgrades") {
+                       system("rm -f /etc/fcron.daily/pakfire-upgrade");
+               }
        } else {
                &Pakfire::usage;
        }
-       
-exit 0;
+
+       &Pakfire::logger("PAKFIRE INFO: Pakfire has finished. Closing.");
+
+       exit 0;