#!/usr/local/bin/icmake -qt /tmp/callback

#include "configure.h"

usage()
{
    printf
    (
        "\n"
        "Icmake callback build script\n"
        "Usage:\n"
        "   build arg\n"
        "Where arg:\n"
        "   clean            - to remove .o and .a files and binaries\n"
        "   dist             - to make a distribution (internal use only)\n"
        "   install callback - to install for callback in /conf/callback\n"
        "   install dial-in  - to install for dial-in in /conf/callback\n"
        "   man              - to install gzipped manpages in var/man/cat{1,4}\n"
        "   progs            - to build the programs and library\n"
        "   tar              - 'build progs' and tar the callback directories\n"
    );

    exit (1);
}

void refresh()
{
    system("rm -f cb/cb login/cblogin mgetty/cbmgetty");
}

void building(string dir)
{
    printf("\n"
           "Changing to directory '", dir, "'\n");

    chdir(dir);
    system("./build");
    chdir("..");
}

void prog(string dir, string program)
{
    printf("\n"
           "Changing to directory '", dir, "'\n");

    chdir(dir);

    if ("../lib/librss.a" younger program)
        exec("rm", "-f", program);

    system("./build");
    chdir("..");
}

void testconfigure(string source)
{
    if ("configure.h" younger source)
        exec("touch", source);
}

void progs()
{   
    if ("lib/version.c" younger "lib/librss.a")
        refresh();
    testconfigure("lib/config/configdata.c");
    testconfigure("lib/dbase/dbasedata.c");
    testconfigure("lib/parser/parsesetupfile.c");
    testconfigure("lib/process/killinit.c");
    testconfigure("lib/process/modemrespawn.c");
    testconfigure("lib/log/logdata.c");
                   
    building("lib");
    prog("cb", "cb");
    prog("mgetty", "cbmgetty");
    prog("login", "cblogin");
}

void tarring()
{
    printf("\n"
           "Tarring all information\n");

    chdir("..");
       
    system(P_NOCHECK, "tar czvf src-callback-`grep version\\\\\\[\\\\\\\] "
            "callback/version.c | cut -f2 -d\\\"`.tar.gz callback");
}
             
void tar()
{
    printf("\n"
           "Building library and programs\n");
           
    progs();
    
    tarring();
}

void clean()
{
    system("rm -f cb/cb login/cblogin mgetty/cbmgetty lib/librss.a */o/*"
           " */*/o/*");
}
                
void dist()
{
    printf("\n"
           "Making a distribution tar.gz\n");

    tar();

    chdir("callback");

    system("cp cb/cb login/cblogin mgetty/cbmgetty bin");

    clean();
       
    chdir("..");
    
    system("echo `grep version\\\\\\[\\\\\\\] callback/version.c | "
           "      cut -f2 -d\\\"` > VERSION");
        
    system(P_NOCHECK, "mv callback-* old");

    system("tar czvf callback-`cat VERSION`.tar.gz "
            "callback/[A-Z]* "
            "callback/{build,callback.{lsm,FAQ},*.h} "
            "callback/{bin,cb,examples,lib,login,man,mgetty} "
            );
            
    system("cp callback/callback.lsm callback-`cat VERSION`.lsm");

    system(P_NOCHECK, "tar xzf src-callback-`cat VERSION`.tar.gz");
    
    system("rm VERSION");
}

void alerting()
{
    printf("\n"
        "\n"
        "******************************\n"
        "* Make sure to have a link to " BASE_PATH "cb\n"
        "******************************\n"
        "\a\n"
        );
}

void man()
{
    chdir("man");
    system("groff -Tascii -mandoc cb.1 | gzip > /var/man/cat1/cb.1.gz");
    system("groff -Tascii -mandoc cblogin.1 | gzip > /var/man/cat1/cblogin.1.gz");
    system("groff -Tascii -mandoc cbmgetty.1 | gzip > /var/man/cat1/cbmgetty.1.gz");
    system("groff -Tascii -mandoc callback.4 | gzip > /var/man/cat4/callback.4.gz");
}

void instprog (string prog, string destdir)
{       
    printf("\n"
           "Installing ", prog, " in ", destdir, "\n");

    exec("chmod", "750", prog);
    exec("cp", prog, destdir);
}


void dialin()
{
    printf("Installing for dial-in\n");

    instprog ("cb/cb", BASE_PATH);
    alerting();
}
    
void callback()
{
    printf("Installing for callback\n");

    instprog ("login/cblogin",   BASE_PATH);
    instprog ("mgetty/cbmgetty", BASE_PATH);
    dialin();
}

int main(int argc, list argv)
{       
    string
        arg;

    if (argc == 1)
        usage();

    arg = element(1, argv);
    
    if (arg == "progs")
        progs();
    else if (arg == "tar")
        tar();
    else if (arg == "clean")
        clean();
    else if (arg == "man")
        man();
    else if (arg == "dist")
        dist();
    else if (arg == "install")
    {
        arg = element(2, argv);
        
        if (arg == "dial-in")
            dialin();
        else if (arg == "callback")
            callback();
        
        return (0);
    }
    else
        usage();

    return (0);
}

