/perl/Locale/

type='text/javascript' src='/cgit.js'>
summaryrefslogtreecommitdiff
path: root/net-setup.pl
blob: a9fc3341e0d7047a819b580e1f5c650f78951409 (plain)
  1. #!/usr/bin/perl
  2. #
  3. ######################################################################
  4. # LedgerSMB Small Medium Business Accounting Software Installer
  5. # http://www.ledgersmb.org/
  6. #
  7. # Copyright (C) 2006
  8. # This work contains copyrighted information from a number of sources all used
  9. # with permission.
  10. #
  11. # This file contains source code included with or based on SQL-Ledger which
  12. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  13. # under the GNU General Public License version 2 or, at your option, any later
  14. # version. For a full list including contact information of contributors,
  15. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  16. #
  17. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  18. # Copyright (c) 2002, Dieter Simader
  19. #
  20. # Web: http://www.sql-ledger.org
  21. #
  22. #######################################################################
  23. # Next bunch of lines are to check to see if they have the cpan module installed.
  24. my $cpan = 0;
  25. eval { use CPAN; };
  26. if ( !$@ ) {
  27. $cpan = 1;
  28. }
  29. $| = 1;
  30. #not sure how safe this is. If the browser sends a blank GATEWAY_INTERFACE
  31. #will this script destroy part of the install?
  32. #This script should probably be made inaccessible via HTTP until this feature is working
  33. if ( ( $ENV{GATEWAY_INTERFACE} ) || ( $ENV{HTTP_HOST} ) ) {
  34. print
  35. "Content-type: text/html\n\nThis does not work yet! use $0 from the command line";
  36. exit;
  37. }
  38. # Make sure they have the required perl modules installed.
  39. # bin/mozilla/admin.pl needs Digest::MD5 for session handling
  40. # HTML:LinkExtor is used by the setup program.
  41. my @req_modules = (qw(DBI DBD::Pg Digest::MD5 HTML::LinkExtor));
  42. foreach my $module (@req_modules) {
  43. print "Checking for: $module ...\t";
  44. my @results = &check_module($module);
  45. print "$results[1]\n";
  46. next if ( $results[0] ); # Passed, no need to continue..
  47. if ( $cpan == 1 ) {
  48. # Can try to install the module..
  49. print
  50. "\n\nWould you like to try and install this package ($module) through CPAN? (Y/N) [Y]:";
  51. my $response = <STDIN>;
  52. if ( ( $response =~ /y/i ) or ( $response eq "\n" ) ) {
  53. my $inst_obj = CPAN::Shell->install($module);
  54. @results = &check_module($module);
  55. if ( !$results[0] ) {
  56. print "\n\nCould not install $module using CPAN.\n";
  57. die "Please try to install this module manually\n";
  58. }
  59. }
  60. else {
  61. die "Please install the $module perl module and retry the setup.\n";
  62. }
  63. }
  64. else {
  65. # Can't try to install the module..
  66. die "Please install the $module perl module and retry the setup.\n";
  67. }
  68. }
  69. use HTML::LinkExtor;
  70. my $lynx = `lynx -version`; # if LWP is not installed use lynx
  71. my $gzip = `gzip -V 2>&1`; # gz decompression utility
  72. my $tar = `tar --version 2>&1`; # tar archiver
  73. my $latex = `latex -version`;
  74. my $versionurl = 'http://prdownloads.sourceforge.net/ledger-smb';
  75. my %source = (
  76. 1 => {
  77. url => "http://voxel.dl.sourceforge.net/sourceforge/ledger-smb",
  78. site => "New York, U.S.A",
  79. locale => 'us'
  80. },
  81. 2 => {
  82. url => "http://easynews.dl.sourceforge.net/sourceforge/ledger-smb",
  83. site => "Arizona, U.S.A",
  84. locale => 'us'
  85. },
  86. 3 => {
  87. url => "http://ufpr.dl.sourceforge.net/sourceforge/ledger-smb",
  88. site => "Brazil",
  89. locale => 'br'
  90. },
  91. 4 => {
  92. url => "http://surfnet.dl.sourceforge.net/sourceforge/ledger-smb",
  93. site => "The Netherlands",
  94. locale => 'nl'
  95. },
  96. 5 => {
  97. url => "http://http://kent.dl.sourceforge.net/sourceforge/ledger-smb",
  98. site => "U.K",
  99. locale => 'uk'
  100. },
  101. 6 => {
  102. url => "http://ovh.dl.sourceforge.net/sourceforge/ledger-smb",
  103. site => "France",
  104. locale => 'fr'
  105. },
  106. 7 => {
  107. url => "http://mesh.dl.sourceforge.net/sourceforge/ledger-smb",
  108. site => "Germany",
  109. locale => 'de'
  110. },
  111. 8 => {
  112. url => "http://citkit.dl.sourceforge.net/sourceforge/ledger-smb",
  113. site => "Russia",
  114. locale => 'ru'
  115. },
  116. 9 => {
  117. url => "http://optusnet.dl.sourceforge.net/sourceforge/ledger-smb",
  118. site => "Sydney, Australia",
  119. locale => 'au'
  120. },
  121. 10 => {
  122. url => "http://nchc.dl.sourceforge.net/sourceforge/ledger-smb",
  123. site => "Taiwan",
  124. locale => 'tw'
  125. },
  126. 11 => {
  127. url => "http://jaist.dl.sourceforge.net/sourceforge/ledger-smb",
  128. site => "Japan",
  129. locale => 'jp'
  130. },
  131. 12 => {
  132. url => "http://heanet.dl.sourceforge.net/sourceforge/ledger-smb",
  133. site => "Ireland",
  134. locale => 'ie'
  135. }
  136. );
  137. my $userspath = "users"; # default for new installation
  138. eval { require "ledger-smb.conf"; };
  139. my $filename = shift;
  140. chomp $filename;
  141. my $newinstall = 1;
  142. # is LWP installed
  143. eval { require LWP::Simple; };
  144. $lwp = !($@);
  145. unless ( $lwp || $lynx || $filename ) {
  146. die "You must have either lynx or LWP installed or specify a filename.
  147. perl $0 <filename>\n";
  148. }
  149. if ($filename) {
  150. # extract version
  151. die "Not a Ledger-SMB archive\n" if ( $filename !~ /^ledger-smb/ );
  152. $version = $filename;
  153. $version =~ s/ledger-smb-(\d+\.\d+\.\d+).*$/$1/;
  154. }
  155. if ( -f "VERSION" ) {
  156. # get installed version from VERSION file
  157. open( FH, '<', "VERSION" );
  158. @a = <FH>;
  159. close(FH);
  160. $version = $a[0];
  161. chomp $version;
  162. $newinstall = !$version;
  163. if ( !-f "ledger-smb.conf" ) {
  164. $newinstall = 1;
  165. }
  166. }
  167. # Try to determine web user and group..
  168. $webowner = "nobody";
  169. $webgroup = "nogroup";
  170. # Check for apache2.conf
  171. if ( $httpd = `find /etc /usr/local/etc -type f -name 'apache2.conf'` ) {
  172. chomp $httpd;
  173. $webowner = `grep "^User " $httpd`;
  174. $webgroup = `grep "^Group " $httpd`;
  175. chomp $webowner;
  176. chomp $webgroup;
  177. ( undef, $webowner ) = split / /, $webowner;
  178. ( undef, $webgroup ) = split / /, $webgroup;
  179. }
  180. elsif ( $httpd = `find /etc /usr/local/etc -type f -name 'httpd.conf'` ) {
  181. # Else check for httpd.conf
  182. chomp $httpd;
  183. $webowner = `grep "^User " $httpd`;
  184. $webgroup = `grep "^Group " $httpd`;
  185. chomp $webowner;
  186. chomp $webgroup;
  187. ( undef, $webowner ) = split / /, $webowner;
  188. ( undef, $webgroup ) = split / /, $webgroup;
  189. }
  190. if ( $confd = `find /etc /usr/local/etc -type d -name 'apache*/conf.d'` ) {
  191. chomp $confd;
  192. }
  193. # If we are doing a new install.. check the postgresql installation..
  194. if ( $newinstall == 1 ) {
  195. # Check the postgresql version before we even check for a connection if local
  196. system("tput clear"); # Clear the screen..
  197. our ( $pghost, $pgport, $pguser, $pgpassword );
  198. print
  199. "\n\nIs PostgreSQL installed [L]ocally,\n or will you be connecting to a [R]emote server? (L/R) [L]:";
  200. my $localremote = <STDIN>;
  201. if ( ( $localremote =~ /L/i ) or ( $localremote eq "\n" ) ) {
  202. $pghost = 'localhost';
  203. # If local, check the local postgresql version..
  204. my $pgversion = `psql --version`;
  205. ($pgversionnum) = $pgversion =~ m/(\d\.\d\.\d)/;
  206. unless ( $pgversionnum gt '8.0.0' ) {
  207. # Die, cannot continue..
  208. print
  209. "LedgerSMB requires postgres version 8.0 or higher. You have version $pgversionnum installed\n";
  210. die;
  211. }
  212. }
  213. if ( !&check_pgconnect ) {
  214. print "\n\n\nInstallation was not successful\n Exiting....\n";
  215. exit;
  216. }
  217. }
  218. system("tput clear");
  219. if ($filename) {
  220. $install = "\ninstall $version from (f)ile\n";
  221. }
  222. # check for latest version
  223. &get_latest_version;
  224. chomp $latest_version;
  225. if ( !$newinstall ) {
  226. $install .= "\n(r)einstall $version\n";
  227. }
  228. if ( $version && $latest_version ) {
  229. if ( $version ne $latest_version ) {
  230. $install .= "\n(u)pgrade to $latest_version\n";
  231. }
  232. }
  233. $install .= "\n(i)nstall $latest_version (from Internet)\n" if $latest_version;
  234. $install .= "\n(d)ownload $latest_version (no installation)" unless $filename;
  235. print qq|
  236. LedgerSMB Accounting and ERP Installation
  237. $install
  238. Enter: |;
  239. $a = <STDIN>;
  240. chomp $a;
  241. exit unless $a;
  242. $a = lc $a;
  243. if ( $a !~ /d/ ) {
  244. print qq|\nEnter httpd owner [$webowner] : |;
  245. $web = <STDIN>;
  246. chomp $web;
  247. $webowner = $web if $web;
  248. print qq|\nEnter httpd group [$webgroup] : |;
  249. $web = <STDIN>;
  250. chomp $web;
  251. $webgroup = $web if $web;
  252. }
  253. if ( $a ne 'f' ) {
  254. system("tput clear");
  255. # choose site
  256. foreach $item ( sort { $a <=> $b } keys %source ) {
  257. $i++;
  258. print qq|$i. $source{$item}{site}\n|;
  259. }
  260. $site = "1";
  261. print qq|\nChoose Location [$site] : |;
  262. $b = <STDIN>;
  263. chomp $b;
  264. $site = $b if $b;
  265. }
  266. if ( $a eq 'd' ) {
  267. &download;
  268. }
  269. if ( $a =~ /(i|u)/ ) {
  270. &install_smb;
  271. }
  272. if ( $a eq 'r' ) {
  273. $latest_version = $version;
  274. &install_smb;
  275. }
  276. if ( $a eq 'f' ) {
  277. &install_smb;
  278. }
  279. exit;
  280. # end main
  281. sub check_module {
  282. my ($module) = @_;
  283. eval "use $module";
  284. if ( !$@ ) {
  285. return 1, "Ok";
  286. }
  287. else {
  288. return 0, "FAILED", $@;
  289. }
  290. }
  291. sub download {
  292. &get_source_code;
  293. }
  294. sub get_latest_version {
  295. print "Checking for latest version number .... ";
  296. if ($filename) {
  297. print "skipping, filename supplied\n";
  298. return;
  299. }
  300. my $urlresult = '';
  301. if ($lwp) {
  302. if ( $urlresult = LWP::Simple::get("$versionurl") ) {
  303. $latest_version = parse_links($urlresult);
  304. }
  305. else {
  306. print "not found";
  307. }
  308. }
  309. else {
  310. if ( !$lynx ) {
  311. print "\nYou must have either lynx or LWP installed";
  312. exit 1;
  313. }
  314. $ok = `lynx -dump -head $versionurl`;
  315. if ( $ok = ( $ok =~ s/HTTP.*?200 // ) ) {
  316. $urlresult = `lynx -dump $versionurl`;
  317. $latest_version = parse_links($urlresult);
  318. }
  319. else {
  320. print "not found";
  321. }
  322. die unless $ok;
  323. }
  324. if ($latest_version) {
  325. print "ok\n";
  326. 1;
  327. }
  328. }
  329. my @versions = ();
  330. sub parse_links {
  331. # Take the html retrieved by lwp or lynx and look for the version numbers.
  332. my $text = shift;
  333. my $version = '';
  334. my $p = HTML::LinkExtor->new( \&cb );
  335. $p->parse($text) or die;
  336. foreach (@versions) {
  337. my ($chkversion) =
  338. $_ =~
  339. /^\/ledger-smb\/ledger-smb-(\d{1,3}\.\d{1,3}\.\d{1,3}\w*)\.tar\.gz$/;
  340. $version = $chkversion if ( $chkversion gt $version );
  341. }
  342. return $version;
  343. }
  344. sub cb {
  345. # Callback function for LinkExtor
  346. my ( $tag, %attr ) = @_;
  347. return if $tag ne 'a';
  348. return
  349. unless $attr{href} =~
  350. /^\/ledger-smb\/ledger-smb-\d{1,3}\.\d{1,3}\.\d{1,3}\w*\.tar\.gz$/;
  351. push( @versions, values %attr );
  352. }
  353. sub get_source_code {
  354. $err = 0;
  355. @order = ();
  356. push @order, $site;
  357. for ( sort { $a <=> $b } keys %source ) {
  358. push @order, $_;
  359. }
  360. if ($latest_version) {
  361. # download it
  362. chomp $latest_version;
  363. $latest_version = "ledger-smb-${latest_version}.tar.gz";
  364. print "\nStatus\n";
  365. print "Downloading $latest_version .... ";
  366. foreach $key (@order) {
  367. print "\n$source{$key}{site} .... ";
  368. if ($lwp) {
  369. $err =
  370. LWP::Simple::getstore( "$source{$key}{url}/$latest_version",
  371. "$latest_version" );
  372. $err -= 200;
  373. }
  374. else {
  375. $ok = `lynx -dump -head $source{$key}{url}/$latest_version`;
  376. $err = !( $ok =~ s/HTTP.*?200 // );
  377. if ( !$err ) {
  378. $err =
  379. system(
  380. "lynx -dump $source{$key}{url}/$latest_version > $latest_version"
  381. );
  382. }
  383. }
  384. if ($err) {
  385. print "failed!";
  386. }
  387. else {
  388. last;
  389. }
  390. }
  391. }
  392. else {
  393. $err = -1;
  394. }
  395. if ($err) {
  396. die "Cannot get $latest_version";
  397. }
  398. else {
  399. print "ok!\n";
  400. }
  401. $latest_version;
  402. }
  403. sub install_smb {
  404. if ($filename) {
  405. $latest_version = $filename;
  406. }
  407. else {
  408. $latest_version = &get_source_code;
  409. }
  410. &decompress;
  411. if ($newinstall) {
  412. open( FH, '<', "ledger-smb.conf.default" );
  413. @f = <FH>;
  414. close(FH);
  415. unless ($latex) {
  416. grep { s/^\$latex.*/\$latex = 0;/ } @f;
  417. }
  418. open( FH, '>', "ledger-smb.conf" );
  419. print FH @f;
  420. close(FH);
  421. $alias = $absolutealias = $ENV{'PWD'};
  422. $alias =~ s/.*\///g;
  423. $httpddir = `dirname $httpd`;