#
#    Script name hints; process an incoming script and guess at the
#    name.

#    The logic for this is from the V2.2 installer; rewritten from shell
#    into awk after various problems

BEGIN {
   idhint = 0
   m30hint = 0
   m30vers = 30
   m35hint = 0
   m35vers = 35
   m40hint = 0
   m40vers = 40   
   rechint = 0
   mathexehint = 0
   vers = ".old"
}

/Mathematica 4\.0\.0/ {
   m40hint = 1
   m40vers = 400 
   next
}

/Mathematica 4\.0/ {
   m40hint = 1
   m40vers = 40
   next
}

/Mathematica 4/ {
   m40hint = 1
   m40vers = 4
   next
}

/Mathematica 3\.5\.0/ {
   m35hint = 1
   m35vers = 350
   next
}

/Mathematica 3\.5/ {
   m35hint = 1
   m35vers = 35
   next
}

/Mathematica 3\.0\.3/ {
   m30hint = 1
   m30vers = 303
   next
}
/Mathematica 3\.0\.2/ {
   m30hint = 1
   m30vers = 302
   next
}

# No script distinction between 3.0.1 and 3.0.1.1

/Mathematica 3\.0\.1/ {
   m30hint = 1
   m30vers = 301
   next
}

/Mathematica 3\.0/ {
   m30hint = 1
   m30vers = 30
   next
}

# Older versions of the installer sometimes used a leading V.

/Mathematica V4/ {
   m40hint = 1
   m40vers = 40
   next
}

/Mathematica V3\.0/ {
   m30hint = 1
   m30vers = 30
   next
}

/Mathematica V3\.5/ {
   m35hint = 1
   m35vers = 35
   next
}

/\$Id/ {
   idhint = 1
   idval = substr($3,1,3)
   next
}

/recFile\.lock/ {
   rechint = 1
   next
}

/mathexe/ {
   mathexehint = 1
}

{
   next
}

END {
   if (m40hint) {

      vers = m40vers 

   } else {

   if (m35hint) {

      vers = m35vers

   } else {

      if (m30hint) {

         vers = m30vers

      } else {

         if (idhint && idval == 2.2) {
            vers = "22"
         }

         if (idhint && idval == 2.1) {
            vers = "21"
         }

         if (idhint && idval == 2.0) {
           vers = "20"
         }

         if (!idhint) {
            if (rechint) {
              vers = "20"
            } else {
               vers = "12"
            }
         }
      }
  }
}
   print vers
}
