#include <iostream>
#include <unistd.h>
#include <string.h>
#include <apt-pkg/cachefile.h>
#include <apt-pkg/pkgcache.h>
#include <apt-pkg/packagemanager.h>
#include <apt-pkg/dpkgpm.h>
#include <apt-pkg/error.h>
int main()
{
if(!pkgInitConfig(*_config))
{
std::cout << "Can't init config!" << std::endl;
}
if(!pkgInitSystem(*_config, _system))
{
std::cout << "Can't init system!" << std::endl;
}
pkgCacheFile cache_file;
pkgCache *cache = cache_file.GetPkgCache();
if(!cache)
{
std::cout << "Can't retrieve cache!" << std::endl;
}
pkgDepCache *dep_cache = cache_file.GetDepCache();
if(!dep_cache)
{
std::cout << "Can't retrieve depcache!" << std::endl;
}
pkgDPkgPM *pm = new pkgDPkgPM(dep_cache);
if(!pm)
{
std::cout << "Can't create pm!" << std::endl;
}
std::cout << "package manager created!" << std::endl;
GlobalError *err_obj = _GetErrorObj();
for(pkgCache::PkgIterator package = cache->PkgBegin(); package && !package.end(); package++)
{
// if(package.Name()=="pac-about")
// std::cout << package.Name() << std::endl;
// std::cout << "And inside! ..." << std::endl;
if( ((*package).CurrentState == pkgCache::State::NotInstalled) &&
( strcmp(package.Name(),"pac-about")==0) )
{
std::cout << package.Name() << std::endl;
if(dep_cache->MarkInstall(package))
{
std::cout << "Marked" << std::endl;
}
break;
}
}
if (cache_file.BuildSourceList() == false)
std::cout << "ERRR" << std::endl; //return false;
pkgSourceList * const List = cache_file.GetSourceList();
pkgAcquire Fetcher;
//Create the text record parser
pkgRecords Recs(cache_file);
if (_error->PendingError() == true)
std::cout << "ERRR" << std::endl; //return false;
if (pm->GetArchives(&Fetcher,List,&Recs) == false || _error->PendingError() == true)
std::cout << "ERRR" << std::endl; //return false;
APT::Progress::PackageManager *progress = APT::Progress::PackageManagerProgressFactory();
pkgPackageManager::OrderResult rs = pm->DoInstall(progress);
err_obj->DumpErrors(std::cout, GlobalError::MsgType::DEBUG);
if(rs == pkgPackageManager::Completed)
std::cout << "Completed" << std::endl;
else if (rs == pkgPackageManager::Failed)
std::cout << "Failed!" << std::endl;
else
std::cout << "InComplete!" << std::endl;
dep_cache->writeStateFile(NULL);
return 0;
}