#!/bin/sh
# Copyright 1994, Patrick Volkerding, Moorhead, Minnesota USA 
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Sun Nov 26 12:38:25 CST 1995
# Added patch from Glenn Moloney <glenn@physics.unimelb.edu.au> to allow
# packages to be installed to directories other than /.
#

# Plamo tweaks:
# 1997/12/23 Japanise
# 2002/10/04 doinst.sh hack
#            (by Shun-ichi TAHARA <jado@flowernet.gr.jp>)
# 2002/11/04 Fix problem on symlinks with recent tar
#            (by Chisato Yamauchi <cyamauch@hst.phyas.aichi-edu.ac.jp>)
# 2003/03/28 Gather installpkge, installpkgj and makepkg
#            (by Shun-ichi TAHARA <jado@flowernet.gr.jp>)
# 2003/04/06 Fix problem on . with recent tar
#            (by KOJIMA Mitsuhiro <kojima@linet.gr.jp>
#             and TAMUKI Shoichi <tamuki@linet.gr.jp>)
# 2003/04/06 More minor tweaks
#            (by Shun-ichi TAHARA <jado@flowernet.gr.jp>)
# 2003/09/17 Fasten extracting files
#            (by TAMUKI Shoichi <tamuki@linet.gr.jp>)
# 2004/01/23 add long package name facilities
#            (by KOJIMA Mitsuhiro <kojima@Linet.gr.jp>)
#
# 2010/09/26 remove unused installpkg functions & add
#            bzip2, xz compression
#            (by KOJIMA Mitsuhiro <kojima@linet.gr.jp>)

link_script() {
 sed -e '/^#%% begin/,/^#%% end/d' $1
}

init_script() {
 sed -e '/^#%% begin/,/^#%% end/!d' $1
}

make_install_script() {
 COUNT=1
 LINE="`sed -n "$COUNT p" $1`"
 while [ ! "$LINE" = "" ]; do
  LINKGOESIN="`dirname $LINE`"
  LINKNAMEIS="`basename $LINE`"
  COUNT=`expr $COUNT + 1`
  LINKPOINTSTO="`sed -n "$COUNT p" $1`"
  echo "( cd $LINKGOESIN ; rm -rf $LINKNAMEIS )"
  echo "( cd $LINKGOESIN ; ln -sf $LINKPOINTSTO $LINKNAMEIS )"
  COUNT=`expr $COUNT + 1`
  LINE="`sed -n "$COUNT p" $1`"
 done
}


usage() {
# need to fill
echo
echo "Usage: $0 <pkgname>"
echo "<pkgname> must be abc(basename)-1.2.3(version)-i586(arch)-P1(build).t[gbx]z format"
echo "arch should be : noarch, i[3456]86, x86_64"
exit

}

check_pkgname() {

# ex 1: curl-7.20.1-i586-P1.tgz
# ex 2: openssl-0.9.8o-i586-P2.tgz

    tmp=$1
    # echo "tmp:",$tmp
    pkgname=`echo ${tmp%.t[gbx]z}`
    ext=`echo $tmp|sed "s/$pkgname\.//"` 

    basename=`echo $pkgname | cut -f1 -d'-'`
    version=`echo $pkgname | cut -f2 -d'-'`
    arch=`echo $pkgname | cut -f3 -d'-'`
    build=`echo $pkgname | cut -f4 -d'-'`

    echo "basename:"$basename
    echo "version:"$version
    echo "arch:"$arch
    echo "build:"$build
    echo "ext:"$ext

    if [ "$basename.x" = ".x" ]; then
	echo "Error: cannot find basename for $tmp"
	echo "use abc(basename)-1.2.3(version)-i586(arch)-P1(build).t[gbx]z for package name"
	usage
    fi
    if [ "$version.x" = ".x" ]; then
	echo "Error: cannot find version for  $tmp"
	echo "use abc(basename)-1.2.3(version)-i586(arch)-P1(build).t[gbx]z for package name"
	usage
    fi
    if [ "$arch.x" = ".x" ]; then
	echo "Error: cannot find arch for $tmp"
	echo "use abc(basename)-1.2.3(version)-i586(arch)-P1(build).t[gbx]z for package name"
	usage
    fi

    if [ "$arch" != "i586" -a "$arch" != "i386" -a "$arch" != "i486" -a "$arch" != "i686"  -a "$arch" != "x86_64" -a "$arch" != "noarch" ]; then
	echo "Error: cannot find arch for $tmp"
	echo "use abc(basename)-1.2.3(version)-i586(arch)-P1(build).t[gbx]z for package name"
	usage
    fi

    if [ "$build.x" = ".x" ]; then
	echo "Error: cannot find build for $tmp"
	echo "use abc(basename)-1.2.3(version)-i586(arch)-P1(build).t[gbx]z for package name"
	usage
    fi


    if [ "$ext" != "tgz" -a "$ext" != "tbz" -a "$ext" != "txz"  ]; then
	echo "Error: cannot find extension for $tmp"
	echo "use abc(basename)-1.2.3(version)-i586(arch)-P1(build).t[gbx]z(extension) for package name"
	usage
    fi
}


if [ $# != 1 ] ; then
  usage
fi

PKGNAME=$1
FILENAME=`basename $PKGNAME`

check_pkgname $FILENAME

case $ext in
    tgz) compress=gzip ;;
    tbz) compress=bzip2 ;;
    txz) compress=xz;;
esac

if find . -type l -print | grep '.*' > /dev/null; then
    ( for l in `find . -type l -print | cut -b3-`; do
       	    target=`readlink $l`
            echo "$l -> $target"	
	    echo $l >&2
	    echo $target >&2
	    done
	    ) 2> /tmp/iNsT-a.$$
    echo
    if [ ! -d install ]; then
	mkdir install
    fi

    if [ -r install/doinst.sh ]; then
	make_install_script /tmp/iNsT-a.$$ | tee --append install/doinst.sh
    else
	make_install_script /tmp/iNsT-a.$$ | tee install/doinst.sh
    fi
    
    rm -f /tmp/iNsT-a.$$
    find . -type l -print -exec rm {} \;
fi
    
if [ -r install/initpkg ]; then
    if [ ! -d install ]; then
	mkdir install
    fi
    if [ -r install/doinst.sh ]; then
	link_script install/doinst.sh > /tmp/iNsT-i.$$
	mv /tmp/iNsT-i.$$ install/doinst.sh
    fi
    echo "#%% begin initialize $2" >> install/doinst.sh
    cat install/initpkg >> install/doinst.sh
    echo "#%% end" >> install/doinst.sh
fi

if [ "$compress" = "gzip" ]; then
    tar --create --gzip --exclude $PKGNAME --exclude initpkg --file $PKGNAME `ls -f --indicator-style=none | sed '/^\.\{1,2\}$/d'`
elif [ "$compress" = "bzip2" ]; then
    tar --create --bzip2 --exclude $PKGNAME --exclude initpkg --file $PKGNAME `ls -f --indicator-style=none | sed '/^\.\{1,2\}$/d'`
else
    tar --create --xz --exclude $PKGNAME --exclude initpkg --file $PKGNAME `ls -f --indicator-style=none | sed '/^\.\{1,2\}$/d'`
fi

