#!/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.

# JHR 150330 I added $5.  See below.  And now $4 can be "-" again.

# JHR 150330 I removed the code that puts $1/doinst.sh into install. 
# It isn't needed now.  Instead make a staged setup like this ("L"
# means "directory", because it looks like two lines making a corner):

#    L /tmp/mainplus
#       SAM -> SAM_pkgs/main/
#       L SAM_pkgs
#          L main
#             <all the files of main, but no doinst.sh>

# In the above example $1 is /tmp/mainplus.  $5 is /SAM_pkgs/main.
# In this way a package for main can be made that includes a link to it
# in the parent directory.  I do this in SAM.  I have

#    /opt/SAM -> SAM_pkgs/main

# JHR 150330 I changed the line that runs makepkg so that the package
# is made directly to

#    $env_scratch/genpak/burn/${pack_name}.tgz

# instead of to the current directory.  Making it to the current
# directory was giving an error.  Perhaps this is a change to makepkg.


# $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.  Can use "-" for place holder.
# $5 $1$5 is dir containing slack-desc.txt. Optional.

# 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" = "" \) -o \( "$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$5/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

# 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 "$env_scratch/genpak/burn/${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
