#!/bin/sh

unset MAGMA_STARTUP_FILE

if [ "$1" = "-r" ]
then
    shift
    rand=1
fi

par=0
if [ "$1" = "-p" ]
then
    shift
    par=1
fi

#ARGS='-sstart -S1' 
ARGS='-sstart'

magma=$1
if [ "$magma" = "" ]
then
    magma=magma
fi

case $magma in
*magma*) echo Do save:
    $magma save
    echo Do restore:
    $magma restore
esac

# This has been carefully arranged so the most expensive tests
# are near the top (with some exceptions); ONLY add near the top if
# the test is expensive [based on 3 min alarm]:

TESTS="
    permg mat language benchmark integer \
    lll galois_groups series_ext alg_ass_max_ord_alff_inf \
    ClassGroupSieveQuadDiscPos ff_log picard_group_power_prod zeta \
    order test_cc ec_fldrat alff \
    alg_ass_max_ord_alff montes_alff_char_p \
    local_factorization CrvG1/3descent-demo.m \
    timd/lseries.m timd/galrep.m timd/galrepcrv.m timd/artin.m \
    timd/groupname.m timd/hyplser.m timd/elltwist.m timd/splitfield.m \
    cyclo smith code test_oconj lattice_genus lagrange \
    timd/rational.m hecke_char \
    dpoly dfact vfact basica numer_linalg \
    alff_completion CodesOverZ4_R_test.m ff mata orderq \
    alg_ass_max_ord_anf \
    ClassGroupSievePolys ClassGroupSieveFamily ClassGroupSieveQuadDiscNeg \
    test_sympconj order_automorphisms test_WGraph crvhyp_pmin_model \
    vpoly test_uconj test_ComplexRootData kas_prime_split fld_ab_max_order \
    galois_groups_reducible \
    grp_fp groebner gb_sol hfe irr_mods alffoq \
    decomp insep lattice invar \
    rational mod series ecpc sq \
    qform cyclo_units bool sgp_fp dpoly_local acf \
    dmodule pselmer scheme lazy gen_grp_ab ellmodel \
    real r4test resolution grp_lie_short grp_lie_reps gl_n cu_n hypercox \
    E14_example.mag tpoly locala locala_rel \
    kummer grp_pc artin \
    complex complex_roots sheaf_n_cohom \
    subfields minrank \
rspace dfact_ff hc smat vpoly_mult ff_fact random determinant dgcd \
rational_decompose list set iset mset mat_vpoly quad eob_matrix newton \
    grp_mat_inf_test series_ext_unbounded \
    beck_series beck_param small_mod_crvs \
    quad_class qform_class s_unit neq nf1 nfauto fld_fun_div \
    quat/embed-test.m quat/hilbert-test.m quat/ideals-test.m quat/interface-test.m \
    quat/iseichler-test.m quat/maxorder-test.m quat/order-test.m quat/pmatrix-test.m \
    quat/units-test.m \
    RelativeAlffTests/MoreElements \
    GrpPSL2 ModFrm/modsym ModFrm/half ModFrm/newforms ModFrm/ohs ModFrm/wt1 \
    theta_mf \
    ModFrmHil/p1 ModFrmHil/hmf1 ModFrmHil/hmf2 ModFrmHil/hmf3 \
    CrvG1/invariance.m CrvG1/discrete.m CrvG1/misc.m CrvG1/salmon-demo.m \
    CrvG1/minred-demo1.m CrvG1/minred-demo2.m CrvG1/add.m \
    Descent/2Q Descent/2isog_power Descent/ct Descent/8 Descent/2charp Descent/2char2 \
    Descent/nice \
    ec ec_formal chabauty_ec crv_reg_model delpezzo \
    matg_unip.mag matg_unip_stab.mag \
    alg_lie/cartan-tp.mag alg_lie/chevbas.mag alg_lie/fplie.mag alg_lie/melikian.mag \
    alg_lie/twisted.mag alg_lie/hwrep.mag alg_lie/extr.mag alg_lie/kac.mag \
    convex/lattice convex/cones convex/equivalence convex/isomorphism \
    convex/points convex/polytopes convex/pyramids convex/v24 \
    toric_geom/divisors toric_geom/mmp1 toric_geom/mmp2 toric_geom/mmp3 \
    toric_geom/projective toric_geom/resolution toric_geom/variety \
    toric_geom/laurent_reflexive toric_geom/ambient test_bmr\
    test_ShephardTodd test_OrderFormulae \
    test_Clifford test_quadform test_ChevOrder \
    test_cox test_Witt test_GrpLie test_GrpLieRng test_StdRep \
    test_BasicDegrees test_WittDecomp test_Forms test_NF test_GS\
    test_restmat \
    process \
    order_non_simple \
    LatNF \
    local local_inf_prec \
    galois_group_applications montes_interface \
    glnz_conjugacy local_exact \
    vring \
    Geometry/CrvMod/test_Bug_42.m \
    Geometry/ModFrm/test_Bug_251.m \
    Geometry/ModFrmHil/test_Bug_58.m \
    Geometry/ModFrmHil/test_Bug_87.m \
    Geometry/ModFrmHil/test_Bug_143.m \
    Geometry/ModFrmHil/test_Bug_144.m \
    Geometry/ModFrmHil/test_Bug_247.m \
    Geometry/RepLoc/test_Bug_72.m \
    Geometry/RepLoc/test_LW12_Ex1.m \
    Geometry/RepLoc/test_LW12_Ex2.m Geometry/RepLoc/test_LW12_Ex3.m \
    Geometry/RepLoc/test_LW12_Ex4.m \
    src/map/test_aut.m \
    integer_power2"

if [ "$par" = 1 ]
then
    case `hostname` in
    node13*)	JOBS=20;;
    node3*)	JOBS=16;;
    *)		JOBS=4;;
    esac

    if [ "$rand" = 1 ]
    then
	CMD="echo Testing {}:; $magma $ARGS -S ''"
    else
	CMD="echo Testing {}:; $magma $ARGS -S1 "
    fi

    echo "CMD: $CMD";

    for x in $TESTS
    do
	echo $x
    done | 
    parallel -j $JOBS "$CMD < {}"

    # parallel -j $JOBS "echo "Testing $x:" $magma $ARGS -S '' < {}"

    exit
fi

for x in $TESTS
do
    echo "Testing $x:"
    if [ "$rand" = 1 ]
    then
	$magma $ARGS -S '' < $x
    else
	$magma $ARGS -S1 < $x
    fi
done
