summaryrefslogtreecommitdiff
path: root/setup.pl
blob: c1b11e48c7e7c9b77755e6958fe7e788d3296712 (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 {
  26. use CPAN;
  27. };
  28. if (!$@){
  29. $cpan = 1;
  30. }
  31. $| = 1;
  32. #not sure how safe this is. If the browser sends a blank HTTP_USER_AGENT
  33. #will this script destroy part of the install?
  34. #This script should probably be made inaccessible via HTTP until this feature is working
  35. if (($ENV{HTTP_USER_AGENT})||($ENV{HTTP_HOST})) {
  36. print "Content-type: text/html\n\nThis does not work yet! use $0 from the command line";
  37. exit;
  38. }
  39. # Make sure they have the required perl modules installed.
  40. # bin/mozilla/admin.pl needs Digest::MD5 for session handling
  41. # HTML:LinkExtor is used by the setup program.
  42. my @req_modules=(qw(DBI DBD::Pg Digest::MD5 HTML::LinkExtor));
  43. foreach my $module(@req_modules){
  44. print "Checking for: $module ...\t";
  45. my @results=&check_module($module);
  46. print "$results[1]\n";
  47. next if($results[0]); # Passed, no need to continue..
  48. if ($cpan == 1){
  49. # Can try to install the module..
  50. print "\n\nWould you like to try and install this package ($module) through CPAN? (Y/N) [Y]:";
  51. $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. } else {
  60. die "Please install the $module perl module and retry the setup.\n";
  61. }
  62. } else {
  63. # Can't try to install the module..
  64. die "Please install the $module perl module and retry the setup.\n";
  65. }
  66. }
  67. use HTML::LinkExtor;
  68. $lynx = `lynx -version`; # if LWP is not installed use lynx
  69. $gzip = `gzip -V 2>&1`; # gz decompression utility
  70. $tar = `tar --version 2>&1`; # tar archiver
  71. $latex = `latex -version`;
  72. my $versionurl ='http://prdownloads.sourceforge.net/ledger-smb';
  73. %source = (
  74. 1 => { url => "http://voxel.dl.sourceforge.net/sourceforge/ledger-smb", site => "New York, U.S.A", locale => 'us' },
  75. 2 => { url => "http://easynews.dl.sourceforge.net/sourceforge/ledger-smb", site => "Arizona, U.S.A", locale => 'us' },
  76. 3 => { url => "http://ufpr.dl.sourceforge.net/sourceforge/ledger-smb", site =>"Brazil", locale => 'br' },
  77. 4 => { url => "http://surfnet.dl.sourceforge.net/sourceforge/ledger-smb", site => "The Netherlands", locale => 'nl' },
  78. 5 => { url => "http://http://kent.dl.sourceforge.net/sourceforge/ledger-smb", site => "U.K", locale => 'uk' },
  79. 6 => { url => "http://ovh.dl.sourceforge.net/sourceforge/ledger-smb", site => "France", locale => 'fr' },
  80. 7 => { url => "http://mesh.dl.sourceforge.net/sourceforge/ledger-smb", site => "Germany", locale => 'de' },
  81. 8 => { url => "http://citkit.dl.sourceforge.net/sourceforge/ledger-smb", site => "Russia", locale => 'ru' },
  82. 9 => { url => "http://optusnet.dl.sourceforge.net/sourceforge/ledger-smb", site => "Sydney, Australia", locale => 'au' },
  83. 10 => { url => "http://nchc.dl.sourceforge.net/sourceforge/ledger-smb", site => "Taiwan", locale => 'tw' },
  84. 11 => { url => "http://jaist.dl.sourceforge.net/sourceforge/ledger-smb", site => "Japan", locale => 'jp' },
  85. 12 => { url => "http://heanet.dl.sourceforge.net/sourceforge/ledger-smb", site => "Ireland", locale => 'ie' }
  86. );
  87. $userspath = "users"; # default for new installation
  88. eval { require "ledger-smb.conf"; };
  89. $filename = shift;
  90. chomp $filename;
  91. $newinstall = 1;
  92. # is LWP installed
  93. eval { require LWP::Simple; };
  94. $lwp = !($@);
  95. unless ($lwp || $lynx || $filename) {
  96. die "You must have either lynx or LWP installed or specify a filename.
  97. perl $0 <filename>\n";
  98. }
  99. if ($filename) {
  100. # extract version
  101. die "Not a Ledger-SMB archive\n" if ($filename !~ /^ledger-smb/);
  102. $version = $filename;
  103. $version =~ s/ledger-smb-(\d+\.\d+\.\d+).*$/$1/;
  104. }
  105. if (-f "VERSION") {
  106. # get installed version from VERSION file
  107. open(FH, "VERSION");
  108. @a = <FH>;
  109. close(FH);
  110. $version = $a[0];
  111. chomp $version;
  112. $newinstall = !$version;
  113. if (! -f "ledger-smb.conf") {
  114. $newinstall = 1;
  115. }
  116. }
  117. $webowner = "nobody";
  118. $webgroup = "nogroup";
  119. if ($httpd = `find /etc /usr/local/etc -type f -name 'httpd.conf'`) {
  120. chomp $httpd;
  121. $webowner = `grep "^User " $httpd`;
  122. $webgroup = `grep "^Group " $httpd`;
  123. chomp $webowner;
  124. chomp $webgroup;
  125. ($null, $webowner) = split / /, $webowner;
  126. ($null, $webgroup) = split / /, $webgroup;
  127. }
  128. if ($confd = `find /etc /usr/local/etc -type d -name 'apache*/conf.d'`) {
  129. chomp $confd;
  130. }
  131. system("tput clear");
  132. if ($filename) {
  133. $install = "\ninstall $version from (f)ile\n";
  134. }
  135. # check for latest version
  136. &get_latest_version;
  137. chomp $latest_version;
  138. if (!$newinstall) {
  139. $install .= "\n(r)einstall $version\n";
  140. }
  141. if ($version && $latest_version) {
  142. if ($version ne $latest_version) {
  143. $install .= "\n(u)pgrade to $latest_version\n";
  144. }
  145. }
  146. $install .= "\n(i)nstall $latest_version (from Internet)\n" if $latest_version;
  147. $install .= "\n(d)ownload $latest_version (no installation)" unless $filename;
  148. print qq|
  149. LedgerSMB Accounting and ERP Installation
  150. $install
  151. Enter: |;
  152. $a = <STDIN>;
  153. chomp $a;
  154. exit unless $a;
  155. $a = lc $a;
  156. if ($a !~ /d/) {
  157. print qq|\nEnter httpd owner [$webowner] : |;
  158. $web = <STDIN>;
  159. chomp $web;
  160. $webowner = $web if $web;
  161. print qq|\nEnter httpd group [$webgroup] : |;
  162. $web = <STDIN>;
  163. chomp $web;
  164. $webgroup = $web if $web;
  165. }
  166. if ($a ne 'f') {
  167. system("tput clear");
  168. # choose site
  169. foreach $item (sort { $a <=> $b } keys %source) {
  170. $i++;
  171. print qq|$i. $source{$item}{site}\n|;
  172. }
  173. $site = "1";
  174. print qq|\nChoose Location [$site] : |;
  175. $b = <STDIN>;
  176. chomp $b;
  177. $site = $b if $b;
  178. }
  179. if ($a eq 'd') {
  180. &download;
  181. }
  182. if ($a =~ /(i|u)/) {
  183. &install_smb;
  184. }
  185. if ($a eq 'r') {
  186. $latest_version = $version;
  187. &install_smb;
  188. }
  189. if ($a eq 'f') {
  190. &install_smb;
  191. }
  192. exit;
  193. # end main
  194. sub check_module{
  195. my($module)=@_;
  196. eval "use $module";
  197. if(!$@){
  198. return 1, "Ok";
  199. }else{
  200. return 0, "FAILED",$@;
  201. }
  202. }
  203. sub download {
  204. &get_source_code;
  205. }
  206. sub get_latest_version {
  207. print "Checking for latest version number .... ";
  208. if ($filename) {
  209. print "skipping, filename supplied\n";
  210. return;
  211. }
  212. my $urlresult = '';
  213. if ($lwp) {
  214. if ($urlresult = LWP::Simple::get("$versionurl")){
  215. $latest_version = parse_links($urlresult);
  216. } else {
  217. print "not found";
  218. }
  219. } else {
  220. if (!$lynx) {
  221. print "\nYou must have either lynx or LWP installed";
  222. exit 1;
  223. }
  224. $ok = `lynx -dump -head $versionurl`;
  225. if ($ok = ($ok =~ s/HTTP.*?200 //)) {
  226. $urlresult = `lynx -dump $versionurl`;
  227. $latest_version = parse_links($urlresult);
  228. } else {
  229. print "not found";
  230. }
  231. die unless $ok;
  232. }
  233. if ($latest_version) {
  234. print "ok\n";
  235. 1;
  236. }
  237. }
  238. my @versions = ();
  239. sub parse_links{
  240. # Take the html retrieved by lwp or lynx and look for the version numbers.
  241. my $text = shift;
  242. my $version = '';
  243. my $p = HTML::LinkExtor->new(\&cb);
  244. $p->parse($text) or die;
  245. foreach (@versions){
  246. my ($chkversion) = $_ =~ /^\/ledger-smb\/ledger-smb-(\d{1,3}\.\d{1,3}\.\d{1,3}\w*)\.tar\.gz$/;
  247. $version = $chkversion if ($chkversion gt $version);
  248. }
  249. return $version;
  250. }
  251. sub cb {
  252. # Callback function for LinkExtor
  253. my($tag, %attr) = @_;
  254. return if $tag ne 'a';
  255. return unless $attr{href} =~ /^\/ledger-smb\/ledger-smb-\d{1,3}\.\d{1,3}\.\d{1,3}\w*\.tar\.gz$/;
  256. push(@versions, values %attr);
  257. }
  258. sub get_source_code {
  259. $err = 0;
  260. @order = ();
  261. push @order, $site;
  262. for (sort { $a <=> $b } keys %source) {
  263. push @order, $_;
  264. }
  265. if ($latest_version) {
  266. # download it
  267. chomp $latest_version;
  268. $latest_version = "ledger-smb-${latest_version}.tar.gz";
  269. print "\nStatus\n";
  270. print "Downloading $latest_version .... ";
  271. foreach $key (@order) {
  272. print "\n$source{$key}{site} .... ";
  273. if ($lwp) {
  274. $err = LWP::Simple::getstore("$source{$key}{url}/$latest_version", "$latest_version");
  275. $err -= 200;
  276. } else {
  277. $ok = `lynx -dump -head $source{$key}{url}/$latest_version`;
  278. $err = !($ok =~ s/HTTP.*?200 //);
  279. if (!$err) {
  280. $err = system("lynx -dump $source{$key}{url}/$latest_version > $latest_version");
  281. }
  282. }
  283. if ($err) {
  284. print "failed!";
  285. } else {
  286. last;
  287. }
  288. }
  289. } else {
  290. $err = -1;
  291. }
  292. if ($err) {
  293. die "Cannot get $latest_version";
  294. } else {
  295. print "ok!\n";
  296. }
  297. $latest_version;
  298. }
  299. sub install_smb {
  300. if ($filename) {
  301. $latest_version = $filename;
  302. } else {
  303. $latest_version = &get_source_code;
  304. }
  305. &decompress;
  306. if ($newinstall) {
  307. open(FH, "ledger-smb.conf.default");
  308. @f = <FH>;
  309. close(FH);
  310. unless ($latex) {
  311. grep { s/^\$latex.*/\$latex = 0;/ } @f;
  312. }
  313. open(FH, ">ledger-smb.conf");
  314. print FH @f;
  315. close(FH);
  316. $alias = $absolutealias = $ENV{'PWD'};
  317. $alias =~ s/.*\///g;
  318. $httpddir = `dirname $httpd`;
  319. if ($confd) {
  320. $httpddir = $confd;
  321. }
  322. chomp $httpddir;
  323. $filename = "ledger-smb-httpd.conf";
  324. # do we have write permission?
  325. if (!open(FH, ">>$httpddir/$filename")) {
  326. open(FH, ">$filename");
  327. $norw = 1;
  328. }
  329. $directives = qq|
  330. Alias /$alias $absolutealias/
  331. <Directory $absolutealias>
  332. AllowOverride All
  333. AddHandler cgi-script .pl
  334. Options ExecCGI Includes FollowSymlinks
  335. Order Allow,Deny
  336. Allow from All
  337. </Directory>
  338. <Directory $absolutealias/users>
  339. Order Deny,Allow
  340. Deny from All
  341. </Directory>
  342. |;
  343. print FH $directives;
  344. close(FH);
  345. print qq|
  346. This is a new installation.
  347. |;
  348. if ($norw) {
  349. print qq|
  350. Webserver directives were written to $filename
  351. Copy $filename to $httpddir
  352. |;
  353. if (!$confd) {
  354. print qq| and add
  355. # Ledger-SMB
  356. Include $httpddir/$filename
  357. to $httpd
  358. |;
  359. }
  360. print qq| and restart your webserver!\n|;
  361. if (!$permset) {
  362. print qq|
  363. WARNING: permissions for templates, users, css and spool directory
  364. could not be set. Login as root and set permissions
  365. # chown -hR :$webgroup users templates css spool
  366. # chmod 771 users templates css spool
  367. |;
  368. }
  369. } else {
  370. print qq|
  371. Webserver directives were written to
  372. $httpddir/$filename
  373. |;
  374. if (!$confd) {
  375. if (!(`grep "^# LedgerSMB" $httpd`)) {
  376. open(FH, ">>$httpd");
  377. print FH qq|
  378. # LedgerSMB
  379. Include $httpddir/$filename
  380. |;
  381. close(FH);
  382. }
  383. }
  384. if (!$>) {
  385. # send SIGHUP to httpd
  386. if ($f = `find /var -type f -name 'httpd.pid'`) {
  387. $pid = `cat $f`;
  388. chomp $pid;
  389. if ($pid) {
  390. system("kill -s HUP $pid");
  391. }
  392. }
  393. }
  394. }
  395. }
  396. # if this is not root, check if user is part of $webgroup
  397. if ($>) {
  398. if ($permset = ($) =~ getgrnam $webgroup)) {
  399. `chown -hR :$webgroup users templates css spool`;
  400. chmod 0771, 'users', 'templates', 'css', 'spool';
  401. `chown :$webgroup ledger-smb.conf`;
  402. }
  403. } else {
  404. # root
  405. `chown -hR 0:0 *`;
  406. `chown -hR $webowner:$webgroup users templates css spool`;
  407. chmod 0771, 'users', 'templates', 'css', 'spool';
  408. `chown $webowner:$webgroup ledger-smb.conf`;
  409. }
  410. chmod 0644, 'ledger-smb.conf';
  411. unlink "ledger-smb.conf.default";
  412. &cleanup;
  413. while ($a !~ /(Y|N)/) {
  414. print qq|\nDisplay README (Y/n) : |;
  415. $a = <STDIN>;
  416. chomp $a;
  417. $a = ($a) ? uc $a : 'Y';
  418. if ($a eq 'Y') {
  419. @args = ("more", "doc/README");
  420. system(@args);
  421. }
  422. }
  423. }
  424. sub decompress {
  425. die "Error: gzip not installed\n" unless ($gzip);
  426. die "Error: tar not installed\n" unless ($tar);
  427. &create_lockfile;
  428. # ungzip and extract source code
  429. print "Decompressing $latest_version ... ";
  430. if (system("gzip -df $latest_version")) {
  431. print "Error: Could not decompress $latest_version\n";
  432. &remove_lockfile;
  433. exit;
  434. } else {
  435. print "done\n";
  436. }
  437. # strip gz from latest_version
  438. $latest_version =~ s/\.gz//;
  439. # now untar it
  440. print "Unpacking $latest_version ... ";
  441. if (system("tar -xf $latest_version")) {
  442. print "Error: Could not unpack $latest_version\n";
  443. &remove_lockfile;
  444. exit;
  445. } else {
  446. # now we have a copy in ledger-smb
  447. if (system("tar -cf $latest_version -C ledger-smb .")) {
  448. print "Error: Could not create archive for $latest_version\n";
  449. &remove_lockfile;
  450. exit;
  451. } else {
  452. if (system("tar -xf $latest_version")) {
  453. print "Error: Could not unpack $latest_version\n";
  454. &remove_lockfile;
  455. exit;
  456. } else {
  457. print "done\n";
  458. print "cleaning up ... ";
  459. `rm -rf ledger-smb`;
  460. print "done\n";
  461. }
  462. }
  463. }
  464. }
  465. sub create_lockfile {
  466. if (-d "$userspath") {
  467. open(FH, ">$userspath/nologin");
  468. close(FH);
  469. }
  470. }
  471. sub cleanup {
  472. unlink "$latest_version";
  473. unlink "$userspath/members.default" if (-f "$userspath/members.default");
  474. &remove_lockfile;
  475. }
  476. sub remove_lockfile { unlink "$userspath/nologin" if (-f "$userspath/nologin") };