#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

RCSID("$Id: pagsh.c,v 1.8 1996/04/29 11:58:04 bg Exp $");

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <pwd.h>

#include <roken.h>

#include <krb.h>
#include <kafs.h>

int
main(int argc, char **argv)
{
  int f;
  char tf[1024];
  char *p;

  char *path;
  char **args;
  int i;

  do{
    sprintf(tf, "%s%d_%d", TKT_ROOT, (int)getuid(), (int)(getpid()*time(0)));
    f = open(tf, O_CREAT|O_EXCL|O_RDWR);
  }while(f < 0);
  close(f);
  unlink(tf);
  setenv("KRBTKFILE", tf, 1);

  i = 0;

  args = (char **) malloc((argc + 10)*sizeof(char *));
  
  argv++;

  if(*argv == NULL){
    path = getenv("SHELL");
    if(path == NULL){
      struct passwd *pw = getpwuid(geteuid());
      path = strdup(pw->pw_shell);
    }
  }else{
    if(strcmp(*argv, "-c") ==  0) argv++;
    path = strdup(*argv);
  }
  
  p=strrchr(path, '/');
  if(p)
    args[i++] = strdup(p+1);
  else
    args[i++] = strdup(path);
  

  while(*argv)
    args[i++] = *argv++;

  args[i] = NULL;

  if(k_hasafs())
    k_setpag();

  execvp(path, args);
  perror("execvp");
  exit(1);
}
