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