#! /bin/sh
# Package Create  -  @2000 by Antonio Gallo
# agx@linux.it - www.badpenguin.org - www.linux.it/~agx
# 20 May 2000, This script is used to help a mantainer to create
#              a package to be used on Bad Penguin.

# Check parameters

fn_sanity_check() {
  # Announce
  echo "----------------------------------------------------------------------"
  echoc CYAN	"Bad Penguin - Package Create $PKG_VERSION"
  # Check parameters
  if [ "$TEMPDIR" = "" ]; then
    echoc RED "Sorry, you didn't specified a temporary directory"
    exit 1
  fi
  # Include BPP options
  echo "- Reading package.conf"
  . /etc/badpenguin/bpp/package.conf
}

##### ==============================================================  #####

fn_package_check_info() {
  echoc CYAN "(1) Checking package into $TEMPDIR"
  if [ $PLEVEL -gt 1 ]; then
    echoc YELLOW "- Skipped"
    return
  fi
  # Directories
  if [ ! -d "$TEMPDIR" ]; then
    echoc YELLOW "- Creating directory $TEMPDIR"
    mkdir -p -m 750 "$TEMPDIR"
  fi
  if [ ! -d "$TEMPDIR" ]; then
    echoc RED "! Cannot create $TEMPDIR"
    exit 1
  fi
  if [ ! -d "$TEMPINST" ]; then
    echoc YELLOW "- Creating directory $TEMPINST"
    mkdir -p -m 750 "$TEMPINST"
  fi
  if [ ! -d "$TEMPINST" ]; then
    echoc RED "! Cannot create $TEMPINST"
    exit 1
  fi
  # read INFO
  if [ -f "$TEMPINST/INFO" ]; then
    echo "- Reading Package Informations ..."
    . "$TEMPINST/INFO"
  else
    echoc YELLOW "- No Package Informations are available"
  fi
  # SONAME
  echo "> Please, enter the package SONAME without any version number"
  echo -n "  ($SONAME): "
  read SONAME
  if [ "$SONAME" = "" ]; then
    echoc RED "! Nothing specified ($SONAME), giving up."
    exit 1
  fi
  # VERSION
  echo "> Please, enter the complete package's VERSION number: "
  echo -n "  ($VERSION): "
  read VERSION
  if [ "$VERSION" = "" ]; then
    echoc RED "! Nothing specified, giving up."
    exit 1
  fi
  # CATEGORY
  echo "> Please, enter the package CATEGORY from this list: "
  echo -n "  ($CATEGORY): "  
  read CATEGORY
  if [ "$CATEGORY" = "" ]; then
    echoc RED "! Nothing specified, giving up."
    exit 1
  fi
  # SUMMARY
  echo "> Please, enter a brief summary at least 50 chars: "
  echo -n "  ($SUMMARY): "
  read SUMMARY
  # COPYRIGHT
  echo "> Please, enter the License of this software (es. GPL): "
  echo -n "  ($COPYRIGHT): "  
  read COPYRIGHT
  # AUTHOR
  echo "> Please, enter the AUTHOR of this software: "
  echo -n "  ($AUTHOR): "  
  read AUTHOR
  # HOMEPAGE
  echo "> Please, enter the HOMEPAGE of this software: "
  echo -n "  ($HOMEPAGE): "  
  read HOMEPAGE
  # ARCH
  echo "> Please, enter the ARCHitecture of this package, use 'i386' or 'ANY' : "
  echo -n "  ($ARCH): "  
  read ARCH
  # OPTIM
  echo "> Please, enter the OPTIMized arch. of this package, use 'i386' or 'ANY' : "
  echo -n "  ($OPTIM): "
  read OPTIM
  # MANTAINER
  echo "> Please, enter the MANTAINER of this package: "
  echo -n "  ($MANTAINER): "  
  read MANTAINER
  # FULLVERSION
  FULLVERSION="${SONAME}_${VERSION}"
  echoc GREEN "- Package name: $FULLVERSION"
  echoc GREEN "- Updating package infoes"
  > "$TEMPINST/INFO"
  echo "SONAME=$SONAME"			>>"$TEMPINST/INFO"
  echo "VERSION=$VERSION"		>>"$TEMPINST/INFO"
  echo "CATEGORY=$CATEGORY"		>>"$TEMPINST/INFO"
  echo "ARCH=$ARCH"			>>"$TEMPINST/INFO"
  echo "OPTIM=$OPTIM"			>>"$TEMPINST/INFO"
  echo "SUMMARY=\"$SUMMARY\""		>>"$TEMPINST/INFO"
  echo "AUTHOR=\"$AUTHOR\""		>>"$TEMPINST/INFO"
  echo "COPYRIGHT=\"$COPYRIGHT\""	>>"$TEMPINST/INFO"
  echo "MANTAINER=\"$MANTAINER\""	>>"$TEMPINST/INFO"
  echo "HOMEPAGE=$HOMEPAGE"		>>"$TEMPINST/INFO"
  echo "DISKNAME=1"			>>"$TEMPINST/INFO"
  echo "DISKUSAGE=0"			>>"$TEMPINST/INFO"
  echo "STATUS=UNSTABLE"		>>"$TEMPINST/INFO"
  echo "  done."

  # read DESCRIPTION
  echo  "> Please, press ENTER to edit the file with a full"
  echo  "  description of this package"
  read x
  touch "$TEMPINST/DESCRIPTION"
  edit "$TEMPINST/DESCRIPTION"
  echo "  done."
  #
  echo "2" >"$TEMPLEVEL"
  PLEVEL=2
}

##### ==============================================================  #####

fn_package_check_source_info() {
  echoc CYAN "(2) Checking source package infoes into $TEMPDIR"
  if [ $PLEVEL -gt 2 ]; then
    echoc YELLOW "- Skipped"
    return
  fi
  # check if the package uses SOURCEs
  if [ ! -f "$TEMPINST/SOURCEREQUEST" ]; then
    x="C";
    while [ "$x" != "Y" -a "$x" != "y" -a "$x" != "N" -a "$x" != "n" ]; do
      echo -n "> Want to create your package from source (Y/N) ? "
      read x
    done
    if [ "$x" = "N" -o "$x" = "n" ]; then
	touch "$TEMPINST/SOURCEREQUEST"
	touch "$TEMPINST/SOURCEFILES"
	touch "$TEMPINST/SOURCEMIRRORS"
	touch "$TEMPINST/SOURCEINFO"
	return 0
    fi
  fi
  
  # read SOURCEREQUEST
  echo  "> Please, press ENTER to edit the file with the list"
  echo  "  of the packages required to build this source"
  read x
  touch "$TEMPINST/SOURCEREQUEST"
  edit "$TEMPINST/SOURCEREQUEST"

  # read SOURCEFILES
  clear
  echo "Sources:"
  ( cd ${PKG_SOURCE} ; ls *.gz *.bz2 2>/dev/null )
  echo
  echo  "> Please, ENTER the name of the archive with the source"
  echo  "  to be used as sourcefiles from $PKG_SOURCE"
  echo -n "  (`cat $TEMPINST/SOURCEFILES 2>/dev/null`) : "
  read SOURCEFILE
  if [ "$SOURCEFILE" != "" ]; then
    if [ ! -f "$PKG_SOURCE/$SOURCEFILE" ]; then
      echoc RED "File $PKG_SOURCE/$SOURCEFILE does not exists !"
      exit 1
    fi
    ( cd $PKG_SOURCE ; md5sum "$SOURCEFILE" > "$TEMPINST/SOURCEFILES" )
    if [ $? -ne 0 ]; then
      echoc RED "Error creating $TEMPINST/SOURCEFILES !"
      rm -f "$TEMPINST/SOURCEFILES"
      exit 1    
    fi
  else
    > "$TEMPINST/SOURCEFILES"
  fi
  
  # read SOURCEMIRRORS
  echo  "> Please, press ENTER to edit the file with the list"
  echo  "  of the sites where to get the package's sources"
  read x
  touch "$TEMPINST/SOURCEMIRRORS"
  edit "$TEMPINST/SOURCEMIRRORS"

  touch "$TEMPINST/SOURCEINFO"
  echo "  done."
  #
  echo "3" >"$TEMPLEVEL"
  PLEVEL=3
}

##### ==============================================================  #####

fn_package_check_source() {
  echoc CYAN "(3) Compiling package into $TEMPDIR"
  if [ $PLEVEL -gt 3 ]; then
    echoc YELLOW "- Skipped"
    return
  fi

  # check sources
  SOURCEFILE="`cut -c 35- < $TEMPINST/SOURCEFILES`"
  if [ "$SOURCEFILE" = "" ]; then
    echoc YELLOW "> This package does not uses sources"
    return 0
  fi
  echo "- Package $FULLVERSION require the tarball $SOURCEFILE"
  if [ ! -f "$PKG_SOURCE/$SOURCEFILE" ]; then
    echoc RED "! Tarball $PKG_SOURCE/$SOURCEFILE not found"
    exit 1
  fi  

  # read SOURCEINFO
  if [ -f "$TEMPINST/SOURCEINFO" ]; then
    echo "- Reading Source Package Informations ..."
    . "$TEMPINST/SOURCEINFO"
  else
    echoc YELLOW "- No Source Package Informations are available"
  fi

  # get the tarball
  if [ "$SOURCEDIR" = "" ]; then
    EXTRACT=1
  else
    if [ -f "$SOURCEDIR/.tarok" ]; then
      EXTRACT=0
    else
      EXTRACT=1
    fi
  fi
  if [ $EXTRACT -eq 1 ]; then
    echo "? Where do you want to extract the source (Default = /usr/local/src)"
    echo -n ": "
    read SOURCEPREFIX
    [ "$SOURCEPREFIX" = "" ] && SOURCEPREFIX="/usr/local/src"
    echoc YELLOW "> Extracting the tarball ..."
    ( cd $SOURCEPREFIX ; tar -zxvf "$PKG_SOURCE/$SOURCEFILE" )
    if [ $? -ne 0 ]; then
      echoc RED "! Untar error"
      exit 1
    fi
    touch "$SOURCEDIR/.tarok"
  else
    echo "- Tarball already extracted"  
  fi  

  # SOURCEDIR
  if [ "$SOURCEDIR" != "" ]; then
    echo "- sourcedir...: $SOURCEDIR"
  else    
    echoc YELLOW "- sourcedir not specified"
    SOURCEDIR=`tar -ztf "$PKG_SOURCE/$SOURCEFILE" | cut -f 1 -d '/' | uniq -d | tail -1`
    if [ "$SOURCEDIR" = "" ]; then
      echo -n "> Please, enter the source tree directory under $SOURCEPREFIX: "
      read SOURCEDIR
      if [ "$SOURCEDIR" = "" ]; then
        echoc RED "! Nothing specified, giving up."
        exit 1
      fi
    fi
    SOURCEDIR="$SOURCEPREFIX/$SOURCEDIR"
    echo "- sourcedir is $SOURCEDIR"
    if [ ! -d "$SOURCEDIR" ]; then
      echoc RED "! Directory $SOURCEDIR does not exists"
      exit 1
    fi
    cd "$SOURCEDIR"
    if [ $? -ne 0 ]; then
      echoc RED "! Cannot chdir to $SOURCEDIR"
      exit 1
    fi
    echo "SOURCEDIR=\"$SOURCEDIR\"" >> "$TEMPINST/SOURCEINFO"
  fi

  # chdir
  cd "$SOURCEDIR"
  if [ $? -ne 0 ]; then
    echoc RED "! Cannot chdir to $SOURCEDIR"
    exit 1
  fi

  # PRECONFIG
  if [ "$PRECONFIG" != "" ]; then
    echo "- preconfig...: $PRECONFIG"
  else    
    echoc YELLOW "- preconfig not specified"
    echo -n "> Please, enter the preconfig command (make distclean): "
    read PRECONFIG
    if [ "$PRECONFIG" = "" ]; then
      echoc RED "! Nothing specified, giving up."
      exit 1
    fi
    if [ -f $SOURCEDIR/[Mm]akefile ]; then
      sh -x -c "$PRECONFIG"
      REPLY=$?
      if [ $REPLY -ne 0 ]; then
        echoc RED "PRECONFIG has returned error nr. $REPLY"
	if [ $REPLY -eq 2 ]; then
	    echoc YELLOW "TIPS: If it is a Makefile error you can skip it with ENTER"
	    echoc YELLOW "      otherwise press CTRL + C to QUIT and try again"
	    read x
	else
            exit 1	
	fi
      fi
    else
      echoc YELLOW "> $PRECONFIG will not be executed without any Makefile"
    fi
    echo "PRECONFIG=\"$PRECONFIG\"" >> "$TEMPINST/SOURCEINFO"
  fi

  # CONFIGURE
  if [ "$CONFIGURE" != "" ]; then
    echo "- configure...: $CONFIGURE"
  else    
    echoc YELLOW "- configure not specified"
    if [ ! -x "$SOURCEDIR/configure" ]; then
      echoc YELLOW "- Package seems not be a GNU program (no configure script)"
    else      
      echo "? Did you read README, INSTALL and the output from ./configure --help"
      echo -n "Type YES to continue: "
      read IAGREE
      if [ "$IAGREE" != "YES" ]; then
        for i in $SOURCEDIR/*
	do
          case "`basename $i`" in
	    [a-z]*) continue ;;
            COPYING*) continue ;;
            Makefile*) continue ;;
          esac
          clear
          echoc CYAN $i
          more $i
          echoc GREEN "- Press ENTER to display the next file"
          read x
        done
        clear
        echoc CYAN "Configure help"
        configure --help | more
	clear
      fi
    fi
    echo "? Do you want to use the standard configure command"
    CONFIGURE="CFLAGS='-O6 -mcpu=pentium -malign-loops=2 -malign-jumps=2 -malign-functions=2 -D_REENTRANT' LDFLAGS='-s' ./configure --prefix=/usr --cache-file=/dev/null"
    echo "  ( $CONFIGURE )"    
    echo -n "Type YES to accept: "
    read DCHECK
    if [ "$DCHECK" != "YES" ]; then
      echo "> Please, enter the CONFIGURE command (./configure --prefix=/usr)"
      echo -n ": "
      CONFIGURE=""
      read CONFIGURE
      if [ "$CONFIGURE" = "" ]; then
        echoc RED "! Nothing specified, giving up."
        exit 1
      fi
    fi
    sh -x -c "$CONFIGURE"
    REPLY=$?
    if [ $REPLY -ne 0 ]; then
      echoc RED "CONFIGURE has returned error nr. $REPLY"
      exit 1
    fi
    echo "CONFIGURE=\"$CONFIGURE\"" >> "$TEMPINST/SOURCEINFO"
  fi

  # POSTCONFIG
  if [ "$POSTCONFIG" != "" ]; then
    echo "- postconfig..: $POSTCONFIG"
  else    
    echoc YELLOW "- POSTCONFIG not specified"
    echo -n "> Please, enter the POSTCONFIG command (make clean): "
    read POSTCONFIG
    if [ "$POSTCONFIG" = "" ]; then
      echoc RED "! Nothing specified, giving up."
      exit 1
    fi
    sh -x -c "$POSTCONFIG"
    REPLY=$?
    if [ $REPLY -ne 0 ]; then
      echoc RED "POSTCONFIG has returned error nr. $REPLY"
      exit 1
    fi
    echo "POSTCONFIG=\"$POSTCONFIG\"" >> "$TEMPINST/SOURCEINFO"
  fi

  # COMPILE
  if [ "$COMPILE" != "" ]; then
    echo "- compile.....: $COMPILE"
  else    
    echoc YELLOW "- COMPILE not specified"
    echo -n "> Please, enter the COMPILE command (make): "
    read COMPILE
    if [ "$COMPILE" = "" ]; then
      echoc RED "! Nothing specified, giving up."
      exit 1
    fi
    sh -x -c "$COMPILE"
    REPLY=$?
    if [ $REPLY -ne 0 ]; then
      echoc RED "COMPILE has returned error nr. $REPLY"
      exit 1
    fi
    echo "COMPILE=\"$COMPILE\"" >> "$TEMPINST/SOURCEINFO"
  fi

  # MAKETEST
  if [ "$MAKETEST" != "" ]; then
    echo "- maketest....: $MAKETEST"
  else    
    echoc YELLOW "- MAKETEST not specified"
    echo -n "> Please, enter the MAKETEST command (make check): "
    read MAKETEST
    if [ "$MAKETEST" = "" ]; then
      echoc RED "! Nothing specified, giving up."
      exit 1
    fi
    sh -x -c "$MAKETEST"
    REPLY=$?
    if [ $REPLY -ne 0 ]; then
      echoc RED "MAKETEST has returned error nr. $REPLY"
      exit 1
    fi
    echo "MAKETEST=\"$MAKETEST\"" >> "$TEMPINST/SOURCEINFO"
  fi

  # PREINSTALL
  if [ "$PREINSTALL" != "" ]; then
    echo "- preinstall..: $PREINSTALL"
  else    
    echoc YELLOW "- PREINSTALL not specified"
    echo -n "> Please, enter the PREINSTALL command (make uninstall): "
    read PREINSTALL
    if [ "$PREINSTALL" = "" ]; then
      echoc RED "! Nothing specified, giving up."
      exit 1
    fi
    sh -x -c "$PREINSTALL"
    REPLY=$?
    if [ $REPLY -ne 0 ]; then
      echoc RED "PREINSTALL has returned error nr. $REPLY"
      exit 1
    fi
    echo "PREINSTALL=\"$PREINSTALL\"" >> "$TEMPINST/SOURCEINFO"
  fi

  # MILESTONE
  if [ ! -f "$MILESTONE" ]; then
    echoc GREEN "> Creating MILESTONE"
    > "$MILESTONE"
  else
    echo "- Milestone...: Ok"
  fi

  # INSTALL
  if [ "$INSTALL" != "" ]; then
    echo "- install.....: $INSTALL"
  else    
    echoc YELLOW "- INSTALL not specified"
    echo -n "> Please, enter the INSTALL command (make install): "
    read INSTALL
    if [ "$INSTALL" = "" ]; then
      echoc RED "! Nothing specified, giving up."
      exit 1
    fi
    sh -x -c "$INSTALL"
    REPLY=$?
    if [ $REPLY -ne 0 ]; then
      echoc RED "INSTALL has returned error nr. $REPLY"
      exit 1
    fi
    echo "INSTALL=\"$INSTALL\"" >> "$TEMPINST/SOURCEINFO"
  fi

  # POSTINSTALL
  if [ "$POSTINSTALL" != "" ]; then
    echo "- postinstall.: $POSTINSTALL"
  else    
    echoc YELLOW "- POSTINSTALL not specified"
    echo -n "> Please, enter the POSTINSTALL command (ldconfig): "
    read POSTINSTALL
    if [ "$POSTINSTALL" = "" ]; then
      echoc RED "! Nothing specified, giving up."
      exit 1
    fi
    sh -x -c "$POSTINSTALL"
    REPLY=$?
    if [ $REPLY -ne 0 ]; then
      echoc RED "POSTINSTALL has returned error nr. $REPLY"
      exit 1
    fi
    echo "POSTINSTALL=\"$POSTINSTALL\"" >> "$TEMPINST/SOURCEINFO"
  fi

  # REMOVESOURCE
  if [ "$REMOVESOURCE" != "" ]; then
    echo "- removesource: $REMOVESOURCE"
  else    
    echoc YELLOW "- REMOVESOURCE not specified"
    echo -n "> Please, enter the REMOVESOURCE flag (0=no or 1=yes): "
    read REMOVESOURCE
    if [ "$REMOVESOURCE" = "" ]; then
      echoc RED "! Nothing specified, giving up."
      exit 1
    fi
    echo "REMOVESOURCE=\"$REMOVESOURCE\"" >> "$TEMPINST/SOURCEINFO"
  fi
  echo "  done."  
  #
  echo "4" >"$TEMPLEVEL"
  PLEVEL=4
}

##### ==============================================================  #####

fn_package_scan() {
  echoc CYAN "(4) Scanning for changed files $TEMPDIR"
  if [ $PLEVEL -gt 4 ]; then
    echoc YELLOW "- Skipped"
    return
  fi

  # MILESTONE
  if [ ! -f "$MILESTONE" ]; then
    echoc RED    "! No MILESTONE was found"
    echoc YELLOW "  You can press ENTER to continue and create a new MILESTONE ..."
    read x
    touch "$MILESTONE"
    echoc YELLOW "  Milestone has been created, please, now you can just switch"
    echoc YELLOW "  to another vcs to install the software to be packaged, after that"
    echoc YELLOW "  you can press ENTER to begin the analisys of changed files ..."
    read x
  fi

  # Hold your horses
  echo "- Syncing disks ..."
  sync
  echo -n "- Wait 5 seconds, please "
  for i in 1 2 3 4 5 ; do
    echo -n "."
    sleep 1
  done
  echo
  
  # Scanning ...
  echoc GREEN "Finding changes into the filesystem, at the end you'll edit them ... "
  echo "# Package Create - scanning on `date` " >$CHANGETEMP
  echo "# Please, edit this file removing comment and unwanted entries " >>$CHANGETEMP
  echo -n "> Scanning : "
  echo -n " var "  ; find /var  -xdev -cnewer $MILESTONE -newer $MILESTONE >>$CHANGETEMP
  echo -n " bin "  ; find /bin  -xdev -cnewer $MILESTONE -newer $MILESTONE >>$CHANGETEMP
  echo -n " etc "  ; find /etc  -xdev -cnewer $MILESTONE -newer $MILESTONE >>$CHANGETEMP
  echo -n " lib "  ; find /lib  -xdev -cnewer $MILESTONE -newer $MILESTONE >>$CHANGETEMP
  echo -n " root " ; find /root -xdev -cnewer $MILESTONE -newer $MILESTONE >>$CHANGETEMP
  echo -n " sbin " ; find /sbin -xdev -cnewer $MILESTONE -newer $MILESTONE >>$CHANGETEMP
  echo -n " usr "  ; find /usr  -xdev -cnewer $MILESTONE -newer $MILESTONE >>$CHANGETEMP
  echo -n " opt "  ; find /opt  -xdev -cnewer $MILESTONE -newer $MILESTONE >>$CHANGETEMP
  echo " sorting "
  sort $CHANGETEMP > $CHANGELIST 
  rm -f $CHANGETEMP
  edit $CHANGELIST
  echo "  done."  
  #
  echo "5" >"$TEMPLEVEL"
  PLEVEL=5
}

##### ==============================================================  #####

fn_package_pack() {
  echoc CYAN "(5) Gathering files from the filesystem ..."
  if [ $PLEVEL -gt 5 ]; then
    echoc YELLOW "- Skipped"
    return
  fi

  # Check changelist  
  if grep -q "^#" $CHANGELIST ; then
    echoc RED "! Please remove comments from $CHANGELIST then try again"
    exit 1
  fi
  
  # Pack
  ( cd / ; tar -O -p -T $CHANGELIST -c | tar -v -x -p -C $TEMPDIR )
  if [ $? -ne 0 ]; then
    echoc RED "! Error while gathering files"
    exit 1
  fi
  echo "  done."  
  #
  echo "6" >"$TEMPLEVEL"
  PLEVEL=6
}

##### ==============================================================  #####

fn_package_check() {
  echoc CYAN "(6) Checking the package"
  if [ $PLEVEL -gt 6 ]; then
    echoc YELLOW "- Skipped"
    return
  fi
  
  # Squeeze
  echo - "> Squeezing $TEMPDIR ..."
  find  $TEMPDIR \( -name \*~ -or -name \*\# \) -exec rm -vf {} \;

  # Deprecated dirs
  if [ -d $TEMPDIR/usr/doc ]; then
    echoc YELLOW "- moveing /usr/doc to /usr/share/doc"
    mv -v $TEMPDIR/usr/doc/ $TEMPDIR/usr/share/doc/
  fi
  if [ -d $TEMPDIR/usr/man ]; then
    echoc YELLOW "- moveing /usr/man to /usr/share/man"
    mv -v $TEMPDIR/usr/man/ $TEMPDIR/usr/share/man/
  fi
  if [ -d $TEMPDIR/usr/info ]; then
    echoc YELLOW "- moveing /usr/info to /usr/share/info"
    mv -v $TEMPDIR/usr/info/ $TEMPDIR/usr/share/info/
  fi

  # Check obsolete files  
  for i in PRE-DELETE DELETE POST-DELETE PRUNE CONFIGURE doinst.sh
  do
    if [ -f $TEMPINST/$i ]; then
      echoc YELLOW "! File $TEMPINST/$i is obsolete !"
      exit 1
    fi
  done

  # Check install scripts
  for i in PRE-INSTALL  INSTALL  POST-INSTALL X-POST-INSTALL \
	    PRE-REMOVE REMOVE POST-REMOVE PURGE
  do
    TEMPSCRIPT="$TEMPINST/$i"
    if [ -f $TEMPSCRIPT ]; then
      echo $i
      chmod 700 $TEMPSCRIPT
      chown root.root $TEMPSCRIPT

      grep -q "^set -e" $TEMPSCRIPT
      if [ $? -ne 0 ]; then
        echoc YELLOW "You are not using 'set -e' into this script"
        echo "Press ENTER to continue"
        read x
      fi

      grep -q "^set -x" $TEMPSCRIPT
      if [ $? -eq 0 ]; then
        echoc YELLOW "You are using 'set -x' into this script"
        echo "Press ENTER to continue"
        read x
      fi
    fi
  done
  
  # Se c'e' incoming ci deve essere anche PURGE
  if [ -d $TEMPDIR/incoming ]; then
    if [ ! -f $TEMPINST/PURGE ]; then
      echoc YELLOW "! directoy 'incoming' is present, you should write a PURGE script !"
      echoc YELLOW "  Press ENTER to continue"
      read x
    fi
  fi

  # Icons
  if [ -d $TEMPDIR/usr/bin -o -d $TEMPDIR/usr/local/bin ]; then
    if [ ! -d $TEMPDIR/usr/share/gnome/apps ]; then
      echoc YELLOW "! There are programs but no icons under /usr/share/gnome/apps"
      echoc YELLOW "  Press ENTER to continue"
      read x
    fi
  fi


  # DOCDIR
  DOCDIR="$TEMPDIR/usr/share/doc/"
  if [ -d $DOCDIR ]; then
    echoc GREEN "- Changing mode to docs ..."
    find $DOCDIR -type f -exec chmod 644 {} \;
    rmdir $DOCDIR          2>/dev/null
  fi

  # INFO
  if [ -d "$TEMPDIR/usr/share/info" ]; then
    #TODO echoc GREEN "- Adding texinfo to the deps"
    #TODO echo $DEPS_INFO >> $PKG_INSTALL/REQUEST
    echo "- Compressing infoes ..."
    find "$TEMPDIR/usr/share/info" -type f -exec gzip -9f {} 2>/dev/null \;
    echo "- Changing mode to infoes ..."
    find "$TEMPDIR/usr/share/info" -type f -exec chmod 644 {} \;
    # Scripts
    touch $TEMPINST/POST-INSTALL
    touch $TEMPINST/PRE-REMOVE
    grep -q "install-info" $TEMPINST/PRE-REMOVE
    if [ $? -ne 0 ]; then
      echoc YELLOW "You are not handling info files in PRE-REMOVE"
      echo "Press ENTER to continue"
      read x
    fi
    grep -q "install-info" $TEMPINST/POST-INSTALL
    if [ $? -ne 0 ]; then
      echoc YELLOW "You are not handling info files in POST-INSTALL"
      echo "Press ENTER to continue"
      read x
    fi
  fi

  # MAN
  if [ -d "$TEMPDIR/usr/share/man" ]; then
    #echoc GREEN "[MAN] Adding man to the deps"
    #echo $DEPS_MAN >> $PKG_INSTALL/REQUEST
    echo "- Compressing manpages ..."
    find $TEMPDIR/usr/share/man -type f -exec gzip -v9f {} 2>/dev/null \;
    echo "- Changing mode to manpages ..."
    find $TEMPDIR/usr/share/man -type f -exec chmod 644 {} \;
  fi

  # Stripping Binaries
  echoc GREEN -n "Stripping  binaries:"
  for i in  "bin"  "sbin"  "usr/bin"  "usr/X11R6/bin"  "usr/sbin"  "usr/local/bin"  "usr/local/sbin" "opt/bin" "opt/sbin" ; do
    if [ -d $TEMPDIR/$i ]; then
      echo -n " $i"
      find "$TEMPDIR/$i"  -type f -exec strip --strip-unneeded {} 2>/dev/null \;
    fi
    if [ -d $TEMPDIR/incoming/$i ]; then
      echo -n "incoming/$i"
      find "$TEMPDIR/incoming/$i" -type f -exec strip --strip-unneeded {} 2>/dev/null \;
    fi
  done
  echo "  done."

  # Stripping Libraries
  echoc GREEN -n "Stripping libraries:"
  for i in  "lib"  "sbin"  "usr/lib"  "usr/X11R6/lib"  "usr/local/lib" "opt/lib" ; do
    if [ -d $TEMPDIR/$i ]; then
      echo -n " $i"
      find "$TEMPDIR/$i" -type f -iname "lib*so*"  -not -name "*.a" -exec strip --strip-unneeded {} \;
    fi
    if [ -d $TEMPDIR/incoming/$i ]; then
      echo "incoming/$i"
      find "$TEMPDIR/incoming/$i"  -type f  -iname "lib*so*"  -not -name "*.a" -exec strip --strip-unneeded {} \;
    fi
  done
  echo "  done."
  #
  echo "7" >"$TEMPLEVEL"
  PLEVEL=7
}


##### ==============================================================  #####

fn_package_structure() {
  echoc CYAN "(7) Checking the package structure"
  if [ $PLEVEL -gt 7 ]; then
    echoc YELLOW "- Skipped"
    return
  fi

  echo "> Press ENTER to edit the CONFLICTS file, we suggest you a value"
  echo -n "${SONAME}" > "$CONFLICTS"
  echo -n '_'        >> "$CONFLICTS"
  echo "`echo $VERSION | cut -f 1 -d'.'`" >> "$CONFLICTS"
  read x
  edit "$CONFLICTS"

  echo "> Press ENTER to edit the REQUEST file, we suggest you a value"
  read x
  touch $REQUEST
  edit "$REQUEST"

  echo "  done."
  #
  echo "8" >"$TEMPLEVEL"
  PLEVEL=8
}


##### =====================   M  A  I  N   =========================  #####

# Parameters
[ "$1" = "" ] && fn_package_help
PKGROOT="$1"
PKGINST="$PKGROOT/install"
PKGSTEP="$PKGINST/.package-create"
PKGLEVEL=""
PKGMODE="$2"
if [ "$2" = "" ]; then 
  if [ -f $PKGSTEP ]; then
    PKGLEVEL="`cat $PKGSTEP`"
  fi
else
  PKGLEVEL="$2"
fi

# Temp files
MILESTONE="$PKGINST/.milestone"
CHANGELIST="$PKGINST/.changelist"
CHANGETEMP="$PKGINST/.changetemp"

case $PKGLEVEL in
  info)	fn_package_info ;;
  *) fn_package_help ;;
esac
echo "package-create: done."
exit 0



DISKNAME="1"
STATUS="UNSTABLE"


# Functions
fn_sanity_check

# (step 1)
fn_package_check_info
# read INFO again at top level
if [ -f "$TEMPINST/INFO" ]; then
  echo "- Reading Package Informations at top level ..."
  . "$TEMPINST/INFO"
  FULLVERSION="${SONAME}_${VERSION}"
else
  echoc RED "- No Package Informations are available"
  exit 1
fi

# (2)
