summaryrefslogtreecommitdiff
path: root/prtreverse
blob: 64231f105a945b6b26d67dd85fd4bc8402067d40 (plain)
    1 #!/bin/bash
    2 #
    3 #   prtreverse - create a crux package from files already installed
    4 #                on the hard disk.
    5 #
    6 #	Copyright (c) 2003 by Simone Rota                  <sip@varlock.com>
    7 #
    8 #	*************************************************************************
    9 #	*                                                                       *
   10 #	* This program is free software; you can redistribute it and/or modify  *
   11 #	* it under the terms of the GNU General Public License as published by  *
   12 #	* the Free Software Foundation; either version 2 of the License, or     *
   13 #	* (at your option) any later version.                                   *
   14 #	*                                                                       *
   15 #	*************************************************************************
   16 #
   17 #   **** USE AT YOUR OWN RISK ****
   18 #
   19 
   20 VERSION="0.2"
   21 
   22 info() {
   23     echo "=======> $1"
   24 }
   25 
   26 note() {
   27     echo "=======> NOTE: $1"
   28 }
   29 
   30 error() {
   31     echo "=======> ERROR: $1"
   32 }
   33 
   34 usage() {
   35     echo "Usage: prtreverse [-r] [-k] [-o] [-p] <path>"
   36     exit 1
   37 }
   38 
   39 showversion() {
   40     echo "prtreverse" $VERSION
   41 	echo "(c) 2003 by Simone Rota"
   42 	echo "This program is distributed under the GNU GPL license"
   43 }
   44 
   45 interrupted() {
   46     echo "=======> operation interrupted."
   47     exit 1
   48 }
   49 
   50 checkparams() {
   51 # Do some test on given parameters.
   52     if [ -z "$prtdir" ]; then
   53         usage
   54 		exit 1
   55     fi	
   56 
   57     if [ ! -d "$prtdir" ]; then
   58         echo "'$prtdir': directory not found"
   59     	exit -1
   60     fi
   61 
   62     if [ ! -f "$prtdir"/Pkgfile ]; then
   63         echo "no Pkgfile found in $prtdir. Skipping."
   64 	    exit 1
   65     fi
   66 
   67     if [ ! -f "$prtdir"/.footprint ]; then
   68         echo "no .footprint found in $prtdir. Skipping."
   69 	    exit 1
   70     fi
   71 
   72 }
   73 
   74 getoptions () {
   75 # Name says it all.
   76     while getopts rhvkfpo opt
   77     do
   78       case "$opt" in
   79         r) userejected=1;;
   80 		k) keepworking=1;;
   81 		f) forcecreation=1;;
   82 		o) leaveowner=1;;
   83 		p) leaveperms=1;;
   84 		h) usage
   85 		   exit 0
   86 		   ;;
   87 		v) showversion
   88 		   exit 0
   89 		   ;;
   90         \?) usage
   91 			exit -1
   92 			;;
   93       esac
   94     done
   95 	shift $(($OPTIND - 1))
   96 	prtdir="$@"
   97 }
   98 
   99 
  100 printparams() {
  101 # used for testing.
  102     echo userejected: $userejected
  103     echo test: $test
  104 }
  105 
  106 adjustperms(){
  107 # changes file permissions according to given string
  108 # (taken from footprint)
  109 	tonum $2
  110 	chmod `echo "$perms" reverse/$1`
  111 }
  112 
  113 adjustowner(){
  114 # changes file user/group according to given string
  115 # (taken from footprint)
  116     chown `echo "$2" | sed 's|/|:|g'` reverse/$1
  117 }
  118 
  119 tonum() {
  120 # change rxwrxwrxw permission format with
  121 # numeric one (777). 
  122 # I know, this code sucks.
  123     us=0
  124 	gr=0
  125 	ot=0
  126 	perms="000"
  127     str=`echo $1 | tr "r" "4"`
  128     str=`echo $str | tr "w" "2"`
  129     str=`echo $str | tr "x" "1"`
  130     str=`echo $str | tr "-" "0"`
  131 	
  132 	for i in 0 1 2
  133 	do
  134 	    let us=us+${str:$i:1}
  135 	done
  136 	for i in 3 4 5
  137 	do
  138 	    let gr=gr+${str:$i:1}
  139 	done
  140 	for i in 6 7 8
  141 	do
  142 	    let ot=ot+${str:$i:1}
  143 	done
  144 	
  145 	perms=$us$gr$ot
  146 }
  147 
  148 createpkg(){
  149 # creates tarred package
  150     cd reverse
  151 	info "Creating "$PKGTAR
  152 	tar cfz ../"$PKGTAR" *
  153 	cd ..
  154 }
  155 ###################################################
  156 # Main
  157 ###################################################
  158 
  159 trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM
  160 
  161 if [ $# -lt 1 ]; then
  162   usage
  163   exit -1
  164 fi
  165 
  166 userejected=0
  167 keepworking=0
  168 forcecreation=0
  169 goterror=0
  170 leaveowner=0
  171 leaveperms=0
  172 
  173 getoptions $@
  174 checkparams $@
  175 
  176 # removes existing working directory
  177 if [ -d "$prtdir"/reverse ]
  178 then
  179     rm -r "$prtdir"/reverse
  180 fi
  181 
  182 # make working directory and initializes Pkgfile
  183 # vars
  184 mkdir -p "$prtdir"/reverse
  185 . "$prtdir"/Pkgfile
  186 PKGMK_COMPRESSION_MODE="gz"
  187 [ -f /etc/pkgmk.conf ] && . /etc/pkgmk.conf
  188 PKGTAR="$name"#"$version"-"$release".pkg.tar.$PKGMK_COMPRESSION_MODE
  189 
  190 cd $prtdir
  191 
  192 # package already exists, exit if not in force method
  193 if [ -f "$PKGTAR" -a $forcecreation = 0 ]
  194 then
  195     echo "$PKGTAR" exists, skipping.
  196 	exit 1
  197 fi	
  198 
  199 # the main loop to read entries in .footprint
  200 # and copy files to the <path>/reverse directory.
  201 # Could be implemented better.
  202 info "Copying files..."
  203 for f in `awk '{print $3}' .footprint`
  204 do
  205     if [ ! -d /"$f" -a ! -f /"$f" ]
  206 	then
  207 	    error /$f" does not seem to be installed!"
  208 	    exit 1
  209 	fi
  210 		
  211     if [ $userejected = 1 ]
  212 	then
  213 	    if [ -f "/var/lib/pkg/rejected/""$f" ]
  214 		then
  215 			note "Using /"$f" from rejected dir"
  216             cp -p -R -f "/var/lib/pkg/rejected/""$f" reverse/"$f"	
  217 		else
  218 	        if [ -d /"$f" ]
  219 	        then
  220                 mkdir -p reverse/"$f"
  221 	        else
  222 		
  223 	            if [ -f /"$f" ]
  224 			    then
  225                     cp -p -R /"$f" reverse/"$f"
  226 	            else
  227 			        goterror=1
  228 			    fi
  229 			fi	
  230 	    fi
  231 	else		
  232 	    if [ -d /"$f" ]
  233 	    then
  234             mkdir -p reverse/"$f"
  235 	    else
  236 		    if [ -f /"$f" ]
  237 		    then
  238                 cp -p -R /"$f" reverse/"$f"
  239 		    else
  240 		        goterror=1	
  241 	        fi
  242 		fi	
  243     fi
  244 done
  245 
  246 # now we create the package adjust owner and permssions
  247 if [ $goterror = 0 -o $forcecreation = 1 ]
  248 then
  249 	# adjust owner/group
  250     if [ ! $leaveowner = 1 ]
  251     then
  252     	info "Adjusting files owner/group"
  253         for f in `awk '{print $1"|"$2"|"$3}' .footprint`
  254         do
  255      	    fowner=`echo $f | awk -F"|" '{print $2}'`
  256      	    fname=`echo $f | awk -F"|" '{print $3}'`
  257 			adjustowner $fname $fowner
  258         done
  259     fi
  260     
  261 	# adjust permissions
  262     if [ ! $leaveperms = 1 ]
  263     then
  264     	info "Adjusting files permissions"
  265         for f in `awk '{print $1"|"$2"|"$3}' .footprint`
  266         do
  267      	    fperms=`echo $f | awk -F"|" '{print $1}'`
  268      	    fname=`echo $f | awk -F"|" '{print $3}'`
  269 			adjustperms $fname "${fperms:1:9}"
  270         done
  271     fi
  272 	
  273     # finally create the package
  274     createpkg
  275 
  276 	# removes the working directory
  277     if [ ! $keepworking = 1 ]
  278   	then
  279 		info "Removing working dir"
  280         rm -r reverse
  281     fi
  282   	  
  283 fi
  284 
  285 exit 0

Generated by cgit