#!/bin/sh
# mkbase - Make VL base from slackware or VL CD.
# environment variables:
# SOURCE = the contents of Clack/VL CDROM
# TARGET = ROOT to install to
# DISTRO_NAME   = the new distro name
# DISTRO_VERSION= the new distro version
# DISTRO_MARK   = file to put the marking
# MKBASE_VECTOR = install script (if not exist, will be created)
# MKBASE_SLACK  = install script (if not exist will be created)


error_exit() {
    echo "ERROR: $1"
    exit 1
}

echo Installing BASE system to the TARGET
[ -z "$SOURCE" ] && error_exit "no SOURCE"
[ -z "$TARGET" ] && error_exit "no TARGET"

# default names
MKBASE_VECTOR=${MKBASE_VECTOR:-mkbase-vector}
MKBASE_SLACK=${MKBASE_SLACK:-mkbase-slack}

## this is the standard places, do not EDIT
export SLACKWARE=$SOURCE/slackware
export VECTOR=$SOURCE/veclinux
SLACKWARE_GROUPS="a l ap d e f k n t tcl x xap"

## Mount the CDROM
vcmount $SOURCE $SOURCE_DEV || exit 1
vcmount $TARGET $TARGET_DEV || exit 1

## create header file of base-install
function mkheader() {
cat <<"_EOF_" > $MKBASE_SLACK
#!/bin/sh
# mkbase-slack
# Installing slackware for Vector Linux
# Auto created by mkbase

SOURCE=${SOURCE:-/mnt/cdrom}
TARGET=${TARGET:-/mnt/target}

SLACKWARE=$SOURCE/slackware
export ROOT=$TARGET

## Clear the target
rm -rf $TARGET/*

NO_LIST=""
if [ "$KERNEL_VERSION" ] && [ "$KERNEL_BOOT" ]; then
    NO_LIST="$NO_LIST kernel-ide kernel-modules alsa-driver"
fi

function ipkg() {
   PKGNAME=`pkgname $(basename $1)`
   if echo "$NO_LIST" | grep -qw $PKGNAME; then
      echo Skipping $1
   else
     pkg -i $SLACKWARE/$1
     if [ $? != 0 ]; then
        echo FAILED to Install $1
        exit 1
     fi
   fi
}

# Turning off ldconfig was a hack againts glibc on Slackware 10.
# Please check if it is working in other version
chmod -x /sbin/ldconfig

## Here are the packages to be installed
## Just comment/uncomment them as necessary.
## The list was enabled according to the HOSTs /var/log/packages.
## So if your HOST was correct, there is no need to edit it.
_EOF_
}

function mkpkgs() {
  echo "## group $GRP ---------------------------------" >> $MKBASE_SLACK
  for PKG in $SLACKWARE/$GRP/*.tgz; do
     BNAME=`basename $PKG .tgz`
     echo "$GRP/$BNAME.tgz"
     PNAME=`pkgname $BNAME`
     PDESC=`cat $SLACKWARE/$GRP/$BNAME.txt | grep "$PNAME:" | head -n1 | cut -f2 -d:`
     if ls -1 /var/log/packages/$PNAME-* 2>/dev/null; then
        echo "ipkg $GRP/$BNAME.tgz   ## $PDESC" >> $MKBASE_SLACK
     else
        echo "#ipkg $GRP/$BNAME.tgz  ## $PDESC" >> $MKBASE_SLACK
     fi 
  done
  echo >> $MKBASE_SLACK
}

function mkfooter()
{
cat <<"_EOF_" >> $MKBASE_SLACK

# Turn ON ldconfig again
chmod +x /sbin/ldconfig

## Clean the slackware if DISTRO_MARK is defined
if [ "$DISTRO_MARK" ]; then
    echo "Marking $DISTRO_MARK"
    echo "$DISTRO_NAME $DISTRO_VERSION" > $ROOT/$DISTRO_MARK
    echo "vector.linux.vnet" > $ROOT/etc/HOSTNAME
    (
    cd $ROOT/etc/rc.d/
    mkdir -p slackware
    for ff in *; do
	if [ -f $ff ]; then
	    mv $ff slackware
	fi
    done
    echo "These were the original Slackware init scripts" > slackware/README
    )
else
    echo "WARNING: The Slack has not cleaned for Vector yet !"
fi

if [ "$KERNEL_VERSION" ] && [ "$KERNEL_BOOT" ]; then
    echo "WARNING: Kernel was not installed !"
    echo "You must copy kernel to the TARGET"
    echo "also alsa-driver for Kernel 2.4.x"
fi

_EOF_
}

mkbase_slack()
{
mkheader 
for GRP in $SLACKWARE_GROUPS; do
  mkpkgs $GRP
done
mkfooter
chmod +x $MKBASE_SLACK
}

mkbase_vl()
{
cat<<"EOF" > $MKBASE_VECTOR
#!/bin/sh
# Installing Vector Linux CDROM
# SOURCE and TARGET MUST HAVE BEEN DEFINED and MOUNTED !
SOURCE=${SOURCE:-/mnt/cdrom}
TARGET=${TARGET:-/mnt/target}

VECTOR=$SOURCE/veclinux

ibz2() {
   # SKIP if not exist
   if [ -f $VECTOR/$1 ]; then
	echo Extracting $1
	if ! tar -xjf $VECTOR/$1 -C $TARGET; then
	    echo "ERROR: cannot extract $1"
    	    exit 1
	fi
   else
	echo "ERROR: $1 is not exist"
    	exit 1
    fi
}

## If you to add another bulks,
## edit this list. Couldn't be easier 
ibz2 veclinux.bz2
#ibz2 xfree43.bz2

EOF
}
## Lets see, what we got here ...
if [ -d $VECTOR ]; then
  # just execute the base-vector then ...
  if [ ! -x $MKBASE_VECTOR ]; then
    echo "You do not have $MKBASE_VECTOR yet."
    echo "So I will create the initial one,"
    echo "and let you edit it before continuing"
    echo "Press <enter>"
    read pause
    mkbase_vl
    chmod +x $MKBASE_VECTOR
    if which mcedit 2>/dev/null ;then
	mcedit $MKBASE_VECTOR
    elif which vi 2>/dev/null; then
        vi $MKBASE_VECTOR
    fi
  fi
  echo INSTALLING VECTOR LINUX CDROM TO $TARGET ...
  ./$MKBASE_VECTOR
elif [ -d $SLACKWARE ]; then
  if [ ! -x $MKBASE_SLACK ]; then
    echo "You do not have $MKBASE_SLACK yet."
    echo "So I will create the initial one,"
    echo "and let you edit it before continuing"
    echo "Press <enter>"
    read pause
    mkbase_slack
    chmod +x $MKBASE_SLACK
    if which mcedit 2>/dev/null ;then
	mcedit $MKBASE_SLACK
    elif which vi 2>/dev/null; then
        vi $MKBASE_SLACK
    fi
  fi
  echo INSTALLING SLACKWARE CDROM TO $TARGET ...
  ./$MKBASE_SLACK
else
  echo "Cannot find Slackware or Vector CDROM"
  exit 1
fi

## TESTING THE BASE
cat << EOF > $TARGET/tmp/test-base
#!/bin/sh
echo "ECHO FROM THE BASE"
echo "The root dir contains"
ls
EOF
chmod +x $TARGET/tmp/test-base

chroot $TARGET /tmp/test-base
if [ $? != 0 ]; then
    exit 1
fi
