#!/bin/sh

# genpak
# Copyright (C) 2007, 2008, 2011 Joseph Rosevear

# This file is part of an application of SAM for GNU/Linux Slackware
# known as SAM-GLS.

# SAM-GLS is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.

# SAM-GLS is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# (Slackware is a registered trademark of Patrick Volkerding and
# Slackware Linux, Inc.)


# JHR 110109 I added a fifth argument for burning options like
# "blank=fast".  This required making a change to allow $4 to be "-" as
# a place holder.

# JHR 110111 I made a bunch of changes:

#    I replaced /tmp with ~/scratch

#    I put this tool and all related tools in a separate SAM menu. 
#    That won't show up in this script, but it may be useful to know.

#    All references to ~/scratch/genpak I replaced with
#    ~/scratch/genpak/stage.

#    I modified the makepkg invocation so it puts the resulting package
#    in ~/scratch/genpak/burn.  This dir is made if it doesn't exist,
#    but not wiped.  The wiping is done by a separate tool in the menu
#    that this script is in.

#    I removed the pause at the end.

#    I moved the code that does the burning to a different tool in the
#    menu that this script is in.  So now there are four arguments
#    again and no allowance for $4 to be "-".

# JHR 110112 I added code to put doinst.sh into install if it exists.
# And I changed code to use $scratch instead of ~scratch.  I did
# this by setting scratch=~/sam_temp_base/scratch, then I replaced all
# occurances of ~/scratch with $scratch.  This allowed me to remove the
# use of "echo ~...".

# JHR 110114 I changed the code to use $env_scratch instead of
# $scratch.

# $1 the dir to make a package from
# $2 the dir in which to put the installed package
# $3 the date name.  E.g., "070303ab"
# $4 optional package name

# Package will be named "`basename $2`"-"$3" unless $4 is given

# Zeroth, find the dir to make the package from and what to call it
from="`basename $1`"
pack="`basename $2`"

if [ "$4" = "" ]; then

   pack_name="${pack}-${3}"
   
else

   pack_name="${4}"

fi

# First, remove and remake dir $env_scratch/genpak/stage
rm -Rf $env_scratch/genpak/stage
mkdir --parents -m 777 $env_scratch/genpak/stage

# Second, put file slack-desc into it
mkdir --parents -m 777 $env_scratch/genpak/stage/install
cat $1/slack-desc.txt | (

   while [ 1 ]; do

      read line
      OK="$?"
   
      if [ "$OK" = "0" ]; then

         echo "${pack_name}: $line"
      
      else
   
         exit

      fi
   done
) > $env_scratch/genpak/stage/install/slack-desc

# Second and one half, put file doinst.sh into it if it exists.
if [ -a "$1/doinst.sh" ]; then

   cp -pi "$1/doinst.sh" $env_scratch/genpak/stage/install
fi

# Third, put the $from distro into it
mkdir --parents -m 777 $env_scratch/genpak/stage/$2
cp -Rpdi $1/* $env_scratch/genpak/stage/$2
#mkdir --parents -m 777 $env_scratch/genpak/stage/$from
#cp -Rpdi $1/* $env_scratch/genpak/stage/$from

# Fourth, cd to it and run makepkg. Put the result in
# $env_scratch/genpak/burn, and make the burn dir first if it doesn't
# exist.
pushd $env_scratch/genpak/stage > /dev/null

   if [ "`pwd`" = "$env_scratch/genpak/stage" ]; then

      for dir_var in burn iso; do

         if [ ! -d $env_scratch/genpak/$dir_var ]; then
      
            mkdir --parents -m 777 $env_scratch/genpak/$dir_var
         fi
      done

      echo n | (makepkg -l y "${pack_name}.tgz")
      
      mv -i "${pack_name}.tgz" $env_scratch/genpak/burn
   
   else

      echo "Problem.  Not in $env_scratch/genpak/stage. Exiting..."
      echo you are in `pwd`
      exit

   fi
popd

# Fifth, give write permission for files in $env_scratch/genpak/burn
chmod ugo+w $env_scratch/genpak/burn/*

# Sixth, throw away $env_scratch/genpak/stage.
rm -Rf $env_scratch/genpak/stage
