#!/bin/sh

wtmp=$1

if [ ! $wtmp ];
then
   echo "Usage: $0 <os image name>"
   exit
fi

Lwhich=`echo $wtmp | tr A-Z a-z`
Uwhich=`echo $wtmp | tr a-z A-Z`

echo "Adding $Lwhich $Uwhich to the tree"

if [ ! -d /export/install/images/$Uwhich ];
then
   echo "Make sure that /export/install/images/$Uwhich exists."
   exit
fi

cd /export/install/images/$Uwhich

if [ -d amd64 ];
then
    BIT=64
fi
echo  "We're creating a $BIT bit image"

echo "Creating necessary directories..."
if [ ! -d boot ];
then
   echo "...creating boot directory"
   mkdir boot
fi

if [ ! -d sys ];
then 
   echo "...creating sys directory"
   mkdir sys
fi

if [ ! -d inf ];
then
   echo "...creating inf directory"
   mkdir inf
fi

echo "Complete."

echo "Preparing boot directory."
echo "...cabextracting files..."
cabextract -d boot/ i386/startrom.n1_
cabextract -d boot/ i386/setupldr.ex_
echo "...copying files..."
cp i386/ntdetect.com boot/

echo "...editing binaries..."
echo ".....startrom.n12..."

#########cat > /tmp/startrom.$$ << FOOZLE
cat > /tmp/${Lwhich}.startrom.$$ << EOF
:%s/NTLDR/${Lwhich}/g
:wq!
EOF

vim -b -s /tmp/${Lwhich}.startrom.$$ boot/startrom.n12
#vim -b -s /tmp/startrom.$$ boot/startrom.n12

echo ".....setupldr.exe..."

cat > /tmp/${Lwhich}.setupldr.$$ << EOF
:%s/winnt.sif/${Lwhich}.sif/g
:%s/ntdetect/ntd${Lwhich}/g
:wq!
EOF

vim -b -s /tmp/$Lwhich.setupldr.$$ boot/setupldr.exe

echo "...renaming binaries..."
mv boot/ntdetect.com boot/ntd${Lwhich}.com
mv boot/setupldr.exe boot/${Lwhich}
mv boot/startrom.n12 boot/${Lwhich}.0

if [ ! -f boot/${Lwhich}.sif ];
then

echo "...creating template sif file..."
cat > boot/${Lwhich}.sif << EOF
[data]
floppyless = "1"
msdosinitiated = "1"
; Needed for second stage
Orisrc="\\\\172.30.32.16\REMINST\\${Uwhich}\i386"
OriTyp = "4"
LocalSourceOnCD = 1
;DisableAdminAccountOnDomainJoin = 1  
[SetupData]
OsLoadOptions = "/fastdetect"
; Needed for first stage
SetupSourceDevice = "\Device\LanmanRedirector\172.30.32.16\REMINST\\${Uwhich}"  
[UserData]
ComputerName = *
; if needed
;ProductID=  
EOF

else
  echo "...${Lwhich}.sif already existed..."
fi

echo "Complete."
echo ""
echo "In all likelyhood you'll want a sif from a sibling directory but that drops the placeholder"
echo "in place for you."
echo ""
echo "Preparing inf and sys directories."

if [ ${BIT} ];
then
echo "...extracting inf files..."
    cabextract -d inf amd64/[Nn][Ee][Tt]*.[Ii][Nn]_
echo "...extracting sys files..."
    cabextract -d sys -F *.sys amd64/driver.cab
else 
echo "...extracting inf files..."
    cabextract -d inf i386/[Nn][Ee][Tt]*.[Ii][Nn]_
echo "...extracting sys files..."
    cabextract -d sys -F *.sys i386/driver.cab
fi

echo "Complete."

echo "Preparing tftp stubfile boot/tftp.stub."
cat  > boot/tftp.stub << EOF
rgi ^/${Lwhich}      /images/${Uwhich}/boot/${Lwhich}
rgi ^${Lwhich}  /images/${Uwhich}/boot/${Lwhich}
rgi ^ntd${Lwhich}.com   /images/${Uwhich}/boot/\0
rgi ^${Lwhich}.sif      /images/${Uwhich}/boot/\0  
rgi ${Uwhich}/i386   ${Uwhich}/amd64
rgi ^/images/${Uwhich}/amd64/${Uwhich}/sys/(.*)   /images/${Uwhich}/sys/\1
rgi ^/images/${Uwhich}/i386/${Uwhich}/sys/(.*)   /images/${Uwhich}/sys/\1
EOF

echo "Append that file to /etc/tfpd.map"
echo "You will now need to edit the /export/install/binl/windirs.conf and restart the binld."
echo "That work isnt automated."
echo ""

echo "Creating symlinks now..."

if [ $BIT ];
then
echo "...64 bit..."
cd amd64

# this set gets you up and running for the netboot
ln -sf 1394bus.sy_  1394BUS.SY_
ln -sf bootvid.dl_  BOOTVID.dl_
ln -sf classpnp.sy_ CLASSPNP.SY_
ln -sf hidclass.sy_ HIDCLASS.SY_
ln -sf hidparse.sy_ HIDPARSE.SY_
ln -sf kdcom.dl_    KDCOM.DL_
ln -sf oprghdlr.sy_ OPRGHDLR.SY_
ln -sf pciidex.sy_  PCIIDEX.SY_
ln -sf scsiport.sy_ SCSIPORT.SY_
ln -sf setupreg.hiv SETUPREG.HIV
ln -sf spddlang.sy_ SPDDLANG.SY_
ln -sf tdi.sy_      TDI.SY_
ln -sf usbd.sy_     USBD.SY_
ln -sf usbport.sy_  USBPORT.SY_
ln -sf videoprt.sy_ VIDEOPRT.SY_
ln -sf wmilib.sy_   WMILIB.SY_
ln -sf system32 System32

# now fix the System32 dir

cd ../i386
ln -sf ../amd64/System32 System32

# this set is required for the smb based install
cd ..
ln -sf amd64 I386
cd i386
ln -sf ../amd64/kbdus.dll    KBDUS.DLL

# this is required for the nic in the vmware session
cd ../sys
rm e1G5132e.sys
ln  -sf e1g5132e.sys e1G5132e.sys
cd ..

else



echo "...32 bit..."
cd i386

# this set gets you up and running for the netboot
ln -sf 1394bus.sy_  1394BUS.SY_
ln -sf bootvid.dl_  BOOTVID.dl_
ln -sf bootvid.dl_  BOOTVID.DL_
ln -sf classpnp.sy_ CLASSPNP.SY_
ln -sf hidclass.sy_ HIDCLASS.SY_
ln -sf hidparse.sy_ HIDPARSE.SY_
ln -sf kbdus.dl_    KBDUS.DL_
ln -sf kdcom.dl_    KDCOM.DL_
ln -sf oprghdlr.sy_ OPRGHDLR.SY_
ln -sf pciidex.sy_  PCIIDEX.SY_
ln -sf scsiport.sy_ SCSIPORT.SY_
ln -sf setupreg.hiv SETUPREG.HIV
ln -sf spddlang.sy_ SPDDLANG.SY_
ln -sf tdi.sy_      TDI.SY_
ln -sf usbd.sy_     USBD.SY_
ln -sf usbport.sy_  USBPORT.SY_
ln -sf videoprt.sy_ VIDEOPRT.SY_
ln -sf wmilib.sy_   WMILIB.SY_
ln -sf system32 System32

cd ../sys
rm e1G5132e.sys
ln  -sf e1g5132e.sys e1G5132e.sys
cd ..
fi

echo "Complete."

