- i18n fixes from Ralf:
[apt.git] / apt-pkg / init.cc
1 // -*- mode: c++; mode: fold -*-
2 // Description                                                          /*{{{*/
3 // $Id: init.cc,v 1.20 2003/02/09 20:31:05 doogie Exp $
4 /* ######################################################################
5
6    Init - Initialize the package library
7    
8    ##################################################################### */
9                                                                         /*}}}*/
10 // Include files                                                        /*{{{*/
11 #include <apt-pkg/init.h>
12 #include <apt-pkg/fileutl.h>
13 #include <apt-pkg/error.h>
14
15 #include <apti18n.h>
16 #include <config.h>
17 #include <sys/stat.h>
18
19 // CNC:2003-03-17
20 #include <apt-pkg/luaiface.h>
21
22                                                                         /*}}}*/
23
24 #define Stringfy_(x) # x
25 #define Stringfy(x)  Stringfy_(x)
26 const char *pkgVersion = VERSION;
27 const char *pkgLibVersion = Stringfy(APT_PKG_MAJOR) "."
28                             Stringfy(APT_PKG_MINOR) "." 
29                             Stringfy(APT_PKG_RELEASE);
30 const char *pkgCPU = COMMON_CPU;
31 const char *pkgOS = COMMON_OS;
32     
33 // pkgInitConfig - Initialize the configuration class                   /*{{{*/
34 // ---------------------------------------------------------------------
35 /* Directories are specified in such a way that the FindDir function will
36    understand them. That is, if they don't start with a / then their parent
37    is prepended, this allows a fair degree of flexability. */
38 bool pkgInitConfig(Configuration &Cnf)
39 {
40    // General APT things
41    if (strcmp(COMMON_OS,"linux") == 0 ||
42        strcmp(COMMON_OS,"unknown") == 0)
43       Cnf.Set("APT::Architecture",COMMON_CPU);
44    else
45       Cnf.Set("APT::Architecture",COMMON_OS "-" COMMON_CPU);
46    // CNC:2002-09-10
47    //Cnf.Set("APT::Build-Essential::", "build-essential");
48    Cnf.Set("Dir","/");
49    
50    // State   
51    Cnf.Set("Dir::State","var/lib/apt/");
52    
53    /* Just in case something goes horribly wrong, we can fall back to the
54       old /var/state paths.. */
55    struct stat St;   
56    if (stat("/var/lib/apt/.",&St) != 0 &&
57        stat("/var/state/apt/.",&St) == 0)
58       Cnf.Set("Dir::State","var/state/apt/");
59        
60    Cnf.Set("Dir::State::lists","lists/");
61    Cnf.Set("Dir::State::cdroms","cdroms.list");
62    
63    // Cache
64    Cnf.Set("Dir::Cache","var/cache/apt/");
65    Cnf.Set("Dir::Cache::archives","archives/");
66    Cnf.Set("Dir::Cache::srcpkgcache","srcpkgcache.bin");
67    Cnf.Set("Dir::Cache::pkgcache","pkgcache.bin");
68    
69    // Configuration
70    Cnf.Set("Dir::Etc","etc/apt/");
71    Cnf.Set("Dir::Etc::sourcelist","sources.list");
72    // CNC:2003-03-03
73    Cnf.Set("Dir::Etc::sourceparts","sources.list.d");
74    Cnf.Set("Dir::Etc::vendorlist","vendors.list");
75    Cnf.Set("Dir::Etc::vendorparts","vendors.list.d");
76    Cnf.Set("Dir::Etc::main","apt.conf");
77    Cnf.Set("Dir::Etc::parts","apt.conf.d");
78    Cnf.Set("Dir::Etc::preferences","preferences");
79    Cnf.Set("Dir::Bin::methods",LIBDIR "/apt/methods");
80               
81    bool Res = true;
82    
83    // Read an alternate config file
84    const char *Cfg = getenv("APT_CONFIG");
85    if (Cfg != 0 && FileExists(Cfg) == true)
86       Res &= ReadConfigFile(Cnf,Cfg);
87    
88    // Read the configuration parts dir
89    string Parts = Cnf.FindDir("Dir::Etc::parts");
90    if (FileExists(Parts) == true)
91       Res &= ReadConfigDir(Cnf,Parts);
92       
93    // Read the main config file
94    string FName = Cnf.FindFile("Dir::Etc::main");
95    if (FileExists(FName) == true)
96       Res &= ReadConfigFile(Cnf,FName);
97    
98    if (Res == false)
99       return false;
100    
101    if (Cnf.FindB("Debug::pkgInitConfig",false) == true)
102       Cnf.Dump();
103    
104    bindtextdomain(APT_DOMAIN,LOCALEDIR);
105    
106    return true;
107 }
108                                                                         /*}}}*/
109
110 // CNC:2003-02-16 - We must do that to force a statically linked libapt-pkg
111 //                  library to include the package systems into the binary.
112 #include <apt-pkg/rpmsystem.h>
113 //#include <apt-pkg/debsystem.h>
114 void ForceLinkage()
115 {
116         rpmSystem *rpmsys = &rpmSys;
117         rpmsys->ArchiveSupported("");
118         //debSystem *debsys = &debSys;
119         //depsys->ArchiveSupported("");
120 }
121
122 // pkgInitSystem - Initialize the _system calss                         /*{{{*/
123 // ---------------------------------------------------------------------
124 /* */
125 bool pkgInitSystem(Configuration &Cnf,pkgSystem *&Sys)
126 {
127    ForceLinkage(); // CNC:2003-02-16 - See above.
128
129    Sys = 0;
130    string Label = Cnf.Find("Apt::System","");
131    if (Label.empty() == false)
132    {
133       Sys = pkgSystem::GetSystem(Label.c_str());
134       if (Sys == 0)
135          return _error->Error(_("Packaging system '%s' is not supported"),Label.c_str());
136    }
137    else
138    {
139       signed MaxScore = 0;
140       for (unsigned I = 0; I != pkgSystem::GlobalListLen; I++)
141       {
142          signed Score = pkgSystem::GlobalList[I]->Score(Cnf);
143          if (Score > MaxScore)
144          {
145             MaxScore = Score;
146             Sys = pkgSystem::GlobalList[I];
147          }
148       }
149       
150       if (Sys == 0)
151          return _error->Error(_("Unable to determine a suitable system type"));
152    }
153
154    // CNC:2003-03-15
155    bool Ret = Sys->Initialize(Cnf);
156    // LORG:2006-02-06
157    Cnf.CndSet("APT::DistroVersion", Sys->DistroVer());
158
159 #ifdef WITH_LUA
160    _lua->RunScripts("Scripts::Init");
161 #endif
162    return Ret;
163 }
164                                                                         /*}}}*/