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