#!/bin/sh

#   buildmathpass -- prompt for mathpass file creation

if [ -f $ITMPDIR/PassPrompt ]; then
   exit 1
fi

touch $ITMPDIR/PassPrompt

mathinfovar=`mathinfo 2> /dev/null`

hostnamevar=`echo $mathinfovar | awk '{print $1}'`
mathidvar=`echo $mathinfovar | awk '{print $2}'`
mathpass=$TARGET/Configuration/Licensing/mathpass

if [ ".$hostnamevar" = "." ]; then
   printm NoInstArch
   touch $ITMPDIR/AbortInstall
   exit 1
fi

if (getmathpass); then
    printm ChangePass $mathpass
    cat $mathpass
    echo
    promptname=ChangePass; defanswer=c; . printp;
    if [ .$curans = ".c" ]; then
       exit 1
    fi
else
    printm PasswordIntro
    promptname=PasswordIntro; defanswer=c; . printp;
    if [ .$curans = ".s" ]; then
        printm PasswordLater
        exit 1
    fi
    echo
fi

orgvar=""

promptname=OrgPrompt; defanswer=""; . printp;

again=1

while [ $again -eq 1 ]; do

   printm MachineName $hostnamevar
   promptname=MachineName; defanswer=a; . printp
   again=0
 
   if [ .$curans = ".s" ]; then
      printm PasswordLater
      exit 1
   fi
   
   case .$curans
   in
   
   .a) 
      . promptlic
      printm GetPassword $licensevar $hostnamevar $mathidvar
      promptname=GetPassword; defanswer=c; . printp;
   
      if [ .$curans = ".s" ]; then
         printm PasswordLater
         exit 1
      fi
   
      . promptpass
      ;;
   
   .b)
      printm MachineOther
      promptname=PasswordIntro; defanswer=c; . printp;
   
      if [ .$curans = ".s" ]; then
         exit 1
      fi
   
      . promptlic

      promptname=MachineNamePrompt; defanswer=""; . printp;
      hostnamevar=$curans
   
      promptname=MathIDPrompt; defanswer=""; . printp;
      mathidvar=$curans
   
      # This prompt is a little different than the
      # others, so this cant be moved to promptpw.
   
      promptname=PWPrompt; defanswer=""; . printp;
      passvar=$curans
      ;;
   
   .c)
      mathpass=$TARGET/Configuration/Licensing/mathpass
      if [ -f $mathpass ]; then
          printm NetworkPass $mathpass
          promptname=NetworkPass; defanswer=c; . printp;
          if [ .$curans = ".s" ]; then
              exit 1
          fi
      fi
   
      printm NetworkLicense
   
      promptname=NetworkLicense
      curans=""
      while [ ".$curans" = "." ]; do
         defanswer=""
         . printp
      done
      hostnamevar="!$curans"
      orgvar=""
      mathidvar=""
      licensevar=""
      passvar=""
      namevar=""
      ;;
   
   .d)
      . promptlic
      printm GetPassword $licensevar $hostnamevar $mathidvar
      promptname=GetPassword; defanswer=c; . printp;
 
      if [ .$curans = ".s" ]; then
         printm PasswordLater
         exit 1
      fi
   
      . promptpass
   
      printm NetworkServer "$TARGET/SystemFiles/LicenseManager/Binaries/$selected_arch/mathlm"
   
      hostnamevar="!$hostnamevar"
      ;;
   
   *)
      again=1
   
   esac
done

printm CreatingPass $mathpass

echo "$hostnamevar    $mathidvar    $licensevar    $passvar    $namevar    $orgvar"   >> $ITMPDIR/Password
exit 0
