aboutsummaryrefslogblamecommitdiffstats
path: root/lib/sasl/test/systools_SUITE.erl
blob: 892c4994e88a11cfa229f9c97135ee7d403abe76 (plain) (tree)
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511










































                                                                         
                                                                 





                                                                          
                                                


                                                                             

                                                                     













                                                                      
                                                                    





                                                         

                                                                     




                                                               
                                       
                

                                                                          
         
                                                                         














































































































































































































































































































                                                                               

























                                                                          







                                                                           
                                           



















                                                                                  
















                                                                              





                                                                         


                                              

































































































































































































































































































































































































































































































































































































































































                                                                                  




















                                                                       


                                                    




                                                                           


                                                    











                                                               




















                                                                    

                                                   


                                       








                                                                            














                                                                     



                                                                            
                                                                        


                                                                      


                                             




                                    




















































































                                                                              















                                                                      








                                                                     

                                                                    

                                      

       

                                                          
                                      

       
                                        

                                                                    
                                      

       









                                                                    












                                                                               



                                        



                                                                  






                                                                        




                                                                  
                 
                                                                         

                                                            
                                                  

                                          

                                                                       
                                                    

                                                    

                 
                                                                          

                                                           
                                                  
















                                                                          
                                                                   
                                                    

                                                    




























                                                                         

                                                   














                                                                               






























                                                                                












                                                                                                                                       


                                                                              


                                              
                                                     





                                                                                               
                                                                                                      




                                                                                                               
                                                                                             



                                                                                        
                                                                                               





                                                                                         










                                                                          

                                                   











                                                                     
                                                                















                                                                                




                                                                     

                                                            

















                                                                         
                                                                             

                                                                                
                                                                              





































































                                                                             
                                                                    



                                                          

                                                               









                                                     

                                                       





                                                                         
                                                             





                                                               
                                                                    



                                                        
                                                       











                                                                        

                                                       




                                                                         
                                                             






                                                               
















                                                                    

                                                   










































































































































































































































































                                                                                   

                                                         
                                          

                                                          
                                

                                                       
                                             

                                                               
                                

                                                       
                                        

                                                                 
                                

                                                       
                                     

                                                     
                                                                  

                                                      
                                      

                                                      
                                      

                                                      
                                        

                                                        
                                               

                                                                 
                                               






                                                                 
                                            

                                                                     
                                    

                                                      
                                                

                                                                  
                                                




                                                                                  
                             
                                    

                                                  


                                                                     

                                                 

                                                      















                                                                   
 





















































































































































































































































































































































































                                                                      
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2011. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%
%% Test suite for the systools module.
%%
%%	The systools module is a wrapper for a number of modules that
%%	handle large parts of the release building functionality
%%	(e.g. checking app files, building a tar file, building
%%	release upgrad scripts.
%%


-module(systools_SUITE).

%-define(debug, true).

-include_lib("test_server/include/test_server.hrl").
-define(format(S, A), ok).
-define(datadir, ?config(data_dir, Config)).
-define(privdir, ?config(priv_dir, Config)).
-define(copydir, ?config(copy_dir, Config)).

-include_lib("kernel/include/file.hrl").

-export([all/0,suite/0,groups/0,init_per_group/2,end_per_group/2]).

-export([ script_options/1, normal_script/1, no_mod_vsn_script/1,
	  wildcard_script/1, variable_script/1, no_sasl_script/1,
	  abnormal_script/1, src_tests_script/1, crazy_script/1,
	  warn_shadow_script/1,
	  included_script/1, included_override_script/1,
	  included_fail_script/1, included_bug_script/1, exref_script/1]).
-export([ tar_options/1, normal_tar/1, no_mod_vsn_tar/1, variable_tar/1,
	  src_tests_tar/1, shadow_tar/1, var_tar/1,
	  exref_tar/1, link_tar/1, otp_9507/1]).
-export([ normal_relup/1, restart_relup/1, abnormal_relup/1, no_sasl_relup/1,
	  no_appup_relup/1, bad_appup_relup/1, app_start_type_relup/1,
	  regexp_relup/1, otp_3065/1]).
-export([otp_6226/1]).
-export([normal_hybrid/1,hybrid_no_old_sasl/1,hybrid_no_new_sasl/1]).
-export([init_per_suite/1, end_per_suite/1, 
	 init_per_testcase/2, end_per_testcase/2]).

-import(lists, [foldl/3]).

-define(default_timeout, ?t:minutes(20)).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

suite() ->
    [{ct_hooks, [ts_install_cth]}].

all() -> 
    [{group, script}, {group, tar}, {group, relup}, {group, hybrid},
     {group, tickets}].

groups() -> 
    [{script, [],
      [script_options, normal_script, no_mod_vsn_script,
       wildcard_script, variable_script, abnormal_script,
       no_sasl_script, src_tests_script, crazy_script,
       warn_shadow_script, included_script, included_override_script,
       included_fail_script, included_bug_script, exref_script,
       otp_3065]},
     {tar, [],
      [tar_options, normal_tar, no_mod_vsn_tar, variable_tar,
       src_tests_tar, shadow_tar, var_tar,
       exref_tar, link_tar, otp_9507]},
     {relup, [],
      [normal_relup, restart_relup, abnormal_relup, no_sasl_relup,
       no_appup_relup, bad_appup_relup, app_start_type_relup, regexp_relup
      ]},
     {hybrid, [], [normal_hybrid,hybrid_no_old_sasl,hybrid_no_new_sasl]},
     {tickets, [], [otp_6226]}].

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


init_per_suite(Config) when is_list(Config) ->
    %% Make of copy of the data directory.
    DataDir = ?datadir,
    PrivDir = ?privdir,
    ?line CopyDir = fname(PrivDir, "datacopy"),
    ?line TarFile = fname(PrivDir, "datacopy.tgz"),
    ?line {ok, Tar} = erl_tar:open(TarFile, [write, compressed]),
    ?line ok = erl_tar:add(Tar, DataDir, CopyDir, [compressed]),
    ?line ok = erl_tar:close(Tar),
    ?line ok = erl_tar:extract(TarFile, [compressed]),
    ?line ok = file:delete(TarFile),

    %% Compile source files in the copy directory.
    ?line Sources = filelib:wildcard(fname([CopyDir,'*','*','*','*','*.erl'])),
    ?line lists:foreach(fun compile_source/1, Sources),

    %% To use in end_per_testcase
    Path = code:get_path(),
    {ok,Cwd} = file:get_cwd(),

    [{copy_dir, CopyDir}, {cwd,Cwd}, {path,Path} | Config].

compile_source(File) ->
    %% The compiler will no longer create a Beam file
    %% with a module name that does not match the output
    %% file, so we must compile to a binary and write
    %% the output file ourselves.
    U = filename:dirname(filename:dirname(File)),
    Base = filename:rootname(filename:basename(File)),
    OutFile = filename:join([U,"ebin",Base++".beam"]),
    OutFileTemp = OutFile ++ "#",
    {ok,_,Code} = compile:file(File, [binary]),
    ok = file:write_file(OutFileTemp, Code),
    file:rename(OutFileTemp, OutFile).

end_per_suite(Conf) when is_list(Conf) ->
    %% Nothing.
    Conf.

init_per_testcase(link_tar, Config) ->
    case os:type() of
	{unix, _} -> init_per_testcase(dummy, Config);
	{win32, _} -> {skip, "Skip on windows"}
    end;
init_per_testcase(_Case, Config) ->
    ?line Dog = test_server:timetrap(?default_timeout),
    [{watchdog, Dog}|Config].

end_per_testcase(_Case, Config) ->
    Dog=?config(watchdog, Config),
    test_server:timetrap_cancel(Dog),
    case {?config(path,Config),?config(cwd,Config)} of
	{undefined,undefined} ->
	    ok;
	{Path,Cwd} ->
	    true = code:set_path(Path),
	    ok = file:set_cwd(Cwd)
    end,
    ok.



%% Usage:
%%    systools:make_script("RelName")
%%                        Make a boot file from RelName.rel.
%%                        Generates RelName.{script,boot}
%%    systools:make_tar("RelName")
%%                        Make a release package from RelName.rel.
%%                        Generates RelName.tar,Z
%%    systools:script2boot(File)
%%                        File.script -> File.boot
%%    systools:make_relup("Target", ["UpFromRel"...], ["DownToRel"...], Opts)
%%			  Gather all appup scripts to the relup file
%%


%% make_script
%%
script_options(suite) -> [];
script_options(doc) ->
    ["Check illegal script options."];
script_options(Config) when is_list(Config) ->
    ?line {'EXIT',{{badarg,[{path,["Path",12,"Another"]}]}, _}} =
       (catch systools:make_script("release", [{path,["Path",12,"Another"]}])),
    ?line {'EXIT',{{badarg,[sillent]}, _}} =
	(catch systools:make_script("release",
				    [{path,["Path","Another"]},sillent])),
    ?line {'EXIT',{{badarg,[locall]}, _}} =
	(catch systools:make_script("release",
				    [{path,["Path","Another"]},locall])),
    ?line {'EXIT',{{badarg,[src_testsxx]}, _}} =
	(catch systools:make_script("release",
				    [{path,["Path"]},src_testsxx])),
    ?line {'EXIT',{{badarg,[{variables, {"TEST", "/home/lib"}}]}, _}} =
	(catch systools:make_script("release",
				    [{variables, {"TEST", "/home/lib"}}])),
    ?line {'EXIT',{{badarg,[{variables, [{a, b}, {"a", "b"}]}]}, _}} =
	(catch systools:make_script("release",
				    [{variables, [{a, b}, {"a", "b"}]}])),
    ?line {'EXIT',{{badarg,[exreff]}, _}} =
	(catch systools:make_script("release",
				    [{path,["Path","Another"]},exreff])),
    ?line {'EXIT',{{badarg,[{exref,["appl"]}]}, _}} =
	(catch systools:make_script("release", [{exref,["appl"]}])),
    ?line {'EXIT',{{badarg,[{machine, "appl"}]}, _}} =
	(catch systools:make_script("release", [{machine,"appl"}])),
    ok.


%% make_script
%%
normal_script(suite) -> [];
normal_script(doc) ->
    ["Check that make_script handles normal case."];
normal_script(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line PSAVE = code:get_path(),		% Save path

    ?line {LatestDir, LatestName} = create_script(latest,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_normal, lib]),
    ?line P1 = fname([LibDir, 'db-2.1', ebin]),
    ?line P2 = fname([LibDir, 'fe-3.1', ebin]),

    ?line true = code:add_patha(P1),
    ?line true = code:add_patha(P2),

    ?line ok = file:set_cwd(LatestDir),

    ?line ok = systools:make_script(filename:basename(LatestName)),
    ?line {ok, _} = read_script_file(LatestName),	% Check readabillity

    %% Check the same but w. silent flag
    ?line {ok, _, []} = systools:make_script(LatestName, [silent]),

    %% Use the local option
    ?line ok = systools:make_script(LatestName, [local]),
    ?line ok = check_script_path(LatestName),

    %% use the path option
    ?line code:set_path(PSAVE),			% Restore path
    %% Mess up std path:
    ?line true = code:add_patha(fname([LibDir, 'db-1.0', ebin])),
    ?line true = code:add_patha(fname([LibDir, 'fe-2.1', ebin])),

    ?line error = systools:make_script(LatestName),	%should fail
    ?line ok = systools:make_script(LatestName,[{path, [P1, P2]}]),

    ?line ok = file:set_cwd(OldDir),
    ?line code:set_path(PSAVE),			% Restore path
    ok.


%% make_script
%%
no_mod_vsn_script(suite) -> [];
no_mod_vsn_script(doc) ->
    ["Check that make_script handles normal case.",
     "Modules specified without version in .app file (db-3.1)."
     "Note that this is now the normal way - i.e. systools now "
     "ignores the module versions in the .app file."];
no_mod_vsn_script(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line PSAVE = code:get_path(),		% Save path

    ?line {LatestDir, LatestName} = create_script(latest_no_mod_vsn,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_normal, lib]),
    ?line P1 = fname([LibDir, 'db-3.1', ebin]),
    ?line P2 = fname([LibDir, 'fe-3.1', ebin]),

    ?line true = code:add_patha(P1),
    ?line true = code:add_patha(P2),

    ?line ok = file:set_cwd(LatestDir),

    ?line ok = systools:make_script(filename:basename(LatestName)),
    ?line {ok, _} = read_script_file(LatestName),	% Check readabillity

    %% Check the same but w. silent flag
    ?line {ok, _, []} = systools:make_script(LatestName, [silent]),

    %% Use the local option
    ?line ok = systools:make_script(LatestName, [local]),
    ?line ok = check_script_path(LatestName),

    %% use the path option
    ?line code:set_path(PSAVE),			% Restore path
    %% Mess up std path:
    ?line true = code:add_patha(fname([LibDir, 'db-1.0', ebin])),
    ?line true = code:add_patha(fname([LibDir, 'fe-2.1', ebin])),

    ?line error = systools:make_script(LatestName),	%should fail
    ?line ok = systools:make_script(LatestName,
				    [{path, [P1, P2]}]),

    ?line ok = file:set_cwd(OldDir),
    ?line code:set_path(PSAVE),			% Restore path
    ok.


%% make_script
%%
wildcard_script(suite) -> [];
wildcard_script(doc) ->
    ["Check that make_script handles wildcards in path."];
wildcard_script(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir, LatestName} = create_script(latest,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_normal, lib]),
    ?line WildDir = fname([LibDir, '*', ebin]),

    ?line ok = file:set_cwd(LatestDir),

    ?line error = systools:make_script(filename:basename(LatestName)),

    ?line ok = systools:make_script(LatestName,
				    [{path, [WildDir]}]),

    ?line {ok, _} = read_script_file(LatestName),	% Check readabillity

    ?line ok = file:set_cwd(OldDir),
    ok.


%% make_script
%%
variable_script(suite) -> [];
variable_script(doc) ->
    ["Add own installation dependent variable in script."];
variable_script(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir, LatestName} = create_script(latest,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_normal, lib]),
    ?line P = [fname([LibDir, 'db-2.1', ebin]),
	       fname([LibDir, 'fe-3.1', ebin])],

    ?line ok = file:set_cwd(LatestDir),

    ?line ok = systools:make_script(LatestName,
				    [{path, P},
				     {variables, [{"TEST", LibDir}]}]),

    %% Check variables
    ?line ok = check_var_script_file([fname(['$TEST', 'db-2.1', ebin]),
				      fname(['$TEST', 'fe-3.1', ebin])],
				     P,
				     LatestName),

    ?line ok = file:set_cwd(OldDir),
    ok.

%% make_script
%%
abnormal_script(suite) -> [];
abnormal_script(doc) ->
    ["Abnormal cases."];
abnormal_script(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir, LatestName} = create_script(latest,Config),

    ?line DataDir = filename:absname(?copydir),

    ?line ok = file:set_cwd(LatestDir),
    ?line LibDir = fname([DataDir, d_bad_app_vsn, lib]),
    ?line P = [fname([LibDir, 'db-2.1', ebin]),
	       fname([LibDir, 'fe-3.1', ebin])],

    %% Check wrong app vsn
    ?line error = systools:make_script(LatestName, [{path, P}]),
    ?line {error,
	   systools_make,
	   [{error_reading, {db, {no_valid_version,
				  {{"should be","2.1"},
				   {"found file", _, "2.0"}}}}}]} =
	systools:make_script(LatestName, [silent, {path, P}]),

    ?line ok = file:set_cwd(OldDir),
    ok.


%% make_script
%%
no_sasl_script(suite) -> [];
no_sasl_script(doc) ->
    ["Create script without sasl appl. Check warning."];
no_sasl_script(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir, LatestName} = create_script(latest1_no_sasl,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = [fname([DataDir, d_normal, lib])],
    ?line P = [fname([LibDir, '*', ebin]),
	       fname([DataDir, lib, kernel, ebin]),
	       fname([DataDir, lib, stdlib, ebin]),
	       fname([DataDir, lib, sasl, ebin])],

    ?line ok = file:set_cwd(LatestDir),

    ?line {ok, _ , [{warning,missing_sasl}]} =
	systools:make_script(LatestName,[{path, P},silent]),

    ?line ok = file:set_cwd(OldDir),
    ok.


%% make_script
%%
src_tests_script(suite) -> [];
src_tests_script(doc) ->
    ["Do not check date of object file or that source code can be found."];
src_tests_script(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line PSAVE = code:get_path(),		% Save path

    ?line {LatestDir, LatestName} = create_script(latest,Config),
    ?line BootFile = LatestName ++ ".boot",

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_missing_src, lib]),
    ?line P1 = fname([LibDir, 'db-2.1', ebin]),
    ?line P2 = fname([LibDir, 'fe-3.1', ebin]),
    N = [P1, P2],

    ?line ok = file:set_cwd(LatestDir),

    %% Manipulate the modification date of a beam file so it seems
    %% older than its .erl file
    ?line Erl = filename:join([P1,"..","src","db1.erl"]),
    ?line {ok, FileInfo=#file_info{mtime={{Y,M,D},T}}} = file:read_file_info(Erl),
    ?line Beam = filename:join(P1,"db1.beam"),
    ?line ok=file:write_file_info(Beam, FileInfo#file_info{mtime={{Y-1,M,D},T}}),

    %% Remove a .erl file
    ?line Erl2 = filename:join([P1,"..","src","db2.erl"]),
    ?line file:delete(Erl2),

    %% Then make script

    %% .boot file should not exist
    ?line ok = file:delete(BootFile),
    ?line false = filelib:is_regular(BootFile),
    %% With warnings_as_errors and src_tests option, an error should be issued
    ?line error =
	systools:make_script(LatestName, [silent, {path, N}, src_tests,
					  warnings_as_errors]),
    ?line error =
	systools:make_script(LatestName, [{path, N}, src_tests,
					  warnings_as_errors]),

    %% due to warnings_as_errors .boot file should still not exist
    ?line false = filelib:is_regular(BootFile),

    %% Two warnings should be issued when src_tests is given
    %% 1. old object code for db1.beam
    %% 2. missing source code for db2.beam
    ?line {ok, _, [{warning,{obj_out_of_date,_}},
		   {warning,{source_not_found,_}}]} =
	systools:make_script(LatestName, [silent, {path, N}, src_tests]),

    %% .boot file should exist now
    ?line true = filelib:is_regular(BootFile),

    %% Without the src_tests option, no warning should be issued
    ?line {ok, _, []} =
	systools:make_script(LatestName, [silent, {path, N}]),

    %% Check that the old no_module_tests option (from the time when
    %% it was default to do the src_test) is ignored
    ?line {ok, _, [{warning,{obj_out_of_date,_}},
		   {warning,{source_not_found,_}}]} =
	systools:make_script(LatestName, [silent,
					  {path, N},
					  no_module_tests,
					  src_tests]),

    ?line ok = file:set_cwd(OldDir),
    ?line code:set_path(PSAVE),
    ok.

%% make_script
%%
warn_shadow_script(suite) -> [];
warn_shadow_script(doc) ->
    ["Check that jam file out of date warning doesn't",
     "shadow bad module version error."];
warn_shadow_script(Config) when is_list(Config) ->
    %% This test has been removed since the 'vsn' attribute is
    %% not used any more, starting with R6. No warning
    %% 'obj_out_of_date' seemed to be generated.
    true.


%% make_script
%%
crazy_script(suite) -> [];
crazy_script(doc) ->
    ["Do the crazy cases."];
crazy_script(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir, LatestName} = create_script(latest, Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_normal, lib]),
    ?line P = [fname([LibDir, 'db-2.1', ebin]),
	       fname([LibDir, 'fe-3.1', ebin])],

    ?line ok = file:set_cwd(LatestDir),

    %% Run with bad path
    ?line error = systools:make_script(LatestName),
    ?line {error, _, [{error_reading, _}, {error_reading, _}]} =
	systools:make_script(LatestName, [silent]),

    %% Run with .rel file lacking kernel
    ?line {LatestDir2, LatestName2} = create_script(latest_nokernel, Config),
    ?line ok = file:set_cwd(LatestDir2),

    ?line error = systools:make_script(LatestName2),
    ?line {error, _, {missing_mandatory_app,[kernel,stdlib]}} =
	systools:make_script(LatestName2, [silent,{path,P}]),

    ?line ok = file:set_cwd(OldDir),
    ok.

%% make_script
%%
included_script(suite) -> [];
included_script(doc) ->
    ["Check that make_script handles generation of script",
     "for applications with included applications."];
included_script(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line {LatestDir, LatestName} = create_include_files(inc1, Config),
    ?line ok = file:set_cwd(LatestDir),
    ?line ok = systools:make_script(LatestName),
    ?line ok = check_include_script(LatestName,
				    [t1, t2, t3, t5, t4, t6],
				    [t1, t3, t6]),
    ?line ok = file:set_cwd(OldDir),
    ok.

%% make_script
%%
included_override_script(suite) -> [];
included_override_script(doc) ->
    ["Check that make_script handles generation of script",
     "for applications with included applications which are override by",
     "the .rel file."];
included_override_script(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line {LatestDir, LatestName} = create_include_files(inc2, Config),
    ?line ok = file:set_cwd(LatestDir),
    ?line ok = systools:make_script(LatestName),
    ?line ok = check_include_script(LatestName,
				    [t1, t2, t3, t4, t6, t5],
				    [t1, t3, t6, t5]),

    ?line {_, LatestName1} = create_include_files(inc3, Config),
    ?line ok = systools:make_script(LatestName1),
    ?line ok = check_include_script(LatestName1,
				    [t3, t5, t4, t6, t1, t2],
				    [t3, t6, t1, t2]),

    ?line {_, LatestName2} = create_include_files(inc4, Config),
    ?line ok = systools:make_script(LatestName2),
    ?line ok = check_include_script(LatestName2,
				    [t3, t4, t6, t5, t1, t2],
				    [t3, t6, t5, t1, t2]),

    ?line {_, LatestName3} = create_include_files(inc5, Config),
    ?line ok = systools:make_script(LatestName3),
    ?line ok = check_include_script(LatestName3,
				    [t3, t4, t6, t1, t2],
				    [t3, t6, t1, t2]),

    ?line ok = file:set_cwd(OldDir),
    ok.

%% make_script
%%
included_fail_script(suite) -> [];
included_fail_script(doc) ->
    ["Check that make_script handles errors then generating",
     "script with included applications."];
included_fail_script(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line {LatestDir, LatestName} = create_include_files(inc6, Config),
    ?line ok = file:set_cwd(LatestDir),
    ?line {error, _, {undefined_applications,[t2]}} =
	systools:make_script(LatestName, [silent]),

    ?line {_, LatestName1} = create_include_files(inc7, Config),
    ?line {error, _, {duplicate_include,[{{t5,t7,_,_},{t5,t6,_,_}}]}} =
	systools:make_script(LatestName1, [silent]),

    ?line {_, LatestName3} = create_include_files(inc9, Config),
    ?line {error, _, {circular_dependencies,[{t10,_},{t8,_}]}} =
	systools:make_script(LatestName3, [silent]),

    ?line {_, LatestName4} = create_include_files(inc10, Config),
    ?line {error, _, [{error_reading,{t9,{override_include,[t7]}}}]} =
	systools:make_script(LatestName4, [silent]),

    ?line ok = file:set_cwd(OldDir),
    ok.

%% make_script
%%
included_bug_script(suite) -> [];
included_bug_script(doc) ->
    ["Check that make_script handles generation of script",
     "with difficult dependency for included applications."];
included_bug_script(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line {LatestDir, LatestName} = create_include_files(inc11, Config),
    ?line ok = file:set_cwd(LatestDir),
    ?line ok = systools:make_script(LatestName),
    ?line ok = check_include_script(LatestName,
				    [t13, t11, t12],
				    [t11, t12]),
    ?line ok = file:set_cwd(OldDir),
    ok.


%% make_script
%%
otp_3065(suite) -> [];
otp_3065(doc) ->
    ["Circular dependencies in systools:make_script()."];
otp_3065(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line {LatestDir, LatestName} = create_include_files(otp_3065, Config),
    ?line ok = file:set_cwd(LatestDir),
    ?line ok = systools:make_script(LatestName),
    ?line ok = check_include_script(LatestName,
				    [aa12, chAts, chTraffic],
				    [chTraffic]),
    ?line ok = file:set_cwd(OldDir),
    ok.


%% make_script
%%
exref_script(suite) -> [];
exref_script(doc) ->
    ["Check that make_script exref option works."];
exref_script(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line PSAVE = code:get_path(),		% Save path

    ?line {LatestDir, LatestName} = create_script(latest,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_normal, lib]),
    ?line P = [fname([LibDir, 'db-2.1', ebin]),
	       fname([LibDir, 'fe-3.1', ebin])],

    ?line ok = file:set_cwd(LatestDir),

    ?line {ok, _, _} = systools:make_script(LatestName, [{path,P}, silent]),

    %% Complete exref
    ?line {ok, _, W1} = 
	systools:make_script(LatestName, [exref, {path,P}, silent]),
    ?line check_exref_warnings(with_db1, W1),
    ?line {ok, _} = read_script_file(LatestName),	% Check readabillity

    %% Only exref the db application.
    ?line {ok, _, W2} =
	systools:make_script(LatestName, [{exref,[db]}, {path,P}, silent]),
    ?line check_exref_warnings(with_db1, W2),
    ?line {ok, _} = read_script_file(LatestName),	% Check readabillity

    %% Only exref the fe application.
    ?line {ok, _, W3} =
	systools:make_script(LatestName, [{exref,[fe]}, {path,P}, silent]),
    ?line check_exref_warnings(without_db1, W3),
    ?line {ok, _} = read_script_file(LatestName),	% Check readabillity

    %% exref the db and stdlib applications.
    ?line {ok, _, W4} =
	systools:make_script(LatestName, [{exref,[db,stdlib]}, {path,P}, silent]),
    ?line check_exref_warnings(with_db1, W4),
    ?line {ok, _} = read_script_file(LatestName),	% Check readabillity
    ?line ok = file:set_cwd(OldDir),
    ?line code:set_path(PSAVE),			% Restore path
    ok.

check_exref_warnings(with_db1, W) ->
    case get_exref(undef, W) of
	{ok, [{db2,non_existing_func,0},
	      {fe2,non_existing_func,0},
	      {lists,non_existing_func,1}]} ->
	    ok;
	{ok, L} ->
	    test_server:fail({exref_warning_undef, L});
	_E ->
	    test_server:fail({bad_undef,_E})
    end;
check_exref_warnings(without_db1, W) ->
    case get_exref(undef, W) of
	false ->
	    ok;
	{ok, L} ->
	    test_server:fail({exref_warning_undef, L})
    end.

get_exref(undef, W)   -> filter(no_hipe(get_exref1(exref_undef, W))).

filter(false) ->
    false;
filter({ok, W}) ->
    {ok, filter(W)};
filter(L) ->
    lists:filter(fun%({hipe_consttab,_,_}) -> false;
		    ({int,_,_}) -> false;
		    ({i,_,_}) -> false;
		    ({crypto,_,_}) -> false;
		    (_) -> true
		 end,
		 L).

get_exref1(T, [{warning, {T, Value}}|_]) -> {ok, Value};
get_exref1(T, [_|W])                     -> get_exref1(T, W);
get_exref1(_, [])                        -> false.

no_hipe(false) ->
    false;
no_hipe({ok, Value}) ->
    case erlang:system_info(hipe_architecture) of
	undefined ->
	    Hipe = "hipe",
	    Fun = fun({M,_,_}) -> not lists:prefix(Hipe, atom_to_list(M)) end,
	    NewValue = lists:filter(Fun, Value),
	    {ok, NewValue};
	_Arch ->
	    {ok, Value}
    end.

%% tar_options
%%
tar_options(suite) -> [];
tar_options(doc) ->
    ["Check illegal tar options."];
tar_options(Config) when is_list(Config) ->
    ?line {'EXIT',{{badarg,[{path,["Path",12,"Another"]}]}, _}} =
	(catch systools:make_tar("release", [{path,["Path",12,"Another"]}])),
    ?line {'EXIT',{{badarg,[sillent]}, _}} =
	(catch systools:make_tar("release",
				 [{path,["Path","Another"]},sillent])),
    ?line {'EXIT',{{badarg,[{dirs,["dirs"]}]}, _}} =
	(catch systools:make_tar("release", [{dirs, ["dirs"]}])),
    ?line {'EXIT',{{badarg,[{erts, illegal}]}, _}} =
	(catch systools:make_tar("release", [{erts, illegal}])),
    ?line {'EXIT',{{badarg,[src_testsxx]}, _}} =
	(catch systools:make_tar("release",
				 [{path,["Path"]},src_testsxx])),
    ?line {'EXIT',{{badarg,[{variables, [{a, b}, {"a", "b"}]}]}, _}} =
	(catch systools:make_tar("release",
				 [{variables, [{a, b}, {"a", "b"}]}])),
    ?line {'EXIT',{{badarg,[{var_tar, illegal}]}, _}} =
	(catch systools:make_tar("release", [{var_tar, illegal}])),
    ?line {'EXIT',{{badarg,[exreff]}, _}} =
	(catch systools:make_tar("release",
				 [{path,["Path","Another"]},exreff])),
    ?line {'EXIT',{{badarg,[{exref,["appl"]}]}, _}} =
	(catch systools:make_tar("release", [{exref,["appl"]}])),
    ?line {'EXIT',{{badarg,[{machine, "appl"}]}, _}} =
	(catch systools:make_tar("release", [{machine,"appl"}])),
    ok.


%% normal_tar
%%
normal_tar(suite) -> [];
normal_tar(doc) ->
    ["Check normal case"];
normal_tar(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir, LatestName} = create_script(latest,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_normal, lib]),
    ?line P = [fname([LibDir, 'db-2.1', ebin]),
	       fname([LibDir, 'fe-3.1', ebin])],

    ?line ok = file:set_cwd(LatestDir),

    ?line {ok, _, _} = systools:make_script(LatestName, [silent, {path, P}]),
    ?line ok = systools:make_tar(LatestName, [{path, P}]),
    ?line ok = check_tar(fname([lib,'db-2.1',ebin,'db.app']), LatestName),
    ?line {ok, _, _} = systools:make_tar(LatestName, [{path, P}, silent]),
    ?line ok = check_tar(fname([lib,'fe-3.1',ebin,'fe.app']), LatestName),

    ?line ok = file:set_cwd(OldDir),
    ok.

%% no_mod_vsn_tar
%%
no_mod_vsn_tar(suite) -> [];
no_mod_vsn_tar(doc) ->
    ["Check normal case",
     "Modules specified without version in .app file (db-3.1)."
     "Note that this is now the normal way - i.e. systools now "
     "ignores the module versions in the .app file."];
no_mod_vsn_tar(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir, LatestName} = create_script(latest_no_mod_vsn,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_normal, lib]),
    ?line P = [fname([LibDir, 'db-3.1', ebin]),
	       fname([LibDir, 'fe-3.1', ebin])],

    ?line ok = file:set_cwd(LatestDir),

    ?line {ok, _, _} = systools:make_script(LatestName, [silent, {path, P}]),
    ?line ok = systools:make_tar(LatestName, [{path, P}]),
    ?line ok = check_tar(fname([lib,'db-3.1',ebin,'db.app']), LatestName),
    ?line {ok, _, _} = systools:make_tar(LatestName, [{path, P}, silent]),
    ?line ok = check_tar(fname([lib,'fe-3.1',ebin,'fe.app']), LatestName),

    ?line ok = file:set_cwd(OldDir),
    ok.

%% variable_tar
%%
variable_tar(suite) -> [];
variable_tar(doc) ->
    ["Use variable and create separate tar (included in generated tar)."];
variable_tar(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir, LatestName} = create_script(latest,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_normal, lib]),
    ?line P = [fname([LibDir, 'db-2.1', ebin]),
	       fname([LibDir, 'fe-3.1', ebin])],

    ?line ok = file:set_cwd(LatestDir),

    ?line {ok, _, _} = systools:make_script(LatestName,
					    [silent,
					     {path, P},
					     {variables,[{"TEST", LibDir}]}]),

    ?line ok = systools:make_tar(LatestName, [{path, P},
					      {variables,[{"TEST", LibDir}]}]),
    ?line ok = check_var_tar("TEST", LatestName),

    ?line {ok, _, _} = systools:make_tar(LatestName,
					 [{path, P}, silent,
					  {variables,[{"TEST", LibDir}]}]),
    ?line ok = check_var_tar("TEST", LatestName),

    ?line ok = file:set_cwd(OldDir),
    ok.

%% link_tar
%%
link_tar(suite) -> [];
link_tar(doc) ->
    ["Check that symlinks in applications are handled correctly"];
link_tar(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir, LatestName} = create_script(latest,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_links, lib]),
    ?line P = [fname([LibDir, 'db-2.1', ebin]),
	       fname([LibDir, 'fe-3.1', ebin])],

    %% Make some links
    ?line Db1Erl = fname(['db-2.1',src,'db1.erl']),
    ?line NormalDb1Erl = fname([DataDir,d_normal,lib,Db1Erl]),
    ?line LinkDb1Erl = fname([LibDir, Db1Erl]),
    ?line ok = file:make_symlink(NormalDb1Erl, LinkDb1Erl),
    ?line Db1Beam = fname(['db-2.1',ebin,'db1.beam']),
    ?line NormalDb1Beam = fname([DataDir,d_normal,lib,Db1Beam]),
    ?line LinkDb1Beam = fname([LibDir, Db1Beam]),
    ?line ok = file:make_symlink(NormalDb1Beam, LinkDb1Beam),
    ?line FeApp = fname(['fe-3.1',ebin,'fe.app']),
    ?line NormalFeApp = fname([DataDir,d_normal,lib,FeApp]),
    ?line LinkFeApp = fname([LibDir, FeApp]),
    ?line ok = file:make_symlink(NormalFeApp, LinkFeApp),
    
    %% Create the tar and check that the linked files are included as
    %% regular files
    ?line ok = file:set_cwd(LatestDir),

    ?line {ok,_,[]} = systools:make_script(LatestName, [{path, P},silent]),

    ?line {ok, _, []} = systools:make_tar(LatestName, [{path, P}, silent]),
    ?line ok = check_tar_regular(?privdir,
				 [fname([lib,FeApp]),
				  fname([lib,Db1Beam])],
				 LatestName),
    
    ?line {ok, _, []} = systools:make_tar(LatestName, [{path, P}, silent,
						       {dirs, [src]}]),
    ?line ok = check_tar_regular(?privdir,
				 [fname([lib,FeApp]),
				  fname([lib,Db1Beam]),
				  fname([lib,Db1Erl])],
				 LatestName),

    ?line ok = file:set_cwd(OldDir),
    ok.

%% src_tests_tar
%%
src_tests_tar(suite) -> [];
src_tests_tar(doc) ->
    ["Do not check date of object file or that source code can be found."];
src_tests_tar(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir, LatestName} = create_script(latest,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_missing_src, lib]),
    ?line P1 = fname([LibDir, 'db-2.1', ebin]),
    ?line P2 = fname([LibDir, 'fe-3.1', ebin]),
    P = [P1, P2],

    ?line ok = file:set_cwd(LatestDir),

    %% Manipulate the modification date of a beam file so it seems
    %% older than the .erl file
    Erl = filename:join([P1,"..","src","db1.erl"]),
    {ok, FileInfo=#file_info{mtime={{Y,M,D},T}}} = file:read_file_info(Erl),
    Beam = filename:join(P1,"db1.beam"),
    ok = file:write_file_info(Beam, FileInfo#file_info{mtime={{Y-1,M,D},T}}),

    %% Remove a .erl file
    ?line Erl2 = filename:join([P1,"..","src","db2.erl"]),
    ?line file:delete(Erl2),

    ?line ok = systools:make_script(LatestName, [{path, P}]),

    %% Then make tar - two warnings should be issued when
    %% src_tests is given
    %% 1. old object code for db1.beam
    %% 2. missing source code for db2.beam
    ?line {ok, _, [{warning,{obj_out_of_date,_}},
    		   {warning,{source_not_found,_}}]} =
	systools:make_tar(LatestName, [{path, P}, silent,
				       {dirs, [src]},
				       src_tests]),
    ?line ok = check_tar(fname([lib,'db-2.1',src,'db1.erl']), LatestName),

    %% Without the src_tests option, no warning should be issued
    ?line {ok, _, []} = systools:make_tar(LatestName, [{path, P}, silent,
						       {dirs, [src]}]),
    ?line ok = check_tar(fname([lib,'db-2.1',src,'db1.erl']), LatestName),

    %% Check that the old no_module_tests option (from the time when
    %% it was default to do the src_test) is ignored
    ?line {ok, _, [{warning,{obj_out_of_date,_}},
    		   {warning,{source_not_found,_}}]} =
	systools:make_tar(LatestName, [{path, P}, silent,
				       {dirs, [src]},
				       no_module_tests,
				       src_tests]),
    ?line ok = check_tar(fname([lib,'db-2.1',src,'db1.erl']), LatestName),

    ?line ok = file:set_cwd(OldDir),
    ok.

%% shadow_tar
%%
shadow_tar(suite) -> [];
shadow_tar(doc) ->
    ["Check that jam file out of date warning doesn't",
     "shadow bad module version error."];
shadow_tar(Config) when is_list(Config) ->
    % This test has been commented out since the 'vsn' attribute is not used
    % any more, starting with R6. No warning 'obj_out_of_date' seemed to be
    % generated.
    true;
shadow_tar(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line PSAVE = code:get_path(),		% Save path

    ?line {LatestDir, LatestName} = create_script(latest,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, 'd_bad_mod+warn', lib]),
    ?line P = [fname([LibDir, 'db-2.1', ebin]),
	       fname([LibDir, 'fe-3.1', ebin])],

    ?line ok = file:set_cwd(LatestDir),

    ?line {error, _, _} = systools:make_tar(LatestName, [{path, P}, silent]),
    ?line {error, _, _} = systools:make_tar(LatestName, [{path, P}, silent,
							 {dirs, [src]}]),
    ?line ok = file:set_cwd(OldDir),
    ?line code:set_path(PSAVE),
    ok.


%% var_tar
%%
var_tar(suite) -> [];
var_tar(doc) ->
    ["Check that make_tar handles generation and placement of tar",
     "files for variables outside the main tar file.",
     "Test the {var_tar, include | ownfile | omit} option."];
var_tar(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line PSAVE = code:get_path(),		% Save path

    ?line {LatestDir, LatestName} = create_script(latest,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_normal, lib]),
    ?line P = [fname([LibDir, 'db-2.1', ebin]),
	       fname([LibDir, 'fe-3.1', ebin])],

    ?line ok = file:set_cwd(LatestDir),

    ?line {ok, _, _} = systools:make_script(LatestName,
					    [silent,
					     {path, P},
					     {variables,[{"TEST", LibDir}]}]),

    ?line ok = systools:make_tar(LatestName, [{path, P},
					      {var_tar, ownfile},
					      {variables,[{"TEST", LibDir}]}]),

    ?line true = exists_tar_file("TEST"), %% Also removes the file !
    ?line {error, {not_generated, _}} = check_var_tar("TEST", LatestName),

    ?line ok = systools:make_tar(LatestName, [{path, P},
					      {var_tar, omit},
					      {variables,[{"TEST", LibDir}]}]),

    ?line {error, {not_generated, _}} = check_var_tar("TEST", LatestName),
    ?line false = exists_tar_file("TEST"),

    ?line ok = systools:make_tar(LatestName, [{path, P},
					      {var_tar, include},
					      {variables,[{"TEST", LibDir}]}]),

    ?line ok = check_var_tar("TEST", LatestName),
    ?line false = exists_tar_file("TEST"),

    ?line ok = file:set_cwd(OldDir),
    ?line code:set_path(PSAVE),
    ok.


%% exref_tar
%%
exref_tar(suite) -> [];
exref_tar(doc) ->
    ["Check exref option."];
exref_tar(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir, LatestName} = create_script(latest,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_normal, lib]),
    ?line P = [fname([LibDir, 'db-2.1', ebin]),
	       fname([LibDir, 'fe-3.1', ebin])],

    ?line ok = file:set_cwd(LatestDir),

    ?line {ok, _, _} = systools:make_script(LatestName, [silent, {path, P}]),

    %% Complete exref
    ?line {ok, _, W1} =
	systools:make_tar(LatestName, [exref, {path, P}, silent]),
    ?line check_exref_warnings(with_db1, W1),
    ?line ok = check_tar(fname([lib,'db-2.1',ebin,'db.app']), LatestName),

    %% Only exref the db application.
    ?line {ok, _, W2} =
	systools:make_tar(LatestName, [{exref, [db]}, {path, P}, silent]),
    ?line check_exref_warnings(with_db1, W2),
    ?line ok = check_tar(fname([lib,'fe-3.1',ebin,'fe.app']), LatestName),

    %% Only exref the fe application.
    ?line {ok, _, W3} =
	systools:make_tar(LatestName, [{exref, [fe]}, {path, P}, silent]),
    ?line check_exref_warnings(without_db1, W3),
    ?line ok = check_tar(fname([lib,'db-2.1',ebin,'db.app']), LatestName),

    %% exref the db and stdlib applications.
    ?line {ok, _, W4} =
	systools:make_tar(LatestName, [{exref, [db, stdlib]},
				       {path, P}, silent]),
    ?line check_exref_warnings(with_db1, W4),
    ?line ok = check_tar(fname([lib,'fe-3.1',ebin,'fe.app']), LatestName),

    ?line ok = file:set_cwd(OldDir),
    ok.



%% otp_9507
%%
otp_9507(suite) -> [];
otp_9507(doc) ->
    ["make_tar failed when path given as just 'ebin'."];
otp_9507(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir, LatestName} = create_script(latest_small,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_normal, lib]),
    ?line FeDir = fname([LibDir, 'fe-3.1']),

    ?line ok = file:set_cwd(FeDir),

    RelName = fname([LatestDir,LatestName]),

    ?line P1 = ["./ebin",
		fname([DataDir, lib, kernel, ebin]),
		fname([DataDir, lib, stdlib, ebin]),
		fname([DataDir, lib, sasl, ebin])],
    ?line {ok, _, _} = systools:make_script(RelName, [silent, {path, P1}]),
    ?line ok = systools:make_tar(RelName, [{path, P1}]),
    ?line Content1 = tar_contents(RelName),

    ?line P2 = ["ebin",
		fname([DataDir, lib, kernel, ebin]),
		fname([DataDir, lib, stdlib, ebin]),
		fname([DataDir, lib, sasl, ebin])],

    %% Tickets solves the following line - it used to fail with
    %% {function_clause,[{filename,join,[[]]},...}
    ?line ok = systools:make_tar(RelName, [{path, P2}]),
    ?line Content2 = tar_contents(RelName),
    true = (Content1 == Content2),

    ?line ok = file:set_cwd(OldDir),

    ok.


%% The relup stuff.
%%
%%


%% make_relup
%%
normal_relup(suite) -> [];
normal_relup(doc) ->
    ["Check normal case"];
normal_relup(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir,LatestName}   = create_script(latest0,Config),
    ?line {_LatestDir1,LatestName1} = create_script(latest1,Config),
    ?line {_LatestDir2,LatestName2} = create_script(latest2,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = [fname([DataDir, d_normal, lib])],
    ?line P = [fname([LibDir, '*', ebin]),
	       fname([DataDir, lib, kernel, ebin]),
	       fname([DataDir, lib, stdlib, ebin]),
	       fname([DataDir, lib, sasl, ebin])],

    ?line ok = file:set_cwd(LatestDir),

    %% This is the ultra normal case
    ?line ok = systools:make_relup(LatestName, [LatestName1], [LatestName1],
				   [{path, P}]),
    ?line ok = check_relup([{db, "2.1"}], [{db, "1.0"}]),
    ?line {ok, _, _, []} =
	systools:make_relup(LatestName, [LatestName1], [LatestName1],
			    [{path, P}, silent]),
    ?line ok = check_relup([{db, "2.1"}], [{db, "1.0"}]),

    %% file should not be written if warnings_as_errors is enabled.
    %% delete before running tests.
    ?line ok = file:delete("relup"),

    %% Check that warnings are treated as errors
    ?line error =
	systools:make_relup(LatestName, [LatestName2], [LatestName1],
			    [{path, P}, warnings_as_errors]),
    ?line error =
	systools:make_relup(LatestName, [LatestName2], [LatestName1],
			    [{path, P}, silent, warnings_as_errors]),

    %% relup file should not exist
    ?line false = filelib:is_regular("relup"),

    %% Check that warnings get through
    ?line ok = systools:make_relup(LatestName, [LatestName2], [LatestName1],
				   [{path, P}]),
    ?line ok = check_relup([{fe, "3.1"}, {db, "2.1"}], [{db, "1.0"}]),
    ?line {ok, _, _, [pre_R15_emulator_upgrade,{erts_vsn_changed, _}]} =
	systools:make_relup(LatestName, [LatestName2], [LatestName1],
			    [{path, P}, silent]),
    ?line ok = check_relup([{fe, "3.1"}, {db, "2.1"}], [{db, "1.0"}]),

    %% relup file should exist now
    ?line true = filelib:is_regular("relup"),

    ?line ok = file:set_cwd(OldDir),
    ok.


restart_relup(suite) -> [];
restart_relup(doc) ->
    ["Test relup which includes emulator restart"];
restart_relup(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir,LatestName}   = create_script(latest0,Config),
    ?line {_LatestDir1,LatestName1} = create_script(latest1,Config),
    ?line {_LatestDir0CurrErts,LatestName0CurrErts} =
	create_script(latest0_current_erts,Config),
    ?line {_CurrentAllDir,CurrentAllName} = create_script(current_all,Config),
    ?line {_CurrentAllFutErtsDir,CurrentAllFutErtsName} =
	create_script(current_all_future_erts,Config),
    ?line {_CurrentAllFutSaslDir,CurrentAllFutSaslName} =
	create_script(current_all_future_sasl,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = [fname([DataDir, d_normal, lib])],
    ?line P = [fname([LibDir, '*', ebin]),
	       fname([DataDir, lib, kernel, ebin]),
	       fname([DataDir, lib, stdlib, ebin]),
	       fname([DataDir, lib, sasl, ebin]),
	       fname([DataDir, lib, 'sasl-9.9', ebin])],

    ?line ok = file:set_cwd(LatestDir),

    %% OTP-2561: Check that the option 'restart_emulator' generates a
    %% "restart_emulator" instruction.
    ?line {ok, _ , _, []} =
         systools:make_relup(LatestName, [LatestName1], [LatestName1],
			     [{path, P},restart_emulator,silent]),
    ?line ok = check_relup([{db, "2.1"}], [{db, "1.0"}]),
    ?line ok = check_restart_emulator(),


    %% Pre-R15 to Post-R15 upgrade
    ?line {ok, _ , _, Ws} =
         systools:make_relup(LatestName0CurrErts,
			     [LatestName1],
			     [LatestName1],
			     [{path, P},silent]),
    ?line ok = check_relup([{db,"2.1"}], [{db, "1.0"}]),
    ?line ok = check_pre_to_post_r15_restart_emulator(),
    ?line ok = check_pre_to_post_r15_warnings(Ws),


    %% Check that new sasl version generates a restart_new_emulator
    %% instruction
    ?line {ok, _ , _, []} =
         systools:make_relup(CurrentAllFutSaslName,
			     [CurrentAllName],
			     [CurrentAllName],
			     [{path, P},silent]),
    ?line ok = check_relup([{fe, "3.1"}], []),
    ?line ok = check_restart_emulator_diff_coreapp(),


    %% Check that new erts version generates a restart_new_emulator
    %% instruction, if FromSaslVsn >= R15SaslVsn
    %% (One erts_vsn_changed warning for upgrade and one for downgrade)
    ?line {ok, _ , _, [{erts_vsn_changed,_},{erts_vsn_changed,_}]} =
         systools:make_relup(CurrentAllFutErtsName,
			     [CurrentAllName],
			     [CurrentAllName],
			     [{path, P},silent]),
    ?line ok = check_relup([{fe, "3.1"}], []),
    ?line ok = check_restart_emulator_diff_coreapp(),


    %% Check that new erts version generates a restart_new_emulator
    %% instruction, and can be combined with restart_emulator opt.
    %% (One erts_vsn_changed warning for upgrade and one for downgrade)
    ?line {ok, _ , _, [{erts_vsn_changed,_},{erts_vsn_changed,_}]} =
         systools:make_relup(CurrentAllFutErtsName,
			     [CurrentAllName],
			     [CurrentAllName],
			     [{path, P},restart_emulator,silent]),
    ?line ok = check_relup([{fe, "3.1"}], []),
    ?line ok = check_restart_emulator(),
    ?line ok = check_restart_emulator_diff_coreapp(),

    ?line ok = file:set_cwd(OldDir),
    ok.


%% This test fails if wrong version numbers are seen in the relup file
%% or if any application is missing. This was triggered by OTP-1360.
check_relup(UpVsnL, DnVsnL) ->
    {ok, [{_V1, [{_, _, Up}], [{_, _, Dn}]}]} = file:consult(relup),
    [] = foldl(fun(X, Acc) ->
		       true = lists:member(X, Acc),
		       lists:delete(X, Acc) end,
	       UpVsnL,
	       [{App, Vsn} || {load_object_code,{App,Vsn,_}} <- Up]),
    [] = foldl(fun(X, Acc) ->
		       true = lists:member(X, Acc),
		       lists:delete(X, Acc) end,
	       DnVsnL,
	       [{App, Vsn} || {load_object_code,{App,Vsn,_}} <- Dn]),
    ok.

check_relup_up_only(UpVsnL) ->
    {ok, [{_V1, [{_, _, Up}], []}]} = file:consult(relup),
    [] = foldl(fun(X, Acc) ->
		       true = lists:member(X, Acc),
		       lists:delete(X, Acc) end,
	       UpVsnL,
	       [{App, Vsn} || {load_object_code,{App,Vsn,_}} <- Up]),
    ok.

check_restart_emulator() ->
    {ok, [{_V1, [{_, _, Up}], [{_, _, Dn}]}]} = file:consult(relup),
    restart_emulator = lists:last(Up),
    restart_emulator = lists:last(Dn),
    ok.

check_restart_emulator_up_only() ->
    {ok, [{_V1, [{_, _, Up}], []}]} = file:consult(relup),
    restart_emulator = lists:last(Up),
    ok.

check_restart_emulator_diff_coreapp() ->
    {ok, [{_V1, [{_, _, Up}], [{_, _, Dn}]}]} = file:consult(relup),
    [restart_new_emulator|_] = Up,
    restart_emulator = lists:last(Dn),
    ok.

check_pre_to_post_r15_restart_emulator() ->
    {ok, [{_V1, [{_, _, Up}], [{_, _, Dn}]}]} = file:consult(relup),
    restart_new_emulator = lists:last(Up),
    restart_emulator = lists:last(Dn),
    ok.

check_pre_to_post_r15_warnings(Ws) ->
    true = lists:member(pre_R15_emulator_upgrade,Ws),
    ok.

%% make_relup
%%
no_appup_relup(suite) -> [];
no_appup_relup(doc) ->
    ["Check that appup files may be missing, but only if we don't need them."];
no_appup_relup(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir,LatestName}   = create_script(latest_small,Config),
    ?line {_LatestDir0,LatestName0} = create_script(latest_small0,Config),
    ?line {_LatestDir1,LatestName1} = create_script(latest_small1,Config),

    ?line DataDir = filename:absname(?copydir),

    ?line ok = file:set_cwd(LatestDir),

    %% Check that appup might be missing
    ?line P1 = [fname([DataDir, d_no_appup, lib, 'fe-3.1', ebin]),
		fname([DataDir, lib, kernel, ebin]),
		fname([DataDir, lib, stdlib, ebin]),
		fname([DataDir, lib, sasl, ebin])],
    ?line ok =
	systools:make_relup(LatestName, [LatestName], [], [{path, P1}]),
    ?line {ok,_, _, []} =
	systools:make_relup(LatestName, [LatestName], [],
			    [silent, {path, P1}]),

    %% Check that appup might NOT be missing when we need it
    ?line P2 = [fname([DataDir, d_no_appup, lib, 'fe-3.1', ebin]),
		fname([DataDir, d_no_appup, lib, 'fe-2.1', ebin]),
		fname([DataDir, lib, kernel, ebin]),
		fname([DataDir, lib, stdlib, ebin]),
		fname([DataDir, lib, sasl, ebin])],
    ?line error =
	systools:make_relup(LatestName, [LatestName0], [], [{path, P2}]),
    ?line {error,_,{file_problem, {_,{error,{open,_,_}}}}} =
	systools:make_relup(LatestName, [], [LatestName0],
			    [silent, {path, P2}]),

    %% Check that appups missing vsn traps
    ?line P3 = [fname([DataDir, d_no_appup, lib, 'fe-2.1', ebin]),
		fname([DataDir, d_no_appup, lib, 'fe-500.18.7', ebin]),
		fname([DataDir, lib, kernel, ebin]),
		fname([DataDir, lib, stdlib, ebin]),
		fname([DataDir, lib, sasl, ebin])],

    ?line error =
	systools:make_relup(LatestName0, [LatestName1], [], [{path, P3}]),
    ?line {error,_,{no_relup, _, _, _}} =
	systools:make_relup(LatestName0, [], [LatestName1],
			    [silent, {path, P3}]),

    ?line ok = file:set_cwd(OldDir),
    ok.

%% make_relup
%%
bad_appup_relup(suite) -> [];
bad_appup_relup(doc) ->
    ["Check that badly written appup files are detected"];
bad_appup_relup(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir,LatestName}   = create_script(latest_small,Config),
    ?line {_LatestDir0,LatestName0} = create_script(latest_small0,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line N2 = [fname([DataDir, d_bad_appup, lib, 'fe-3.1', ebin]),
		fname([DataDir, d_bad_appup, lib, 'fe-2.1', ebin]),
		fname([DataDir, lib, kernel, ebin]),
		fname([DataDir, lib, stdlib, ebin]),
		fname([DataDir, lib, sasl, ebin])],

    ?line ok = file:set_cwd(LatestDir),

    %% Check that bad appup is trapped
    ?line error =
	systools:make_relup(LatestName, [LatestName0], [], [{path, N2}]),
    ?line {error,_,{file_problem, {_, {error, {parse,_, _}}}}} =
	systools:make_relup(LatestName, [], [LatestName0],
			    [silent, {path, N2}]),

    ?line ok = file:set_cwd(OldDir),
    ok.

%% make_relup
%%
abnormal_relup(suite) -> [];
abnormal_relup(doc) ->
    ["Check some abnormal cases"];
abnormal_relup(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir,LatestName}   = create_script(latest0,Config),
    ?line {_LatestDir1,LatestName1} = create_script(latest1,Config),

    %% Check wrong app vsn
    ?line DataDir = filename:absname(?copydir),
    ?line P = [fname([DataDir, d_bad_app_vsn, lib, 'db-2.1', ebin]),
	       fname([DataDir, d_bad_app_vsn, lib, 'fe-3.1', ebin]),
	       fname([DataDir, lib, kernel, ebin]),
	       fname([DataDir, lib, stdlib, ebin]),
	       fname([DataDir, lib, sasl, ebin])],
    
    ?line ok = file:set_cwd(LatestDir),

    ?line error = systools:make_relup(LatestName, [LatestName1], [LatestName1],
				      [{path, P}]),
    ?line R0 = systools:make_relup(LatestName, [LatestName1], [LatestName1],
			      [silent, {path, P}]),
    ?line {error,systools_make,
	     [{error_reading,{db,{no_valid_version,
				  {{"should be","2.1"},
				   {"found file", _, "2.0"}}}}}]} = R0,
    ?line ok = file:set_cwd(OldDir),
    ok.


%% make_relup
%%
no_sasl_relup(suite) -> [];
no_sasl_relup(doc) ->
    ["Check relup can not be created is sasl is not in rel file"];
no_sasl_relup(Config) when is_list(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line {Dir1,Name1} = create_script(latest1_no_sasl,Config),
    ?line {_Dir2,Name2} = create_script(latest1,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = [fname([DataDir, d_normal, lib])],
    ?line P = [fname([LibDir, '*', ebin]),
	       fname([DataDir, lib, kernel, ebin]),
	       fname([DataDir, lib, stdlib, ebin]),
	       fname([DataDir, lib, sasl, ebin])],

    ?line ok = file:set_cwd(Dir1),

    ?line error = systools:make_relup(Name2, [Name1], [Name1], [{path, P}]),
    ?line R1 = systools:make_relup(Name2, [Name1], [Name1],[silent, {path, P}]),
    ?line {error,systools_relup,{missing_sasl,_}} = R1,

    ?line error = systools:make_relup(Name1, [Name2], [Name2], [{path, P}]),
    ?line R2 = systools:make_relup(Name1, [Name2], [Name2],[silent, {path, P}]),
    ?line {error,systools_relup,{missing_sasl,_}} = R2,

    ?line ok = file:set_cwd(OldDir),
    ok.


%% Check that application start type is used in relup
app_start_type_relup(suite) ->
    [];
app_start_type_relup(doc) ->
    ["Release upgrade file with various application start types"];
app_start_type_relup(Config) when is_list(Config) ->
    ?line PrivDir = ?config(priv_dir, Config),
    ?line {Dir1,Name1} = create_script(latest_app_start_type1,Config),
    ?line {Dir2,Name2} = create_script(latest_app_start_type2,Config),
    ?line Release1 = filename:join(Dir1,Name1),
    ?line Release2 = filename:join(Dir2,Name2),

    ?line {ok, Release2Relup, systools_relup, []} = systools:make_relup(Release2, [Release1], [Release1], [{outdir, PrivDir}, silent]),
    ?line {"LATEST_APP_START_TYPE2",
	   [{"LATEST_APP_START_TYPE1",[], UpInstructions}],
	   [{"LATEST_APP_START_TYPE1",[], DownInstructions}]} = Release2Relup,
    %% ?t:format("Up: ~p",[UpInstructions]),
    %% ?t:format("Dn: ~p",[DownInstructions]),
    ?line [{load_object_code, {mnesia, _, _}},
           {load_object_code, {runtime_tools, _, _}},
           {load_object_code, {webtool, _, _}},
           {load_object_code, {snmp, _, _}},
           {load_object_code, {xmerl, _, _}},
           point_of_no_return
           | UpInstructionsT] = UpInstructions,
    ?line true = lists:member({apply,{application,start,[mnesia,permanent]}}, UpInstructionsT),
    ?line true = lists:member({apply,{application,start,[runtime_tools,transient]}}, UpInstructionsT),
    ?line true = lists:member({apply,{application,start,[webtool,temporary]}}, UpInstructionsT),
    ?line true = lists:member({apply,{application,load,[snmp]}}, UpInstructionsT),
    ?line false = lists:any(fun({apply,{application,_,[xmerl|_]}}) -> true; (_) -> false end, UpInstructionsT),
    ?line [point_of_no_return | DownInstructionsT] = DownInstructions,
    ?line true = lists:member({apply,{application,stop,[mnesia]}}, DownInstructionsT),
    ?line true = lists:member({apply,{application,stop,[runtime_tools]}}, DownInstructionsT),
    ?line true = lists:member({apply,{application,stop,[webtool]}}, DownInstructionsT),
    ?line true = lists:member({apply,{application,stop,[snmp]}}, DownInstructionsT),
    ?line true = lists:member({apply,{application,stop,[xmerl]}}, DownInstructionsT),
    ?line true = lists:member({apply,{application,unload,[mnesia]}}, DownInstructionsT),
    ?line true = lists:member({apply,{application,unload,[runtime_tools]}}, DownInstructionsT),
    ?line true = lists:member({apply,{application,unload,[webtool]}}, DownInstructionsT),
    ?line true = lists:member({apply,{application,unload,[snmp]}}, DownInstructionsT),
    ?line true = lists:member({apply,{application,unload,[xmerl]}}, DownInstructionsT),
    ok.


%% regexp_relup
regexp_relup(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir,LatestName}   = create_script(latest_small,Config),
    ?line {_LatestDir0,LatestName0} = create_script(latest_small0,Config),
    ?line {_LatestDir1,LatestName1} = create_script(latest_small2,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line P = [fname([DataDir, d_regexp_appup, lib, '*', ebin]),
	       fname([DataDir, lib, kernel, ebin]),
	       fname([DataDir, lib, stdlib, ebin]),
	       fname([DataDir, lib, sasl, ebin])],

    ?line ok = file:set_cwd(LatestDir),

    %% Upgrade fe 2.1 -> 3.1, and downgrade 2.1 -> 3.1
    %% Shall match the first entry if fe-3.1 appup.
    ?line {ok, _, _, []} =
	systools:make_relup(LatestName, [LatestName0], [LatestName0],
			    [{path, P}, silent]),
    ?line ok = check_relup([{fe, "3.1"}], [{fe, "2.1"}]),

    %% Upgrade fe 2.1.1 -> 3.1
    %% Shall match the second entry in fe-3.1 appup. Have added a
    %% restart_emulator instruction there to distinguish it from
    %% the first entry...
    ?line {ok, _, _, []} =
	systools:make_relup(LatestName, [LatestName1], [], [{path, P}, silent]),
    ?line ok = check_relup_up_only([{fe, "3.1"}]),
    ?line ok = check_restart_emulator_up_only(),

    %% Attempt downgrade fe 3.1 -> 2.1.1
    %% Shall not match any entry!!
    ?line {error,systools_relup,{no_relup,_,_,_}} =
	systools:make_relup(LatestName, [], [LatestName1], [{path, P}, silent]),

    ?line ok = file:set_cwd(OldDir),

    ok.


%% For upgrade of erts - create a boot file which is a hybrid between
%% old and new release - i.e. starts erts, kernel, stdlib, sasl from
%% new release, all other apps from old release.
normal_hybrid(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line {Dir1,Name1} = create_script(latest1,Config),
    ?line {_Dir2,Name2} = create_script(current_all,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = [fname([DataDir, d_normal, lib])],
    ?line P = [fname([LibDir, '*', ebin]),
	       fname([DataDir, lib, kernel, ebin]),
	       fname([DataDir, lib, stdlib, ebin]),
	       fname([DataDir, lib, sasl, ebin])],

    ?line ok = file:set_cwd(Dir1),

    ?line {ok, _ , []} = systools:make_script(Name1,[{path, P},silent]),
    ?line {ok, _ , []} = systools:make_script(Name2,[{path, P},silent]),
    ?line {ok,Boot1} = file:read_file(Name1 ++ ".boot"),
    ?line {ok,Boot2} = file:read_file(Name2 ++ ".boot"),

    ?line ok = file:set_cwd(OldDir),

    ?line BasePaths = {"testkernelpath","teststdlibpath","testsaslpath"},
    ?line {ok,Hybrid} = systools_make:make_hybrid_boot("tmp_vsn",Boot1,Boot2,
						       BasePaths, [dummy,args]),

    ?line {script,{"Test release","tmp_vsn"},Script} = binary_to_term(Hybrid),
    ct:log("~p.~n",[Script]),

    %% Check that all paths to base apps are replaced by paths from BaseLib
    Boot1Str = io_lib:format("~p~n",[binary_to_term(Boot1)]),
    HybridStr = io_lib:format("~p~n",[binary_to_term(Hybrid)]),
    ReOpts = [global,{capture,first,list},unicode],
    ?line {match,OldKernelMatch} = re:run(Boot1Str,"kernel-[0-9\.]+",ReOpts),
    ?line {match,OldStdlibMatch} = re:run(Boot1Str,"stdlib-[0-9\.]+",ReOpts),
    ?line {match,OldSaslMatch} = re:run(Boot1Str,"sasl-[0-9\.]+",ReOpts),

    ?line nomatch = re:run(HybridStr,"kernel-[0-9\.]+",ReOpts),
    ?line nomatch = re:run(HybridStr,"stdlib-[0-9\.]+",ReOpts),
    ?line nomatch = re:run(HybridStr,"sasl-[0-9\.]+",ReOpts),
    ?line {match,NewKernelMatch} = re:run(HybridStr,"testkernelpath",ReOpts),
    ?line {match,NewStdlibMatch} = re:run(HybridStr,"teststdlibpath",ReOpts),
    ?line {match,NewSaslMatch} = re:run(HybridStr,"testsaslpath",ReOpts),

    NewKernelN = length(NewKernelMatch),
    ?line NewKernelN = length(OldKernelMatch),
    NewStdlibN = length(NewStdlibMatch),
    ?line NewStdlibN = length(OldStdlibMatch),
    NewSaslN = length(NewSaslMatch),
    ?line NewSaslN = length(OldSaslMatch),

    %% Check that application load instruction has correct versions
    Apps = application:loaded_applications(),
    {_,_,KernelVsn} = lists:keyfind(kernel,1,Apps),
    {_,_,StdlibVsn} = lists:keyfind(stdlib,1,Apps),
    {_,_,SaslVsn} = lists:keyfind(sasl,1,Apps),

    ?line [KernelInfo] = [I || {kernelProcess,application_controller,
			  {application_controller,start,
			   [{application,kernel,I}]}} <- Script],
    ?line [StdlibInfo] = [I || {apply,
			  {application,load,
			   [{application,stdlib,I}]}} <- Script],
    ?line [SaslInfo] = [I || {apply,
			{application,load,
			 [{application,sasl,I}]}} <- Script],

    ?line {vsn,KernelVsn} = lists:keyfind(vsn,1,KernelInfo),
    ?line {vsn,StdlibVsn} = lists:keyfind(vsn,1,StdlibInfo),
    ?line {vsn,SaslVsn} = lists:keyfind(vsn,1,SaslInfo),

    %% Check that new_emulator_upgrade call is added
    ?line [_,{apply,{release_handler,new_emulator_upgrade,[dummy,args]}}|_] =
	lists:reverse(Script),

    %% Check that db-1.0 and fe-3.1 are used (i.e. vsns from old release)
    %% And that fe is in there (it exists in old rel but not in new)
    ?line {match,DbMatch} = re:run(HybridStr,"db-[0-9\.]+",ReOpts),
    ?line {match,[_|_]=FeMatch} = re:run(HybridStr,"fe-[0-9\.]+",ReOpts),
    ?line true = lists:all(fun(["db-1.0"]) -> true;
			(_)  -> false
		     end,
		     DbMatch),
    ?line true = lists:all(fun(["fe-3.1"]) -> true;
			(_)  -> false
		     end,
		     FeMatch),

    %% Check that script has same length as old script, plus one (the
    %% new_emulator_upgrade apply)
    {_,_,Old} = binary_to_term(Boot1),
    OldLength = length(Old),
    NewLength = length(Script),
    ?line NewLength = OldLength + 1,

    ok.

%% Check that systools_make:make_hybrid_boot fails with a meaningful
%% error message if the FromBoot does not include the sasl
%% application.
hybrid_no_old_sasl(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line {Dir1,Name1} = create_script(latest1_no_sasl,Config),
    ?line {_Dir2,Name2} = create_script(current_all,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = [fname([DataDir, d_normal, lib])],
    ?line P = [fname([LibDir, '*', ebin]),
	       fname([DataDir, lib, kernel, ebin]),
	       fname([DataDir, lib, stdlib, ebin]),
	       fname([DataDir, lib, sasl, ebin])],

    ?line ok = file:set_cwd(Dir1),

    ?line {ok, _ , [{warning,missing_sasl}]} =
	systools:make_script(Name1,[{path, P},silent]),
    ?line {ok, _ , []} = systools:make_script(Name2,[{path, P},silent]),
    ?line {ok,Boot1} = file:read_file(Name1 ++ ".boot"),
    ?line {ok,Boot2} = file:read_file(Name2 ++ ".boot"),

    ?line BasePaths = {"testkernelpath","teststdlibpath","testsaslpath"},
    ?line {error,{app_not_replaced,sasl}} =
	systools_make:make_hybrid_boot("tmp_vsn",Boot1,Boot2,
				       BasePaths,[dummy,args]),

    ?line ok = file:set_cwd(OldDir),
    ok.


%% Check that systools_make:make_hybrid_boot fails with a meaningful
%% error message if the ToBoot does not include the sasl
%% application.
hybrid_no_new_sasl(Config) ->
    ?line {ok, OldDir} = file:get_cwd(),
    ?line {Dir1,Name1} = create_script(latest1,Config),
    ?line {_Dir2,Name2} = create_script(current_all_no_sasl,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = [fname([DataDir, d_normal, lib])],
    ?line P = [fname([LibDir, '*', ebin]),
	       fname([DataDir, lib, kernel, ebin]),
	       fname([DataDir, lib, stdlib, ebin]),
	       fname([DataDir, lib, sasl, ebin])],

    ?line ok = file:set_cwd(Dir1),

    ?line {ok, _ , []} = systools:make_script(Name1,[{path, P},silent]),
    ?line {ok, _ , [{warning,missing_sasl}]} =
	systools:make_script(Name2,[{path, P},silent]),
    ?line {ok,Boot1} = file:read_file(Name1 ++ ".boot"),
    ?line {ok,Boot2} = file:read_file(Name2 ++ ".boot"),

    ?line BasePaths = {"testkernelpath","teststdlibpath","testsaslpath"},
    ?line {error,{app_not_found,sasl}} =
	systools_make:make_hybrid_boot("tmp_vsn",Boot1,Boot2,
				       BasePaths,[dummy,args]),

    ?line ok = file:set_cwd(OldDir),
    ok.



otp_6226(suite) ->
    [];
otp_6226(doc) ->
    ["{outdir,Dir} option for systools:make_script()"];
otp_6226(Config) when is_list(Config) ->
    PrivDir = ?privdir,
    ?line {ok, OldDir} = file:get_cwd(),

    ?line {LatestDir, LatestName} = create_script(latest0,Config),
    ?line {_LatestDir, LatestName1} = create_script(latest1,Config),

    ?line DataDir = filename:absname(?copydir),
    ?line LibDir = fname([DataDir, d_normal, lib]),
    ?line P = [fname([LibDir, 'db-2.1', ebin]),
	       fname([LibDir, 'db-1.0', ebin]),
	       fname([LibDir, 'fe-3.1', ebin]),
	       fname([DataDir, lib, kernel, ebin]),
	       fname([DataDir, lib, stdlib, ebin]),
	       fname([DataDir, lib, sasl, ebin])],

    ?line ok = file:set_cwd(LatestDir),

 
    %% Create an outdir1 directory
    ?line ok = file:make_dir("outdir1"),

    %% ==== Now test systools:make_script ====
    %% a) badarg
    ?line {'EXIT', {{badarg,[{outdir,outdir1}]}, _}} =
	(catch systools:make_script(LatestName, [{outdir,outdir1},
						 {path,P},silent])),

    %% b) absolute path
    Outdir1 = filename:join(PrivDir, "outdir1"),
    ?line {ok,_,[]} = systools:make_script(LatestName, [{outdir,Outdir1},
							{path,P},silent]),
    ?line Script1 = filename:join(Outdir1, LatestName ++ ".script"),
    ?line Boot1 = filename:join(Outdir1, LatestName ++ ".boot"),
    ?line true = filelib:is_file(Script1),
    ?line true = filelib:is_file(Boot1),
    ?line ok = file:delete(Script1),
    ?line ok = file:delete(Boot1),

    %% c) relative path
    ?line {ok,_,[]} = systools:make_script(LatestName, [{outdir,"./outdir1"},
							{path,P},silent]),
    ?line true = filelib:is_file(Script1),
    ?line true = filelib:is_file(Boot1),
    ?line ok = file:delete(Script1),
    ?line ok = file:delete(Boot1),

    %% d) absolute but incorrect path
    ?line Outdir2 = filename:join(PrivDir, "outdir2"),
    ?line Script2 = filename:join(Outdir2, LatestName ++ ".script"),
    ?line {error,_,{open,Script2,_}} = 
	systools:make_script(LatestName, [{outdir,Outdir2},{path,P},silent]),

    %% e) relative but incorrect path
    ?line {error,_,{open,_,_}} = 
	systools:make_script(LatestName, [{outdir,"./outdir2"},{path,P},silent]),

    %% f) with .rel in another directory than cwd
    ?line ok = file:set_cwd(Outdir1),
    ?line {ok,_,[]} = systools:make_script(filename:join(PrivDir, LatestName),
					   [{outdir,"."},{path,P},silent]),
    ?line true = filelib:is_file(LatestName ++ ".script"),
    ?line true = filelib:is_file(LatestName ++ ".boot"),
    ?line ok = file:delete(LatestName ++ ".script"),
    ?line ok = file:delete(LatestName ++ ".boot"),
    ?line ok = file:set_cwd(LatestDir),

    %% ==== Now test systools:make_tar =====
    ?line {ok,_,[]} = systools:make_script(LatestName, [{path,P},silent]),
    %% a) badarg
    ?line {'EXIT', {{badarg, [{outdir,outdir1}]}, _}} =
    	(catch systools:make_tar(LatestName,[{outdir,outdir1},{path,P},silent])),

    %% b) absolute path
    ?line {ok,_,[]} = systools:make_tar(LatestName, [{outdir,Outdir1}, 
						     {path,P},silent]),
    ?line Tar1 = filename:join(Outdir1,LatestName++".tar.gz"),
    ?line true = filelib:is_file(Tar1),
    ?line ok = file:delete(Tar1),

    %% c) relative path
    ?line {ok,_,[]} = systools:make_tar(LatestName, [{outdir,"./outdir1"},
						     {path,P},silent]),
    ?line true = filelib:is_file(Tar1),
    ?line ok = file:delete(Tar1),

    %% d) absolute but incorrect path
    ?line Tar2 = filename:join(Outdir2,LatestName++".tar.gz"),
    ?line {error,_,{tar_error,{open,Tar2,{Tar2,enoent}}}} = 
	systools:make_tar(LatestName, [{outdir,Outdir2},{path,P},silent]),
    
    %% e) relative but incorrect path
    ?line {error,_,{tar_error,{open,_,_}}} = 
	   systools:make_tar(LatestName, [{outdir,"./outdir2"},{path,P},silent]),

    %% f) with .rel in another directory than cwd
    ?line ok = file:set_cwd(Outdir1),
    ?line {ok,_,[]} = systools:make_tar(filename:join(PrivDir, LatestName),
					[{outdir,"."},{path,P},silent]),
    ?line true = filelib:is_file(Tar1),
    ?line ok = file:set_cwd(LatestDir),

    %% ===== Now test systools:make_relup =====
    %% a) badarg
    ?line {'EXIT', {{badarg, [{outdir,outdir1}]}, _}} =
    	(catch systools:make_relup(LatestName,[LatestName1],[LatestName1],
    				   [{outdir,outdir1}, 
				    {path,P},silent])),

    %% b) absolute path
    Relup = filename:join(Outdir1, "relup"),
    ?line {ok,_,_,[]} = systools:make_relup(LatestName,[LatestName1],[LatestName1],
					    [{outdir,Outdir1},
					     {path,P},silent]),
    ?line true = filelib:is_file(Relup),
    ?line ok = file:delete(Relup),
    
    %% c) relative path
    ?line {ok,_,_,[]} = systools:make_relup(LatestName,[LatestName1],[LatestName1],
					    [{outdir,"./outdir1"},
					     {path,P},silent]),
    ?line true = filelib:is_file(Relup),
    ?line ok = file:delete(Relup),
    
    %% d) absolute but incorrect path
    ?line {error,_,{file_problem,{"relup",enoent}}} = 
	systools:make_relup(LatestName,[LatestName1],[LatestName1],
			    [{outdir,Outdir2},{path,P},silent]),
    
    %% e) relative but incorrect path
    ?line {error,_,{file_problem,{"relup",enoent}}} = 
	systools:make_relup(LatestName,[LatestName1],[LatestName1],
			    [{outdir,"./outdir2"},{path,P},silent]),

    %% f) with .rel in another directory than cwd
    %% -- not necessary to test since relup by default is placed in
    %%    cwd, not in the same directory as the .rel file --

    %% Change back to previous working directory
    ?line ok = file:set_cwd(OldDir),
    ok.


%%%%%%
%%%%%% Utilities
%%%%%%

check_script_path(RelName) ->
    {ok, [Conts]} = read_script_file(RelName),
    {script, {_, _}, ListOfThings} = Conts,
    case lists:keysearch(path, 1, ListOfThings) of
	{value, {path, [$$,$R,$O,$O,$T | _]}} -> %"$ROOT..."
	    false;
	_ -> ok
    end.

check_var_script_file(VarDirs, NoExistDirs, RelName) ->
    {ok, [Conts]} = read_script_file(RelName),
    {script, {_, _}, ListOfThings} = Conts,
    AllPaths = lists:append(lists:map(fun({path, P}) -> P;
					 (_)         -> []
				      end,
				  ListOfThings)),
    case lists:filter(fun(VarDir) -> lists:member(VarDir, AllPaths) end,
		      VarDirs) of
	VarDirs ->
	    ok;
	_ ->
	    test_server:fail("All variable dirs not in generated script")
    end,
    case lists:filter(fun(NoExistDir) -> lists:member(NoExistDir, AllPaths) end,
		      NoExistDirs) of
	[] ->
	    ok;
	_ ->
	    test_server:fail("Unexpected dirs in generated script")
    end.

check_include_script(RelName, ExpectedLoad, ExpectedStart) ->
    {ok, [Conts]} = read_script_file(RelName),
    {script, {_, _}, ListOfThings} = Conts,

    %% Check that the applications are loaded in given order !
    ActualLoad = 
	[App || {apply,{application,load,[{application,App,_}]}} <- ListOfThings,
		App=/=kernel,
		App=/=stdlib],
    
    if ActualLoad =:= ExpectedLoad -> ok;
       true -> test_server:fail({bad_load_order, ActualLoad, ExpectedLoad})
    end,

    %% Check that applications are started in given order !
    ActualStart =
	[App || {apply,{application,start_boot,[App|_]}} <- ListOfThings,
		App =/= kernel,
		App =/= stdlib],

    if ActualStart =:= ExpectedStart -> ok;
       true -> test_server:fail({bad_start_order, ActualStart,ExpectedStart})
    end,

    ok.

read_script_file(RelName) ->
    file:consult(RelName ++ ".script").

check_var_tar(Variable, RelName) ->
    Expected = tar_name(Variable),
    case check_tar(Expected,RelName) of
	ok ->
	    ok;
	{error, {erroneous_tar_file, _, missing, _}} ->
	    {error, {not_generated, Expected}}
    end.

exists_tar_file(Name) ->
    File = tar_name(Name),
    case filelib:is_regular(File) of
	true ->
	    ok = file:delete(File),
	    true;
	_ ->
	    false
    end.

%% Take a snap of the generated tar file and check if a certain
%% file is included.
%% This ensures at least that the tar file is generated.
check_tar(File, RelName) ->
    TarContents = tar_contents(RelName),
    case lists:member(File,TarContents) of
	true -> ok;
	_    -> {error, {erroneous_tar_file, tar_name(RelName), missing, File}}
    end.

%% Check that the given files exist in the tar file, and that they are
%% not symlinks
check_tar_regular(PrivDir, Files, RelName) ->
    TmpDir = fname(PrivDir,tmp),
    ok = file:make_dir(TmpDir),
    ok = erl_tar:extract(tar_name(RelName),
			 [{files,Files},{cwd,TmpDir},compressed]),
    R = lists:foldl(fun(File,Acc) ->
			    case file:read_link_info(fname(TmpDir,File)) of 
				{ok,#file_info{type=regular}} ->
				    Acc;
				{ok,#file_info{type=Other}} ->
				    [{File,Other}|Acc];
				_ ->
				    [{File,missing}|Acc]
			    end
		    end,
		    [],
		    Files),
    delete_tree(TmpDir),
    case R of
	[] ->
	    ok;
	NotThere ->
	    {error,{erroneous_tar_file,tar_name(RelName),NotThere}}
    end.
	    
delete_tree(Dir) ->
    case filelib:is_dir(Dir) of
	true ->
	    {ok,Files} = file:list_dir(Dir),
	    lists:foreach(fun(File) -> delete_tree(filename:join(Dir,File)) end, 
			  Files),
	    file:del_dir(Dir);
	false ->
	    ok = file:delete(Dir)
    end.

tar_contents(Name) ->
    {ok, Cont} = erl_tar:table(Name ++ ".tar.gz", [compressed]),
    Cont.

tar_name(Name) ->
    Name ++ ".tar.gz".

create_script(latest,Config) ->
    Apps = core_apps(current) ++ [{db,"2.1"},{fe,"3.1"}],
    do_create_script(latest,Config,"4.4",Apps);
create_script(latest_no_mod_vsn,Config) ->
    Apps = core_apps(current) ++ [{db,"3.1"},{fe,"3.1"}],
    do_create_script(latest_no_mod_vsn,Config,"4.4",Apps);
create_script(latest0,Config) ->
    Apps = core_apps("1.0") ++ [{db,"2.1"},{fe,"3.1"}],
    do_create_script(latest0,Config,"4.4",Apps);
create_script(latest0_current_erts,Config) ->
    Apps = core_apps("1.0") ++ [{db,"2.1"},{fe,"3.1"}],
    do_create_script(latest0_current_erts,Config,current,Apps);
create_script(latest1,Config) ->
    Apps = core_apps("1.0") ++ [{db,"1.0"},{fe,"3.1"}],
    do_create_script(latest1,Config,"4.4",Apps);
create_script(latest1_no_sasl,Config) ->
    Apps = [{kernel,"1.0"},{stdlib,"1.0"},{db,"1.0"},{fe,"3.1"}],
    do_create_script(latest1_no_sasl,Config,"4.4",Apps);
create_script(latest2,Config) ->
    Apps = core_apps("1.0") ++ [{db,"1.0"},{fe,"2.1"}],
    do_create_script(latest2,Config,"4.3",Apps);
create_script(latest_small,Config) ->
    Apps = core_apps("1.0") ++ [{fe,"3.1"}],
    do_create_script(latest_small,Config,"4.4",Apps);
create_script(latest_small0,Config) ->		%Differs in fe vsn
    Apps = core_apps("1.0") ++ [{fe,"2.1"}],
    do_create_script(latest_small0,Config,"4.4",Apps);
create_script(latest_small1,Config) ->
    Apps = core_apps("1.0") ++ [{fe,"500.18.7"}],
    do_create_script(latest_small1,Config,"4.4",Apps);
create_script(latest_small2,Config) ->
    Apps = core_apps("1.0") ++ [{fe,"2.1.1"}],
    do_create_script(latest_small2,Config,"4.4",Apps);
create_script(latest_nokernel,Config) ->
    Apps = [{db,"2.1"},{fe,"3.1"}],
    do_create_script(latest_nokernel,Config,"4.4",Apps);
create_script(latest_app_start_type1,Config) ->
    Apps = core_apps(current),
    do_create_script(latest_app_start_type1,Config,current,Apps);
create_script(latest_app_start_type2,Config) ->
    OtherApps = [{mnesia,current,permanent},
		 {runtime_tools,current,transient},
		 {webtool,current,temporary},
		 {snmp,current,load},
		 {xmerl,current,none}],
    Apps = core_apps(current) ++ OtherApps,
    do_create_script(latest_app_start_type2,Config,current,Apps);
create_script(current_all_no_sasl,Config) ->
    Apps = [{kernel,current},{stdlib,current},{db,"2.1"},{fe,"3.1"}],
    do_create_script(current_all_no_sasl,Config,current,Apps);
create_script(current_all,Config) ->
    Apps = core_apps(current) ++ [{db,"2.1"}],
    do_create_script(current_all,Config,current,Apps);
create_script(current_all_future_erts,Config) ->
    Apps = core_apps(current) ++ [{db,"2.1"},{fe,"3.1"}],
    do_create_script(current_all_future_erts,Config,"99.99",Apps);
create_script(current_all_future_sasl,Config) ->
    Apps = [{kernel,current},{stdlib,current},{sasl,"9.9"},{db,"2.1"},{fe,"3.1"}],
    do_create_script(current_all_future_sasl,Config,current,Apps).


do_create_script(Id,Config,ErtsVsn,AppVsns) ->
    ?line PrivDir = ?privdir,
    ?line Name = fname(PrivDir, Id),
    ?line {ok,Fd} = file:open(Name++".rel",write),
    ?line RelfileContent =
	{release,{"Test release", string:to_upper(atom_to_list(Id))},
	 {erts,erts_vsn(ErtsVsn)},
	 app_vsns(AppVsns)},
    ?line io:format(Fd,"~p.~n",[RelfileContent]),
    ?line ok = file:close(Fd),
    {filename:dirname(Name), filename:basename(Name)}.

core_apps(Vsn) ->
    [{App,Vsn} || App <- [kernel,stdlib,sasl]].

app_vsns(AppVsns) ->
    [{App,app_vsn(App,Vsn)} || {App,Vsn} <- AppVsns] ++
	[{App,app_vsn(App,Vsn),Type} || {App,Vsn,Type} <- AppVsns].
app_vsn(App,current) ->
    application:load(App),
    {ok,Vsn} = application:get_key(App,vsn),
    Vsn;
app_vsn(_App,Vsn) ->
    Vsn.

erts_vsn(current) -> erlang:system_info(version);
erts_vsn(Vsn) -> Vsn.


create_include_files(inc1, Config) ->
    ?line PrivDir = ?privdir,
    ?line Name = fname(PrivDir, inc1),
    create_apps(PrivDir),

    ?line Apps = application_controller:which_applications(),
    ?line {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    ?line {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),

    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
          " [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
           ++ StdlibVer ++ "\"},\n"
          "  {t6, \"1.0\"}, {t5, \"1.0\"}, \n"
          "  {t4, \"1.0\"}, {t3, \"1.0\"}, {t2, \"1.0\"}, \n"
          "  {t1, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};

create_include_files(inc2, Config) ->
    ?line PrivDir = ?privdir,
    ?line Name = fname(PrivDir, inc2),
    create_apps(PrivDir),

    ?line Apps = application_controller:which_applications(),
    ?line {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    ?line {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),

    %% t6 does not include t5 !
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
          " [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
           ++ StdlibVer ++ "\"},\n"
          "  {t6, \"1.0\", [t4]}, {t5, \"1.0\"}, \n"
          "  {t4, \"1.0\"}, {t3, \"1.0\"}, {t2, \"1.0\"}, \n"
          "  {t1, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};

create_include_files(inc3, Config) ->
    ?line PrivDir = ?privdir,
    ?line Name = fname(PrivDir, inc3),
    create_apps(PrivDir),

    ?line Apps = application_controller:which_applications(),
    ?line {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    ?line {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),

    %% t3 does not include t2 !
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
          " [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
           ++ StdlibVer ++ "\"},\n"
          "  {t6, \"1.0\"}, {t5, \"1.0\"}, \n"
          "  {t4, \"1.0\"}, {t3, \"1.0\", []}, {t2, \"1.0\"}, \n"
          "  {t1, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};

create_include_files(inc4, Config) ->
    ?line PrivDir = ?privdir,
    ?line Name = fname(PrivDir, inc4),
    create_apps(PrivDir),

    ?line Apps = application_controller:which_applications(),
    ?line {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    ?line {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),

    %% t3 does not include t2 !
    %% t6 does not include t5 !
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
          " [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
           ++ StdlibVer ++ "\"},\n"
          "  {t6, \"1.0\", [t4]}, {t5, \"1.0\"}, \n"
          "  {t4, \"1.0\"}, {t3, \"1.0\", []}, {t2, \"1.0\"}, \n"
          "  {t1, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};

create_include_files(inc5, Config) ->
    ?line PrivDir = ?privdir,
    ?line Name = fname(PrivDir, inc5),
    create_apps(PrivDir),

    ?line Apps = application_controller:which_applications(),
    ?line {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    ?line {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),

    %% t6 does not include t5 !
    %% exclude t5.
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
          " [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
           ++ StdlibVer ++ "\"},\n"
          "  {t6, \"1.0\", [t4]}, \n"
          "  {t4, \"1.0\"}, {t3, \"1.0\", []}, {t2, \"1.0\"}, \n"
          "  {t1, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};

create_include_files(inc6, Config) ->
    ?line PrivDir = ?privdir,
    ?line Name = fname(PrivDir, inc6),
    create_apps(PrivDir),

    ?line Apps = application_controller:which_applications(),
    ?line {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    ?line {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),

    %% t3 does include non existing t2 !
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
          " [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
           ++ StdlibVer ++ "\"},\n"
          "  {t6, \"1.0\"}, {t5, \"1.0\"}, \n"
          "  {t4, \"1.0\"}, {t3, \"1.0\"}, \n"
          "  {t1, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};

create_include_files(inc7, Config) ->
    ?line PrivDir = ?privdir,
    ?line Name = fname(PrivDir, inc7),
    create_apps(PrivDir),
    create_app(t7, PrivDir),

    ?line Apps = application_controller:which_applications(),
    ?line {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    ?line {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),

    %% t7 and t6 does include t5 !
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
          " [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
           ++ StdlibVer ++ "\"},\n"
          "  {t7, \"1.0\"}, {t6, \"1.0\"}, {t5, \"1.0\"}, \n"
          "  {t4, \"1.0\"}, {t3, \"1.0\"}, {t2, \"1.0\"}, \n"
          "  {t1, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};

create_include_files(inc8, Config) ->
    ?line PrivDir = ?privdir,
    ?line Name = fname(PrivDir, inc8),
    create_circular_apps(PrivDir),

    ?line Apps = application_controller:which_applications(),
    ?line {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    ?line {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),

    %% t8 uses t9 and t10 includes t9 !
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
          " [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
           ++ StdlibVer ++ "\"},\n"
          "  {t8, \"1.0\"}, {t9, \"1.0\"}, {t10, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};

create_include_files(inc9, Config) ->
    ?line PrivDir = ?privdir,
    ?line Name = fname(PrivDir, inc9),
    create_circular_apps(PrivDir),

    ?line Apps = application_controller:which_applications(),
    ?line {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    ?line {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),

    %% t8 uses t9, t9 uses t10 and t10 includes t8 ==> circular !!
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
          " [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
           ++ StdlibVer ++ "\"},\n"
          "  {t8, \"1.0\"}, {t9, \"1.0\"}, {t10, \"1.0\", [t8]}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};

create_include_files(inc10, Config) ->
    ?line PrivDir = ?privdir,
    ?line Name = fname(PrivDir, inc10),
    create_circular_apps(PrivDir),

    ?line Apps = application_controller:which_applications(),
    ?line {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    ?line {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),

    %% t9 tries to include not specified (in .app file) application !
    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
          " [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
           ++ StdlibVer ++ "\"},\n"
          "  {t8, \"1.0\"}, {t9, \"1.0\", [t7]}, {t10, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};

create_include_files(inc11, Config) ->
    ?line PrivDir = ?privdir,
    ?line Name = fname(PrivDir, inc11),
    create_apps2(PrivDir),

    ?line Apps = application_controller:which_applications(),
    ?line {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    ?line {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),

    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
          " [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
           ++ StdlibVer ++ "\"},\n"
          "  {t11, \"1.0\"}, \n"
          "  {t12, \"1.0\"}, \n"
          "  {t13, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)};

create_include_files(otp_3065, Config) ->
    ?line PrivDir = ?privdir,
    ?line Name = fname(PrivDir, otp_3065),
    create_apps_3065(PrivDir),

    ?line Apps = application_controller:which_applications(),
    ?line {value,{_,_,KernelVer}} = lists:keysearch(kernel,1,Apps),
    ?line {value,{_,_,StdlibVer}} = lists:keysearch(stdlib,1,Apps),

    Rel = "{release, {\"test\",\"R1A\"}, {erts, \"45\"},\n"
          " [{kernel, \"" ++ KernelVer ++ "\"}, {stdlib, \""
           ++ StdlibVer ++ "\"},\n"
          "  {chAts, \"1.0\"}, {aa12, \"1.0\"}, \n"
          "  {chTraffic, \"1.0\"}]}.\n",
    file:write_file(Name ++ ".rel", list_to_binary(Rel)),
    {filename:dirname(Name), filename:basename(Name)}.

create_apps(Dir) ->
    T1 = "{application, t1,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, [kernel, stdlib]},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't1.app'), list_to_binary(T1)),

    T2 = "{application, t2,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, [t1]},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't2.app'), list_to_binary(T2)),

    T3 = "{application, t3,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, []},\n"
         "  {included_applications, [t2]},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't3.app'), list_to_binary(T3)),

    T4 = "{application, t4,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, [t3]},\n"
         "  {included_applications, []},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't4.app'), list_to_binary(T4)),

    T5 = "{application, t5,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, [t3]},\n"
         "  {included_applications, []},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't5.app'), list_to_binary(T5)),

    T6 = "{application, t6,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, []},\n"
         "  {included_applications, [t4, t5]},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't6.app'), list_to_binary(T6)).

create_app(t7, Dir) ->
    T7 = "{application, t7,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, []},\n"
         "  {included_applications, [t5]},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't7.app'), list_to_binary(T7)).

create_circular_apps(Dir) ->
    T8 = "{application, t8,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, [t9]},\n"
         "  {included_applications, []},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't8.app'), list_to_binary(T8)),

    T9 = "{application, t9,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, [t10]},\n"
         "  {included_applications, []},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't9.app'), list_to_binary(T9)),

    T10 = "{application, t10,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, []},\n"
         "  {included_applications, [t8, t9]},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't10.app'), list_to_binary(T10)).

create_apps2(Dir) ->
    T11 = "{application, t11,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, []},\n"
         "  {included_applications, [t13]},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't11.app'), list_to_binary(T11)),

    T12 = "{application, t12,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, [t11]},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't12.app'), list_to_binary(T12)),

    T13 = "{application, t13,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, []},\n"
         "  {included_applications, []},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 't13.app'), list_to_binary(T13)).



create_apps_3065(Dir) ->
    T11 = "{application, chTraffic,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, []},\n"
         "  {included_applications, [chAts]},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 'chTraffic.app'), list_to_binary(T11)),

    T12 = "{application, chAts,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, []},\n"
         "  {included_applications, [aa12]},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 'chAts.app'), list_to_binary(T12)),

    T13 = "{application, aa12,\n"
         " [{vsn, \"1.0\"},\n"
         "  {description, \"test\"},\n"
	 "  {modules, []},\n"
         "  {applications, [chAts]},\n"
         "  {included_applications, []},\n"
	 "  {registered, []}]}.\n",
    file:write_file(fname(Dir, 'aa12.app'), list_to_binary(T13)).

fname(N) ->
    filename:join(N).

fname(Dir, Basename) ->
    filename:join(Dir, Basename).