#!/bin/tcsh -f

# ================ general constants ========================================
set PROGNAME="`basename $0`"

# ================ enviroment variables =====================================
# =========================== constants =====================================
# ================== help functions =========================================
set USAGE = (list n)
if("$1" == "-H") then 
    cat << EOF
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                           V1.0, V.Strom, 11/25/2001
    Usage:  $PROGNAME $USAGE 

    list    file containing a list of tokens, one per line
    n       number of files to split \$list into
     
    result are n files "\$uttlist_i_n" with i = 1..n.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EOF
    exit 1
else if($#argv != $#USAGE || "$1" == "-h") then 
    echo2 "Usage:  $PROGNAME $USAGE" 
    exit 1
endif
# ================== catching args ==========================================
set  list  = $argv[1]
set  n        = $argv[2]

# =================== variables =============================================
# =================== init ==================================================
if($n !~ [2-9] && $n !~ [1-9][0-9]) then
    echo2 "$n does not make sense"
    exit 1
endif
# =================== main loop =============================================
set j = 1
while($j <= $n)

    set k = $j
    if($k == $n) set k = 0

    echo "creating $list.${j}_$n"
    awk '{lc++; if(lc%'$n'=='$k')print}' $list > $list.${j}_$n

    @ j++
end

# ================= script coda =============================================
exit 0;
# ==================== end ==================================================
