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